如何在 Hibernate 中映射多对一关联,其中子级具有组合键,并且其中一部分是父级中的主键?
我有一个(旧的)表结构,看起来有点像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你在这里有两个问题:
PS:如果childid已经可以识别(唯一,非空),则不需要在密钥中包含parentid(FK就足够了)。
I think you have two issues here:
PS: if childid is already identifying (unique, not-null), there's no need to have the parentid in the key (FK would be enough).
子映射中的
...
中有什么?它是否有机会声明一个名为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?正如 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.