Hibernate 映射同一列两次

发布于 2024-12-07 01:34:25 字数 1130 浏览 2 评论 0原文

如何解决这个问题

实体映射中的重复列:com.abc.domain.PersonConnect 列:PERSON_ID(应使用 insert="false" 进行映射 更新=“假”)

这是我的 hbm 文件中的片段

<class name="com.abc.domain.PersonConnect" table="PERSON_CONNECT">    
    <composite-id>
        <key-many-to-one name="Parent" class="com.abc.domain.Person" column="PARENT_PERSON_ID"/>
        <key-many-to-one name="Child" class="com.abc.domain.Person" column="CHILD_PERSON_ID"/>
    </composite-id>

    <many-to-one class="com.abc.domain.Person" fetch="select" name="parent" lazy="false" > 
        <column length="20" name="PERSON_ID" not-null="true"/> 
    </many-to-one> 
    <many-to-one class="com.abc.domain.Person" fetch="select" name="child" lazy="false" > 
        <column length="20" name="PERSON_ID" not-null="true"/> 
    </many-to-one>    
</class>

,表格如下所示

Person_Connect

  • PK - PARENT_PERSON_ID
  • PK - CHILD_PERSON_ID

Person

  • PK - PERSON_ID
  • FNAME
  • LNAME

How can fix this thing

Repeated column in mapping for entity: com.abc.domain.PersonConnect
column: PERSON_ID (should be mapped with insert="false"
update="false")

this is snippet from my hbm file

<class name="com.abc.domain.PersonConnect" table="PERSON_CONNECT">    
    <composite-id>
        <key-many-to-one name="Parent" class="com.abc.domain.Person" column="PARENT_PERSON_ID"/>
        <key-many-to-one name="Child" class="com.abc.domain.Person" column="CHILD_PERSON_ID"/>
    </composite-id>

    <many-to-one class="com.abc.domain.Person" fetch="select" name="parent" lazy="false" > 
        <column length="20" name="PERSON_ID" not-null="true"/> 
    </many-to-one> 
    <many-to-one class="com.abc.domain.Person" fetch="select" name="child" lazy="false" > 
        <column length="20" name="PERSON_ID" not-null="true"/> 
    </many-to-one>    
</class>

and the table goes like this

Person_Connect

  • PK - PARENT_PERSON_ID
  • PK - CHILD_PERSON_ID

Person

  • PK - PERSON_ID
  • FNAME
  • LNAME

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

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

发布评论

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

评论(2

东风软 2024-12-14 01:34:25

你的映射是错误的,这是正确的映射。在多对一方面,列名是同一个表中的列,它是引用 Person 的主键的外部列。

<class name="com.abc.domain.PersonConnect" table="PERSON_CONNECT">

 <composite-id>
    <key-many-to-one name="Parent" class="com.abc.domain.Person" column="PARENT_PERSON_ID"/>
    <key-many-to-one name="Child" class="com.abc.domain.Person" column=" CHILD_PERSON_ID"/>
     </composite-id>

</class>

Your mapping is wrong, this is the correct mapping. On the many-to-one side the column name is the column in the same table which is a foreign referring the primary key of Person.

<class name="com.abc.domain.PersonConnect" table="PERSON_CONNECT">

 <composite-id>
    <key-many-to-one name="Parent" class="com.abc.domain.Person" column="PARENT_PERSON_ID"/>
    <key-many-to-one name="Child" class="com.abc.domain.Person" column=" CHILD_PERSON_ID"/>
     </composite-id>

</class>
征﹌骨岁月お 2024-12-14 01:34:25

嗯,一方面,“父级”和“子级”似乎不太可能映射到同一列。这可能是个问题。否则,请按照错误提示执行操作,并将 insert="false" update="false" 添加到列映射之一。一列只能“属于”单个属性。否则,您可能会遇到无法解决的情况,其中一个属性说该值应该是 x 而另一个属性说它应该是 y

Well, for one, it seems unlikely that both "Parent" and "Child" should be mapped to the same column. That's probably a problem. Otherwise, do what the error says, and add insert="false" update="false" to one of the column mappings. A column can only "belong" to a single property. Otherwise you can get into unresolvable situations where one property says the value should be x and the other says it should be y.

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