Hibernate注解级联问题

发布于 2024-11-19 03:47:58 字数 1137 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

血之狂魔 2024-11-26 03:47:58

在删除国家之前,您必须从该国家/地区的酒店列表中删除酒店,以便告诉 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.

伏妖词 2024-11-26 03:47:58

不幸的是,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.

拍不死你 2024-11-26 03:47:58

看来你用的是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 with ON DELETE CASCADE (http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文