我有下一個實體。 Car
與Human
有ManyToMany關係。我用的輔助類分配和之間汽車用戶刪除具有關係的實體
@Entity
public class Car implements Serializable
{
//...
@LazyCollection(LazyCollectionOption.TRUE)
@OneToMany(cascade = CascadeType.ALL, mappedBy = "car")
private Set<Assign> cars = new HashSet<Assign>();
//...
}
@Entity
class Assign implements Serializable
{
//...
@LazyCollection(LazyCollectionOption.FALSE)
@ManyToOne
@JoinColumn(name = "HUMAN_CAR", nullable = false)
private Human human;
@LazyCollection(LazyCollectionOption.FALSE)
@ManyToOne
@JoinColumn(name = "CAR_HUMAN", nullable = false)
private Car car;
//..
}
@Entity
public class Human implements Serializable
{
//...
@LazyCollection(LazyCollectionOption.TRUE)
@OneToMany(cascade = CascadeType.ALL, mappedBy = "human")
private Set<Assign> cars = new HashSet<Assign>();
// ...
}
現在我嘗試刪除裏面的容器管理事務
public void deleteCar(final long id)
{
final Car car = entityManager.find(roleId, Car.class);
entityManager.remove(car);
}
車,但我得到
Caused by: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.dto.Assign#<null>]
也許你必須刪除所有'Assign'-s,然後再移除'Car'。 – gaborsch
@GaborSch但我有'CascadeType.ALL'。我認爲Hibernate應該刪除它們 – Ilya
爲什麼你使用'Assign'實體?你可以使用'ManyToMany'關係。或者'Assign'實體中還有其他字段嗎? – home