Hibernate映射问题

发布于 2024-11-28 07:36:44 字数 864 浏览 2 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(1

千纸鹤 2024-12-05 07:36:44

如果 B 和 C 之间存在 oneToOne 关系,为什么在 C 映射中将其定义为 ManyToOne(而不是 OneToOne)?

此外,如果您有双向关系,则必须定义关系的mappedBy元素:

@Entity
private static class B {
    @Id
    private Long id;

    @OneToOne
    private C propertyC;
}


@Entity
class C {

    @Id
    private Long id;
    private Long bId;

    @OneToOne (mappedBy="propertyC")
    private B propertyB;
}

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:

@Entity
private static class B {
    @Id
    private Long id;

    @OneToOne
    private C propertyC;
}


@Entity
class C {

    @Id
    private Long id;
    private Long bId;

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