Я хочу ПОЛУЧИТЬ некоторые записи из базы данных после нажатия кнопки get из внешнего интерфейса весной, но из-за того, что сущность имеет нулевое значение, я не могу это сделать.
Ошибка в терминале:
Код: Выделить всё
java.lang.NullPointerException: Cannot invoke "javax.persistence.EntityManager.createQuery(String)" because "this.entity" is null
at com.rupeedock.pet.dao.DaoIMPL.newExpense(DaoIMPL.java:17) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~
Код: Выделить всё
package com.rupeedock.pet.dao;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import com.rupeedock.pet.entity.*;
import com.rupeedock.pet.model.*;
@Repository
public class DaoIMPL implements RupeedockDAO{
@PersistenceContext
private EntityManager entity;
@Override
public ExpenseModel newExpense(ExpenseModel expenseModel)
{
int id = 1;
ExpenseEntity present = (ExpenseEntity) entity.createQuery("select c from ExpenseEntity c where c.id = :id")
.setParameter("id", id)
.getSingleResult();
ExpenseModel model = new ExpenseModel();
model.setId(present.getId());
model.setDescription(present.getDescription());
model.setAmount(present.getAmount());
System.out.println("Retrieved Description: " + model.getDescription());
return model;
}
}
Код: Выделить всё
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'mysql:mysql-connector-java:8.0.32'
implementation 'org.springframework.session:spring-session-core' // Optional, if session handling needed
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
//implementation 'javax.transaction:javax.transaction-api:1.3' // Optional, needed for transactions
}
Код: Выделить всё
package com.rupeedock.pet.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "expenses")
public class ExpenseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String description;
private String amount;
// Getters and Setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getAmount() { return amount; }
public void setAmount(String amount) { this.amount = amount; }
}
Я хочу ПОЛУЧИТЬ некоторые записи из базы данных после нажатия кнопки get из внешнего интерфейса весной, но из-за того, что объект имеет значение null, невозможно сделать это. Если экземпляр Entity будет создан правильно, я смогу увидеть отпечатки SOP в терминале.
Подробнее здесь: https://stackoverflow.com/questions/791 ... ymanager-c