hibernate lazy=false 影响删除
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在将其从数据库中删除之前,您需要从相应的
Address.usrs
中删除Usr
。否则,Hibernate 会尝试通过级联重新保存它,因为usrs
配置为cascade="all-delete-orphan"
。使用
lazy = "true"
就不会出现此问题,因为级联不适用于未初始化的惰性集合。此外,将所有关系声明为 eager 并不总是解决延迟初始化问题的好方法。其他可能的解决方案包括在视图模式中打开会话和使用
join 进行细粒度的获取策略调整获取
等等。You need to remove
Usr
from the correspondingAddress.usrs
before removing it from the database. Otherwise Hibernate tries to re-save it by cascading, sinceusrs
is configured ascascade="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.