Hibernate在级联上删除对象 [英] Hibernate delete objects on cascade
问题描述
< 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查询字符串的 但是这个方法是旧的,并且在Hibernate 3中已经被弃用了(并且转移到了经典的 如果这是一个问题,那么首选Hibernate提供的批量删除支持: 但是请注意批量删除有限制: 因此,对于批量删除,您必须在 PS:你需要 I'm sligthly confused as to how the cascade="delete" works. I defined the mapping in the following way in the City mapping file: The class Client has a foreign key to a class City. So when I run: 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 So when I run (...) Should all the clients be deleted as well The But this method is old and has been deprecated in Hibernate 3 (and moved to the "classic" If this is a concern, prefer the bulk delete support offered by Hibernate: But note that bulk delete has restrictions: So with a bulk delete, you'd have to delete the PS: You need to 这篇关于Hibernate在级联上删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Session#delete(Object)
在每个对象方面级联(因此,如果您的查询实际上是HQL查询,则客户端将被删除)。
Session
界面),我并不真的推荐它1 + N操作,并且删除大量结果的效率非常低)。
int deleteCount = session.createQuery(从Foo删除where bar =:bar)
.setParameter(bar,bar);
.executeUpdate()
City $之前删除
Client
C $ C>。但是,表演要好得多。
commit()
错误处理,即catch块中的 rollback()
)
$ b 引用
<set inverse="true" name="client" cascade="delete">
<key>
<column name="id_name"/>
</key>
<one-to-many class="model.Client"/>
</set>
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();
}
}
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 Client
s.
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).Session
interface), I do not really recommend it (it performs 1+N operations and is pretty inefficient to delete a huge number of results). int deleteCount = session.createQuery("delete from Foo where bar = :bar")
.setParameter("bar", bar);
.executeUpdate()
Client
before the City
. But performances are much better.commit()
at some point (and also improve your error handling i.e. rollback()
in the catch block) References