我正在用spring-boot,JPA和MySQL编写一个汽车租赁应用程序。我设法通过该应用程序向我的数据库添加了一些数据。我还从“汽车”表中删除了第一个实体。这导致引发以下异常:
org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.doszke.CarRent.model.Car with id 1; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.doszke.CarRent.model.Car with id 1
...
at com.doszke.CarRent.service.ReservationService.getAll(ReservationService.java:39)
at com.doszke.CarRent.PersistenceFacade.init(PersistenceFacade.java:34)
at com.doszke.CarRent.PersistenceFacade.addCar_aroundBody7$advice(PersistenceFacade.java:24)
at com.doszke.CarRent.PersistenceFacade.addCar(PersistenceFacade.java:1)
at com.doszke.CarRent.PersistenceAspectTest.aspectTest(PersistenceAspectTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
我想提到的是,该应用程序非常大,我将只发布在堆栈跟踪中提到的代码。如果还不够,我可以发布一个GIT仓库。
更短的版本
我使用默认定义的Car
从数据库中删除了一个public void JpaRepository.delete(T t)
实例(使用签名JpaRepository<T, V>
的ID为1的实例)。第二次运行该应用程序时,将所有Car
个实体提取到列表时会崩溃。
长版
我的ReservationService
类getAll()
方法上的堆栈跟踪点。接口DAO包含典型DAO类的行为。
/*imports*/
@Component("reservationService")
public class ReservationService implements DAO<Reservation>{
@Autowired
private ReservationRepository reservationRepository;
...
@Override
public List<Reservation> getAll() {
return reservationRepository.findAll();
}
}
ReservationRepository
是扩展JpaRepository的接口。这些方法没有在任何地方实现,因此使用默认的spring-boot实现。
package com.doszke.CarRent.repositories;
import com.doszke.CarRent.model.Reservation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ReservationRepository extends JpaRepository<Reservation, Long> {
//implementation- default methods of JpaRepository
}
现在类PersistenceFacade
-此处的数据正在获取到列表中。
/*imports*/
@Scope("singleton")
@Component("facade")
public class PersistenceFacade {
@Autowired
private ClientService clientService;
@Autowired
private CarService carService;
@Autowired
private ReservationService reservationService;
private List<Car> cars;
private List<Reservation> reservations;
private List<Client> clients;
public void init(){
cars = carService.getAll();
clients = clientService.getAll();
reservations = reservationService.getAll();
}
...
}
我还发布了Car
类代码。
package com.doszke.CarRent.model;
import com.doszke.CarRent.Factory;
import com.doszke.CarRent.model.behaviour.Mappable;
import com.doszke.CarRent.util.GenericTemplates;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
/**
*
* @author User
*/
@Entity
public class Car implements Serializable, Mappable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "registration_number")
private String registrationNumber;
private String brand;
private String model;
private double perDayCost;
public Car() {
}
public Car(String registrationNumber, String brand, String model, double perDayCost) {
this.registrationNumber = registrationNumber;
this.brand = brand;
this.model = model;
this.perDayCost = perDayCost;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public void setRegistrationNumber(String registrationNumber) {
this.registrationNumber = registrationNumber;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPerDayCost() {
return perDayCost;
}
public void setPerDayCost(double perDayCost) {
this.perDayCost = perDayCost;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Car)) {
return false;
}
Car other = (Car) object;
return (this.registrationNumber != null || other.getRegistrationNumber() == null) && (this.registrationNumber == null || this.registrationNumber.equals(other.getRegistrationNumber()));
}
@Override
public String toString() {
return "com.doszke.CarRent.model.Car[ id=" + id + ", registrationNumber=" + registrationNumber + ", brand=" + brand + ", model=" + model + ", perDayCost=" + perDayCost + ']';
}
@Override
public String[] map() {
String[] s = new String[5];
s[0] = String.valueOf(id);
s[1] = registrationNumber;
s[2] = brand;
s[3] = model;
s[4] = String.valueOf(perDayCost);
return s;
}
}