删除子项时,Hibernate 多对一会删除所有父项

发布于 2024-08-18 13:42:38 字数 1662 浏览 5 评论 0原文

我有国家和州对象。我打算建立从州到国家的单向多对一关系。我不想存储对我已定义映射的国家/地区中的任何州的引用,如下所示。当我删除一个 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 技术交流群。

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

发布评论

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

评论(1

小ぇ时光︴ 2024-08-25 13:42:38

提供的映射看起来不错。实际上,完全使用您的映射执行以下代码:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

State aState = (State) session.load(State.class, stateId);
session.delete(aState);

session.getTransaction().commit();

生成以下输出:

...
Hibernate: select state0_.STATE_NAME as STATE1_1_0_, state0_.STATE_CODE as STATE2_1_0_, state0_.COUNTRY as COUNTRY1_0_ from STATE state0_ where state0_.STATE_NAME=?
Hibernate: delete from STATE where STATE_NAME=?
3270 [main] INFO org.hibernate.impl.SessionFactoryImpl - closing

事情按预期进行,我的国家/地区仍然存在。

也许显示一些代码?

The provided mapping looks fine. Actually, executing the following code using exactly your mapping:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

State aState = (State) session.load(State.class, stateId);
session.delete(aState);

session.getTransaction().commit();

Generates the following output:

...
Hibernate: select state0_.STATE_NAME as STATE1_1_0_, state0_.STATE_CODE as STATE2_1_0_, state0_.COUNTRY as COUNTRY1_0_ from STATE state0_ where state0_.STATE_NAME=?
Hibernate: delete from STATE where STATE_NAME=?
3270 [main] INFO org.hibernate.impl.SessionFactoryImpl - closing

Things are working as expected, my countries are still there.

Maybe show some code?

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