Hibernate映射问题
我有 3 个表:A、B 和 C。A 与 B 保持一对多关系,B 与 C 保持一对一关系。当我执行 session.save(objA) 时,会出现一行在 A 中创建,将在 B 中创建许多行引用 A 的 id,并且应在 C 中为 B 中引用 B 的 id 的每个条目创建一行。
现在的问题是,A 和 B 正在填充为符合预期,但在 C 中,行正在被填充但包含 B 的 id 的列填充有空值。
是hbm.xml中给出的映射有问题吗?
B.hbm.xml
<one-to-one name="propertyC" class="com.model.C"
cascade="all" fetch="join">
</one-to-one>
C.hbm.xml
<many-to-one name="propertyB" class="com.model.B"
column="B_ID" unique ="true" fetch="join" update="true" insert="true" cascade="all">
</many-to-one>
B.java
class B{
private Long id;
private C propertyC;
}
C.java
class C{
private Long id;
private Long bId;
private B propertyB;
}
I've 3 tables: A, B and C. A holds a one-to- many relation with B and B holds a one-to-one relation with C. When I do a session.save(objA), a row will be created in A, many rows will be created in B referring to the id of A and one row should be created in C for each entry in B referring to the id of B.
Now the problem is, A and B are getting populated as expected, but in C, rows are getting populated but the column containing id of B is populated with null value.
Is it the problem with the mapping given in hbm.xml?
B.hbm.xml
<one-to-one name="propertyC" class="com.model.C"
cascade="all" fetch="join">
</one-to-one>
C.hbm.xml
<many-to-one name="propertyB" class="com.model.B"
column="B_ID" unique ="true" fetch="join" update="true" insert="true" cascade="all">
</many-to-one>
B.java
class B{
private Long id;
private C propertyC;
}
C.java
class C{
private Long id;
private Long bId;
private B propertyB;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 B 和 C 之间存在 oneToOne 关系,为什么在 C 映射中将其定义为 ManyToOne(而不是 OneToOne)?
此外,如果您有双向关系,则必须定义关系的mappedBy元素:
If you have a oneToOne relation beetween B and C why this is defined like a manyToOne in the C mapping (and not a OneToOne) ?
Furthermore, if you have a bidirectional relationship, you have to define the mappedBy element of the relationship: