使用hql进行休眠删除 [英] hibernate delete using hql

查看:144
本文介绍了使用hql进行休眠删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在hibernate中有以下映射:

 < class name =com.foo.bulk_delete.test3.lim .bl.pom.impl.P4table =P4_BDELETE> 
< list lazy =falsefetch =selectbatch-size =25name =stringstable =L_st_49eea>
< key>
< column name =f_oid $ 1index =I_oid $ _49eeasql-type =char(35)/>
< / key>
< index column =s_idx $/>
< element type =VarcharStringType>
< column name =s_elem $ _5eb03length =512/>
< / element>
< / list>
< / class>

如果我想删除hql中的所有P4元素,我会做类似于



 从com.foo.bulk_delete.test3.lim.bl.pom.impl.P4删除p4 

但显然我有违反约束的异常,因为我需要删除内部的L_st_49eea表,但我不知道如何在hql中执行它。 / p>

解决方案

您无法单独通过HQL完成此操作。您必须先删除引用的实体,然后删除父实体。 />
我建议使用删除级联以及外键约束,然后您可以通过父级上的简单HQL进行删除。

I have the following mapping in hibernate:

    <class name="com.foo.bulk_delete.test3.lim.bl.pom.impl.P4" table="P4_BDELETE">
    <list lazy="false" fetch="select" batch-size="25" name="strings" table="L_st_49eea" >
        <key>
            <column name="f_oid$1" index="I_oid$_49eea" sql-type="char(35)"/>
        </key>
        <index column="s_idx$"/>
        <element type="VarcharStringType">
            <column name="s_elem$_5eb03"  length="512"/>
        </element>
    </list>
</class>

if I want to delete all P4 elements in hql i do something similar to

delete from com.foo.bulk_delete.test3.lim.bl.pom.impl.P4 p4

but obviously I have a constraint violation exception because I need to delete the inner "L_st_49eea" table but I don't know how to do it in hql.

解决方案

You cannot do this via HQL alone.. you have to first delete the referenced entities and then delete the parent entity.
I would recommend to use delete cascade along with the foreign key constraint and then you can delete via simple HQL on parent.

这篇关于使用hql进行休眠删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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