删除子项时,Hibernate 多对一会删除所有父项
我有国家和州对象。我打算建立从州到国家的单向多对一关系。我不想存储对我已定义映射的国家/地区中的任何州的引用,如下所示。当我删除一个 State 对象时,所有国家/地区都会被删除!
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="places.Country"
table="COUNTRY"
dynamic-update="true">
<!-- Technical ID -->
<id name="name" type="string" unsaved-value="new" column="COUNTRY_NAME">
</id>
<!-- Properties -->
<property name="commonName"
column="COMMON_NAME"
/>
<property name="iso2Code"
column="ISO2_CODE"
/>
<property name="iso3Code"
column="ISO3_CODE"
/>
<property name="isoNumeric"
column="ISO_NUMERIC"
/>
<property name="ituCode"
column="ITU_CODE"
/>
<property name="ianaCode"
column="IANA_CODE"
/>
</class>
<class
name="places.State"
table="STATE"
dynamic-update="true">
<!-- Technical ID -->
<id name="name" type="string" unsaved-value="new" column="STATE_NAME">
</id>
<!-- Properties -->
<property name="code" column="STATE_CODE"
/>
<many-to-one name="country" column="COUNTRY" not-null="true" cascade="none"
class="places.Country"
/>
</class>
</hibernate-mapping>
I have Country and State objects. I intend to have unidirectional many to one relationship from State to Country. I don't want to store any references to States in Country I have defined mapping as below. When I delete even one State object, all Countries are deleted!
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="places.Country"
table="COUNTRY"
dynamic-update="true">
<!-- Technical ID -->
<id name="name" type="string" unsaved-value="new" column="COUNTRY_NAME">
</id>
<!-- Properties -->
<property name="commonName"
column="COMMON_NAME"
/>
<property name="iso2Code"
column="ISO2_CODE"
/>
<property name="iso3Code"
column="ISO3_CODE"
/>
<property name="isoNumeric"
column="ISO_NUMERIC"
/>
<property name="ituCode"
column="ITU_CODE"
/>
<property name="ianaCode"
column="IANA_CODE"
/>
</class>
<class
name="places.State"
table="STATE"
dynamic-update="true">
<!-- Technical ID -->
<id name="name" type="string" unsaved-value="new" column="STATE_NAME">
</id>
<!-- Properties -->
<property name="code" column="STATE_CODE"
/>
<many-to-one name="country" column="COUNTRY" not-null="true" cascade="none"
class="places.Country"
/>
</class>
</hibernate-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
提供的映射看起来不错。实际上,完全使用您的映射执行以下代码:
生成以下输出:
事情按预期进行,我的国家/地区仍然存在。
也许显示一些代码?
The provided mapping looks fine. Actually, executing the following code using exactly your mapping:
Generates the following output:
Things are working as expected, my countries are still there.
Maybe show some code?