nhibernate<子类><加入>嵌套另一个子类映射(导致叶类的重复插入)

发布于 2024-09-08 07:32:44 字数 852 浏览 1 评论 0原文

这个映射有什么问题吗? 保存 Class3 的实例时,将在 Table_2 中插入两行!

第一行将 Column4 设置为 null,并将 Column3 的值设置为正确, 第二行将 Column3 设置为 null,并将 Column4 的值设置为正确!

  <class name="Class1" table="Table_1">
    <id name="Column1">
      <generator class="native" />
    </id>
    <discriminator column="ColumnDisc" />
    <property name="Column2" type="int" />
    <subclass name="Class2">
       <join table="Table_2">
         <key column="Column1" />
         <property name="Column3" type="int" />
       </join>
       <subclass name="Class3" >
          <join table="Table_2">
            <key column="Column1" />
            <property name="Column4" type="int" />
          </join>
       </subclass>
    </subclass>
  </class>

What's wrong with this mapping?
On saving an instance of Class3, two rows in Table_2 will be inserted!

first row has Column4 set to null and correct value of Column3,
and second row has Column3 set to null and correct value of Column4!

  <class name="Class1" table="Table_1">
    <id name="Column1">
      <generator class="native" />
    </id>
    <discriminator column="ColumnDisc" />
    <property name="Column2" type="int" />
    <subclass name="Class2">
       <join table="Table_2">
         <key column="Column1" />
         <property name="Column3" type="int" />
       </join>
       <subclass name="Class3" >
          <join table="Table_2">
            <key column="Column1" />
            <property name="Column4" type="int" />
          </join>
       </subclass>
    </subclass>
  </class>

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

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

发布评论

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

评论(1

陈年往事 2024-09-15 07:32:44

在哪里设置每个派生类型的鉴别器值?

如果我相信您在评论中指出的链接,则类映射如下所示:

<?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="com.javalobby.tnt.hib.User" table="USER" lazy="false">
        <id name="id" type="long" column="ID">
        <generator class="identity" />
    </id>

    <discriminator column="type" type="string"/>

    <property name="firstName" column="first_name"/>
    <property name="lastName" column="last_name"/>

    <subclass name="com.javalobby.tnt.hib.Employee" discriminator-value="employee">
        <join table="employee">
            <key column="id"/>
            <property name="jobTitle" column="job_title"/>
            <many-to-one name="supervisor" column="supervisor_id" not-null="true" />
        </join>
        <subclass name="com.javalobby.tnt.hib.Employer" discriminator-value="employer">
            <set name="subordinates">
                <key column="supervisor_id" not-null="true"/>
                <one-to-many class="com.javalobby.tnt.hib.Employee"/>
            </set>
            <join table="employer">
                <key column="id"/>
                <property name="companyCarBrand" column="company_car_brand"/>
            </join>
        </subclass>
    </subclass>
</class>

如果我将您的映射与此映射进行比较,您会发现您的 标记中缺少 discriminator-value 属性。

<class name="Class1" table="Table_1">
    <id name="Column1">
        <generator class="native" />
    </id>
    <discriminator column="ColumnDisc" />

    <property name="Column2" type="int" />

    <subclass name="Class2"> <!-- The discriminator-value="" is missing here... -->
      <join table="Table_2">
        <key column="Column1" />
        <property name="Column3" type="int" />
      </join>
      <subclass name="Class3" > <!-- The discriminator-value="" is missing here... -->
        <join table="Table_2">
          <key column="Column1" />
          <property name="Column4" type="int" />
        </join>
      </subclass>
    </subclass>
  </class>

确保为要映射的每个子类设置正确的鉴别器值。我建议尝试一下,看看它是否效果更好,或者是否会引发另一个问题。 =)

希望这有帮助! =)

Where do you set the discriminator value for each of the derived types?

If I believe the link to which you pointed in your comment, the class mapping looks like this:

<?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="com.javalobby.tnt.hib.User" table="USER" lazy="false">
        <id name="id" type="long" column="ID">
        <generator class="identity" />
    </id>

    <discriminator column="type" type="string"/>

    <property name="firstName" column="first_name"/>
    <property name="lastName" column="last_name"/>

    <subclass name="com.javalobby.tnt.hib.Employee" discriminator-value="employee">
        <join table="employee">
            <key column="id"/>
            <property name="jobTitle" column="job_title"/>
            <many-to-one name="supervisor" column="supervisor_id" not-null="true" />
        </join>
        <subclass name="com.javalobby.tnt.hib.Employer" discriminator-value="employer">
            <set name="subordinates">
                <key column="supervisor_id" not-null="true"/>
                <one-to-many class="com.javalobby.tnt.hib.Employee"/>
            </set>
            <join table="employer">
                <key column="id"/>
                <property name="companyCarBrand" column="company_car_brand"/>
            </join>
        </subclass>
    </subclass>
</class>

If I compare your mapping with this one, you lack discriminator-value attribute within your <subclass> tag.

<class name="Class1" table="Table_1">
    <id name="Column1">
        <generator class="native" />
    </id>
    <discriminator column="ColumnDisc" />

    <property name="Column2" type="int" />

    <subclass name="Class2"> <!-- The discriminator-value="" is missing here... -->
      <join table="Table_2">
        <key column="Column1" />
        <property name="Column3" type="int" />
      </join>
      <subclass name="Class3" > <!-- The discriminator-value="" is missing here... -->
        <join table="Table_2">
          <key column="Column1" />
          <property name="Column4" type="int" />
        </join>
      </subclass>
    </subclass>
  </class>

Make sure you set the right discriminator values for each of the subclasses you intend to map. I suggest trying this and figure out whether it works better, or if this raised another problem. =)

Hope this helps! =)

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