我有一个名为“House”的实体,其中包含多个“Room”,设置如下:
房子.java:
@Entity
public class House {
@OneToMany(mappedBy = "house", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Item> rooms;
...
}
房间.java:
@Entity
public class Room {
@ManyToOne
@JoinColumn(name = "houseId")
private House house;
...
}
现在,如果我直接通过实体管理器删除一个房间,它不会被删除:
getEntityManager().contains(room); // returns true
getEntityManager().remove(room);
getEntityManager().flush();
如果我从集合中删除它,它会被删除:
house.getRooms().remove(room);
getEntityManager().persist();
getEntityManager().flush();
谁能解释我这种行为?谢谢!