Hibernate注解级联问题
我有一个 Java 类,其中包含另一个类的列表。
@Entity public class Country {
private Long id;
private List<Hotel> hotels;
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="COUNTRY_SEQ")
@SequenceGenerator(name="COUNTRY_SEQ", sequenceName="COUNTRY_SEQ", allocationSize=1)
public Long getId() {
return id;
}
public void setHotels(List<Hotel> hotels) {
this.hotels = hotels;
}
@OneToMany
@JoinTable(
name="COUNTRY_HOTELS",
joinColumns=@JoinColumn(name="COUNTRY_ID"),
inverseJoinColumns=@JoinColumn(name="HOTEL_ID")
)
public List<Hotel> getHotels() {
return hotels;
}
}
当我尝试删除一个国家/地区时,我收到“ORA-02292:违反完整性约束(HOT.fk1a1e72aaf2b226a) - 找到子记录”,因为当其子项(=酒店)仍然存在时,它无法删除该国家/地区。
然而,事情本来就是这样的!我不希望在删除国家/地区时删除我的酒店。
我尝试没有任何@Cascade注释,但失败了。我也尝试过SAVE_UPDATE,仍然失败。 那么我需要哪个@Cascade注释(或者还有其他解决方案吗?):
- PERSIST
- MERGE
- REMOVE
- REFRESH
- DELETE
- SAVE_UPDATE
- REPLICATE
- DELETE_ORPHAN
- LOCK
- EVICT
Bart
I have a Java class that contains a list of another class.
@Entity public class Country {
private Long id;
private List<Hotel> hotels;
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="COUNTRY_SEQ")
@SequenceGenerator(name="COUNTRY_SEQ", sequenceName="COUNTRY_SEQ", allocationSize=1)
public Long getId() {
return id;
}
public void setHotels(List<Hotel> hotels) {
this.hotels = hotels;
}
@OneToMany
@JoinTable(
name="COUNTRY_HOTELS",
joinColumns=@JoinColumn(name="COUNTRY_ID"),
inverseJoinColumns=@JoinColumn(name="HOTEL_ID")
)
public List<Hotel> getHotels() {
return hotels;
}
}
When I try to delete a Country, I get "ORA-02292: integrity constraint (HOT.fk1a1e72aaf2b226a) violated - child record found" because it can't delete a Country when its children (=Hotels) still exist.
However, it is MEANT to be like this! I don't want to my Hotels deleted when I delete a Country.
I tried without any @Cascade-annotation but it failed. I also tried with SAVE_UPDATE, still failed.
So which @Cascade-annotation do I need (or is there another solution?):
- PERSIST
- MERGE
- REMOVE
- REFRESH
- DELETE
- SAVE_UPDATE
- REPLICATE
- DELETE_ORPHAN
- LOCK
- EVICT
Bart
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在删除国家之前,您必须从该国家/地区的酒店列表中删除酒店,以便告诉 Hibernate 这些酒店不再有国家/地区。
You have to remove the hotels from the list of hotels of the country before deleting the country, in order to tell Hibernate that the hotels don't have a country anymore.
不幸的是,Hibernate 还不支持
ON DELETE SET NULL
级联类型。因此,您必须先手动删除引用,然后才能删除子实体。
未来请求要求 Hibernate 支持它。
Unfortunately Hibernate doesn't support
ON DELETE SET NULL
type of cascade yet.So you have to delete references manually first and only then delete child entity.
There is a future request for Hibernate to support it.
看来你用的是Oracle。它允许您通过使用
ON DELETE CASCADE
定义外键COUNTRY_ID
在数据库级别实现级联删除 (http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php)。It looks like you use Oracle. It allows you to implement cascade delete at the database level by defining foreign key
COUNTRY_ID
withON DELETE CASCADE
(http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php).