Hibernate在级联上删除对象 [英] Hibernate delete objects on cascade

查看:192
本文介绍了Hibernate在级联上删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于cascade =delete是如何工作的,我很困惑。我在City映射文件中以下列方式定义了映射:

 < set inverse =truename =client cascade =delete> 
< key>
< column name =id_name/>
< / key>
<一对多等级=model.Client/>
< / set>

类Client拥有一个City类的外键。



所以当我运行时:

  List object = null; 
尝试{
org.hibernate.Transaction tx = session.beginTransaction();
try {
session.delete(from city where row_id =+ row_id and table_id =+ table_id);
} catch(Exception e){
e.printStackTrace );
}
}

如果所有的客户端都被删除,或者我是否需要处理它?我是否将查询作为方法参数正确传递给会话的delete()方法?感谢您的帮助。
最好的问候,
sass。

解决方案


我对cascade =delete的工作方式略有困惑(...) >

级联删除操作意味着如果 delete 一个父项,这个操作将会沿着这个关联传播,所以在你的情况下,删除一个 City 实体应该被传播到 Client s。


所以当我运行时(...)所有的客户端都应该被删除

使用 HQL查询字符串 Session#delete(String)方法,执行它,遍历结果并调用 Session#delete(Object)在每个对象方面级联(因此,如果您的查询实际上是HQL查询,则客户端将被删除)。

但是这个方法是旧的,并且在Hibernate 3中已经被弃用了(并且转移到了经典的 Session 界面),我并不真的推荐它1 + N操作,并且删除大量结果的效率非常低)。

如果这是一个问题,那么首选Hibernate提供的批量删除支持:

  int deleteCount = session.createQuery(从Foo删除where bar =:bar)
.setParameter(bar,bar);
.executeUpdate()

但是请注意批量删除有限制:




  • 您无法使用别名。
  • 查询中没有内连接(尽管可以在where子句中使用子查询)。

  • 批量删除不会级联(并且不会处理连接表)。



因此,对于批量删除,您必须在 City Client C $ C>。但是,表演要好得多。



PS:你需要 commit()错误处理,即catch块中的 rollback()
$ b

引用




I'm sligthly confused as to how the cascade="delete" works. I defined the mapping in the following way in the City mapping file:

<set inverse="true" name="client" cascade="delete">
  <key>
    <column name="id_name"/>
  </key>
    <one-to-many class="model.Client"/>
 </set>

The class Client has a foreign key to a class City.

So when I run:

List object = null;
try {
   org.hibernate.Transaction tx = session.beginTransaction();
   try {
       session.delete("from City where row_id=" + row_id and table_id = " + table_id);
   } catch (Exception e) {
       e.printStackTrace();
   }
}

Should all the clients be deleted as well or do I have to handle it somehow? Am I passing the query as a method parameter correctly to the delete() method of a session? Thanks for any help. Best Regards, sass.

解决方案

I'm slightly confused as to how the cascade="delete" works (...)

Cascading a delete operation means that if you delete a parent, the operation will be propagated along the association. So in your case, deleting a City entity should be propagated to the Clients.

So when I run (...) Should all the clients be deleted as well

The Session#delete(String) method that takes a HQL query string, executes it, iterates over the results and calls Session#delete(Object) on each object respect cascading (so the Clients will be deleted if your query is really a HQL query).

But this method is old and has been deprecated in Hibernate 3 (and moved to the "classic" Session interface), I do not really recommend it (it performs 1+N operations and is pretty inefficient to delete a huge number of results).

If this is a concern, prefer the bulk delete support offered by Hibernate:

int deleteCount = session.createQuery("delete from Foo where bar = :bar") 
    .setParameter("bar", bar);
    .executeUpdate()

But note that bulk delete has restrictions:

  • You can not use aliases.
  • No inner joins in the query (although you can use subselects in the where clause).
  • A bulk delete does not cascade (and won't take care of join tables).

So with a bulk delete, you'd have to delete the Client before the City. But performances are much better.

PS: You need to commit() at some point (and also improve your error handling i.e. rollback() in the catch block)

References

这篇关于Hibernate在级联上删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆