Hibernate:级联保存时一对多关系但不删除

发布于 2024-12-21 21:30:45 字数 1700 浏览 2 评论 0原文

我的休眠有问题。

简而言之:

当关系具有属性并且我们需要级联保存、删除和更新时,如何配置与 Hibernate 的多对多关联?

大图:

想象一下以下数据库:

             Super      Mini
                 M______N
                     |
                     attribute

这里有 3 个表:

"Mini", "Super" and "Super_Mini".

现在想象一下 Super_Mini 有 1 个关系属性(显然还有键)。

好的,现在这将通过以下方式转换为 Hibernate:

 Super:
 // The relation is Many to Many, but considering that it has an attribute, this is OneToMany with the ManyMany RelationShip
 @OneToMany(mappedBy="mini", targetEntity=Mini.class)   
 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})    
 @LazyCollection(LazyCollectionOption.TRUE)
 private Set<SuperMini> superMini = new HashSet<SuperMini>();


 SuperMini:
 @Id    
 @ManyToOne(targetEntity=Super.class,fetch=FetchType.LAZY)  
 @Cascade({CascadeType.LOCK})   
 @JoinColumns({ @JoinColumn(name="...", referencedColumnName="...") })  
 private Super super;   

 @Id    
 @ManyToOne(targetEntity=Mini.class,fetch=FetchType.LAZY)   
 @Cascade({CascadeType.LOCK})   
 @JoinColumns({ @JoinColumn(name="...", referencedColumnName="...") })  
 private Mini mini;

因此,我认为配置是正确的,并且如果对象具有 Mini 子对象,则独立保存所有对象。问题是当我尝试删除对象时:

 Super data = getHibernateTemplate().load(Super.class, idSuper);

 getHibernateTemplate().getSessionFactory().getCurrentSession().clear();

 data.setMini( new HashSet<Mini>() );

 getHibernateTemplate().delete( data );
 getHibernateTemplate().getSessionFactory().getCurrentSession().flush();

Hibernate 不删除迷你关系...问题是什么?我知道如何通过HQL解决它,但也许配置不正确,我不知道。

先感谢您,

I have a problem with Hibernate.

In Short:

How to configure a ManyToMany association with Hibernate when the relationship has an attribute and we need save, delete and update in cascade?

In Large:

Imagine the following DataBase:

             Super      Mini
                 M______N
                     |
                     attribute

There are 3 tables here:

"Mini", "Super" and "Super_Mini".

Now imagine Super_Mini has 1 attribute for the relation (and obviously the keys).

Ok, now this is translating to Hibernate by the following:

 Super:
 // The relation is Many to Many, but considering that it has an attribute, this is OneToMany with the ManyMany RelationShip
 @OneToMany(mappedBy="mini", targetEntity=Mini.class)   
 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})    
 @LazyCollection(LazyCollectionOption.TRUE)
 private Set<SuperMini> superMini = new HashSet<SuperMini>();


 SuperMini:
 @Id    
 @ManyToOne(targetEntity=Super.class,fetch=FetchType.LAZY)  
 @Cascade({CascadeType.LOCK})   
 @JoinColumns({ @JoinColumn(name="...", referencedColumnName="...") })  
 private Super super;   

 @Id    
 @ManyToOne(targetEntity=Mini.class,fetch=FetchType.LAZY)   
 @Cascade({CascadeType.LOCK})   
 @JoinColumns({ @JoinColumn(name="...", referencedColumnName="...") })  
 private Mini mini;

So, I think the configuration is correct, and the save, independently if the object has Mini childrens save all of them. The problem is when I try to delete the object:

 Super data = getHibernateTemplate().load(Super.class, idSuper);

 getHibernateTemplate().getSessionFactory().getCurrentSession().clear();

 data.setMini( new HashSet<Mini>() );

 getHibernateTemplate().delete( data );
 getHibernateTemplate().getSessionFactory().getCurrentSession().flush();

Hibernate don´t delete the Mini relation... What´s the problem? I know how to solve it by HQL, but maybe the configuration is not correct, I don´t know.

Thank you in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

分分钟 2024-12-28 21:30:45

你的问题不清楚。 Super 不包含Set。它包含一个Set。所以最后的代码片段没有多大意义。

此外,Super.superMini2 上的 targetEntity 属性不正确且不必要。

CascadeType.ALL 包含 CascadeType.DELETE,因此也是不必要的。

但回答你的问题,我认为问题是删除Super级联到SuperMini2,因为关联有级联删除,但SuperMini2和Mini2之间没有级联删除,所以,Mini2实例当然不会被删除。

编辑:

答案是,OP 在编辑问题之前,在删除 Super 实体之前从 SuperMini 集合中删除了所有实体。所以对Supermini集合的级联删除就没有什么可删除的了。

Your question is not clear. Super does not contain a Set<Mini2>. It contains a Set<SuperMini2>. So the last code snippet doesn't make much sense.

Moreover, the targetEntity attribute on Super.superMini2 is incorrect, and unnecessary.

CascadeType.ALL include CascadeType.DELETE, so it's also unnecessary.

But to answer your question, I think the problem is that deleting Super cascades to SuperMini2 because the association has a cascade delete, but there is no cascade delete between SuperMini2 and Mini2, so of course, Mini2 instances are not deleted.

EDIT:

The answer is that the OP, before editing the question, removed all the entities from the collection of SuperMini before deleting the Super entity. So the cascade delete on the collection of Supermini didn't have anything to delete anymore.

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