hibernate lazy=false 影响删除

发布于 2024-11-01 05:01:36 字数 2595 浏览 0 评论 0原文

我正在尝试使用 hibernate 设置一个项目。我有两个表:用户和地址,具有以下映射:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="Address" table="ADDRESS" >
        <cache usage="read-write"/> 
        <id name="addressId" type="long">
            <column name="ADDRESS_ID" precision="22" scale="0" />
            <generator class="increment" />
        </id>
        <property name="street" type="string">
            <column name="STREET" length="50" />
        </property>
        <property name="city" type="string">
            <column name="CITY" length="20" />
        </property>
        <set name="usrs" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="ADDRESS_ID" precision="22" scale="0" not-null="true"/>
            </key>
            <one-to-many class="Usr" />
        </set>
    </class>


</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="Usr" table="USR" >
        <cache usage="read-write"/>
        <id name="usrId" type="long">
            <column name="USR_ID" precision="22" scale="0" />
            <generator class="increment" />
        </id>
        <many-to-one name="address" class="Address" >
            <column name="ADDRESS_ID" precision="22" scale="0"  />
        </many-to-one>
        <property name="logname" type="string">
            <column name="LOGNAME" length="20" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="PASSWORD" length="20" not-null="true" />
        </property>
    </class>

   <query name="Usr.by.city">
        <![CDATA[
           FROM rUsr as u          
           WHERE  u.address.city = :city 
        ]]>
    </query>
</hibernate-mapping>

如果我设置lazy=false,则删除时会出现错误: 已删除的对象将通过级联重新保存

,并且我设置了lazy=true,那么由于延迟初始化错误,我将无法访问我的对象。

任何帮助表示赞赏。

谢谢。

I'm trying to set up a project using hibernate.I have two tables : Users and Address with the following mappings :

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="Address" table="ADDRESS" >
        <cache usage="read-write"/> 
        <id name="addressId" type="long">
            <column name="ADDRESS_ID" precision="22" scale="0" />
            <generator class="increment" />
        </id>
        <property name="street" type="string">
            <column name="STREET" length="50" />
        </property>
        <property name="city" type="string">
            <column name="CITY" length="20" />
        </property>
        <set name="usrs" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="ADDRESS_ID" precision="22" scale="0" not-null="true"/>
            </key>
            <one-to-many class="Usr" />
        </set>
    </class>


</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="Usr" table="USR" >
        <cache usage="read-write"/>
        <id name="usrId" type="long">
            <column name="USR_ID" precision="22" scale="0" />
            <generator class="increment" />
        </id>
        <many-to-one name="address" class="Address" >
            <column name="ADDRESS_ID" precision="22" scale="0"  />
        </many-to-one>
        <property name="logname" type="string">
            <column name="LOGNAME" length="20" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="PASSWORD" length="20" not-null="true" />
        </property>
    </class>

   <query name="Usr.by.city">
        <![CDATA[
           FROM rUsr as u          
           WHERE  u.address.city = :city 
        ]]>
    </query>
</hibernate-mapping>

If I set lazy=false I get an error on deletion :
deleted object would be re-saved by cascade

and I set lazy=true then I won't be able to access my objects due to lazy initialization errors.

Any help is appreciated.

Thx.

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

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

发布评论

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

评论(1

2024-11-08 05:01:36

在将其从数据库中删除之前,您需要从相应的 Address.usrs 中删除 Usr。否则,Hibernate 会尝试通过级联重新保存它,因为 usrs 配置为 cascade="all-delete-orphan"

使用 lazy = "true" 就不会出现此问题,因为级联不适用于未初始化的惰性集合。

此外,将所有关系声明为 eager 并不总是解决延迟初始化问题的好方法。其他可能的解决方案包括在视图模式中打开会话和使用 join 进行细粒度的获取策略调整获取等等。

You need to remove Usr from the corresponding Address.usrs before removing it from the database. Otherwise Hibernate tries to re-save it by cascading, since usrs is configured as cascade="all-delete-orphan".

With lazy = "true" you don't have this problem since cascading is not applied to uninitialized lazy collections.

Also, declaring all relationships as eager is not always a good solution for lazy initialization problems. Other possible solutions include Open Session in View pattern and fine-grained fetch strategy tuning with join fetch and so on.

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