0
我有以下實體:級聯上的Hibernate /春數據刪除
@Entity
public class Car { ... }
@Entity
public class Driver {
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "CAR_ID")
private Car car;
...
}
@Entity
public class Penalty {
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="D_ID")
private Driver driver;
...
}
我想從Driver
和Penalty
當Car
爲carRepository.delete(car_id)
被刪除刪除所有信息。
WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1451, SQLState: 23000
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot delete or update a parent row: a foreign key constraint fails (`test`.`car_drivers`, CONSTRAINT `FK9ux9oqx6yr66cva4ro6l8m63r` FOREIGN KEY (`CAR_ID`) REFERENCES `cars` (`CAR_ID`))
ERROR: org.hibernate.internal.ExceptionMapperStandardImpl - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
ERROR: com.app.cars.controller.AdminController - Exception during deleting car due to could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
無法更改表屬性和設計。所以,需要通過修復實體模型來擺脫這個錯誤。
事情是,司機不能從汽車訪問。因此,Car沒有稱爲司機的屬性。 – nlimits
爲什麼他們無法訪問?你可以簡單地添加它們。實體類必須是「公共」的,所以它們可以從任何地方訪問。 – manish