如何在 Hibernate 中映射多对一关联,其中子级具有组合键,并且其中一部分是父级中的主键?

发布于 2024-10-03 04:31:42 字数 1587 浏览 0 评论 0原文

我有一个(旧的)表结构,看起来有点像这样:

table parent (
  parentid int (PK)
  ...
)

table child (
  parentid int (PK)
  childid int (PK)
  ...
)

也就是说,子级主键的一部分是父级主键。我尝试像这样映射它(我们使用基于 xml 的 Hibernate):

<hibernate-mapping>
<class name="com.example.MyParent" table="parent">

    <id name="parentid" column="parentid" unsaved-value="0" >
        <generator class="identity"/>
    </id>

    <set name="children" cascade="all">
        <key>
            <column name="parentid"/>
        </key>
        <one-to-many class="com.example.MyChild" />
    </set>
...
</class>

<class name="com.example.MyChild" table="child">
    <composite-id name="id" class="com.example.MyChildId">
        <key-property name="parentid" column="parentid" />
        <key-property name="childid" column="childid" />
    </composite-id>
    ...
</class>

Java 类:

public void updateParent(MyParent param) {
    ht.saveOrUpdate(param);

}

更新: 我使用了错误的关系类型(已更新),但现在我遇到了另一个问题:似乎在表中创建子行时,parentid 为空。由于parentid是复合键的一部分,因此它们插入失败。

从日志中:

DEBUG SQL - insert into Child(PARENTID, CHILDID) values (?, ?)
TRACE IntegerType - binding null to parameter: 1
TRACE IntegerType - binding '5678' to parameter: 2
WARN  JDBCExceptionReporter - SQL Error: -10, SQLState: 23502
ERROR JDBCExceptionReporter - integrity constraint violation: NOT NULL check constraint;     SYS_CT_10028 table: Child

I have a (legacy) table structure that looks a little bit like this:

table parent (
  parentid int (PK)
  ...
)

table child (
  parentid int (PK)
  childid int (PK)
  ...
)

That is, part of the primary key of child is the primary key for parent. I have tried to map this (we use xml-based Hibernate) like this:

<hibernate-mapping>
<class name="com.example.MyParent" table="parent">

    <id name="parentid" column="parentid" unsaved-value="0" >
        <generator class="identity"/>
    </id>

    <set name="children" cascade="all">
        <key>
            <column name="parentid"/>
        </key>
        <one-to-many class="com.example.MyChild" />
    </set>
...
</class>

<class name="com.example.MyChild" table="child">
    <composite-id name="id" class="com.example.MyChildId">
        <key-property name="parentid" column="parentid" />
        <key-property name="childid" column="childid" />
    </composite-id>
    ...
</class>

Java class:

public void updateParent(MyParent param) {
    ht.saveOrUpdate(param);

}

UPDATE:
I had used the wrong relation type (updated), but now I have another problem: It seems that when creating the child rows in the table, the parentid is null. Since parentid is part of the composite key, they insert fails.

From the log:

DEBUG SQL - insert into Child(PARENTID, CHILDID) values (?, ?)
TRACE IntegerType - binding null to parameter: 1
TRACE IntegerType - binding '5678' to parameter: 2
WARN  JDBCExceptionReporter - SQL Error: -10, SQLState: 23502
ERROR JDBCExceptionReporter - integrity constraint violation: NOT NULL check constraint;     SYS_CT_10028 table: Child

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

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

发布评论

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

评论(3

原谅我要高飞 2024-10-10 04:31:42

我认为你在这里有两个问题:

  1. 多对一应该在 MyChild 类中声明(如果我理解正确的话)。
  2. 将 @JoinColumn 注释与组合键一起使用时,referencedColumnName 是必需的。也许这也适用于 XML...?请参阅http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships

PS:如果childid已经可以识别(唯一,非空),则不需要在密钥中包含parentid(FK就足够了)。

I think you have two issues here:

  1. Many-to-one should be declared in the MyChild class (if I understand correctly).
  2. When using @JoinColumn annotation with composite keys, referencedColumnName is mandatory. Maybe this applies to XML as well...? See http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships

PS: if childid is already identifying (unique, not-null), there's no need to have the parentid in the key (FK would be enough).

月野兔 2024-10-10 04:31:42

子映射中的 ... 中有什么?它是否有机会声明一个名为parent 的属性,该属性映射到parentid 列?

What's in the ... in the child mapping? Does it by any chance declare a property called parent which is mapped to the parentid column?

别在捏我脸啦 2024-10-10 04:31:42

正如 Tom Anderson 所说,您需要在子级到父级上进行多对一映射,并且可能在父级集映射上坚持 inverse=true 以使 hibernate 知道子级管理该关系。

As Tom Anderson says, you'll need a many-to-one mapping on the child to the parent, and perhaps stick inverse=true on the parent set mapping to let hibernate know that the child manages the relationship.

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