JPA注解问题

发布于 2024-11-27 18:04:52 字数 1354 浏览 0 评论 0原文

我有一个表 A ,表 AB ,表 B ,表 AB 具有对表 A 和表 B 的外键引用。表 A 和表 AB 之间存在一对一关系。表 B 和表 AB 之间存在多对一关系。

我的问题是,如果我要为表 B 保存域,它会在表 AB 中保存数据,而不是在 A 中。请告诉我这是否是预期行为,或者我可以通过调用对象 B 上的保存来保存所有其他表中的所有数据吗?

Class A
{
    @OneToOne(mappedBy="abpk.a")
    @Cascade({ org.hibernate.annotations.CascadeType.ALL,
               org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    private AB ab;
}

@Entity
@AssociationOverrides({
@AssociationOverride(name = "abpk.a", joinColumns = @JoinColumn(name = "colA", referencedColumnName = "colA")),
@AssociationOverride(name = "abpk.b", joinColumns = @JoinColumn(name = "Colb", referencedColumnName = "colB")) })
Class AB
{
    ABPK abpk = new ABPK();
    A a;
    B b;

    //inner class
    class ABPK
    {
       A a;
       B b;

       @OneToOne(fetch = FetchType.LAZY)
       @Cascade({ org.hibernate.annotations.CascadeType.ALL, 
                  org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
       @JoinColumn(name="b")
       //Getter for A
       GetA();

       @ManyToOne(fetch = FetchType.LAZY)
       //Getter for B
       GetB();
     }
}

Class B
{
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "abpk.b")
    @Cascade({ org.hibernate.annotations.CascadeType.ALL,
               org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    private List<AB> abList;
}

I have a Table A , Table AB , TABLE B , Table AB has foreign key references to Table A and Table B. There is a One to One relation between Table A and Table AB.and Many to one between Table B and Table AB.

My question is if i am saving domain for table B, it is saving data in table AB but not in A Please let me know if this is the expected behavior or can i save all data in all other tables just by calling save on Object B

Class A
{
    @OneToOne(mappedBy="abpk.a")
    @Cascade({ org.hibernate.annotations.CascadeType.ALL,
               org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    private AB ab;
}

@Entity
@AssociationOverrides({
@AssociationOverride(name = "abpk.a", joinColumns = @JoinColumn(name = "colA", referencedColumnName = "colA")),
@AssociationOverride(name = "abpk.b", joinColumns = @JoinColumn(name = "Colb", referencedColumnName = "colB")) })
Class AB
{
    ABPK abpk = new ABPK();
    A a;
    B b;

    //inner class
    class ABPK
    {
       A a;
       B b;

       @OneToOne(fetch = FetchType.LAZY)
       @Cascade({ org.hibernate.annotations.CascadeType.ALL, 
                  org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
       @JoinColumn(name="b")
       //Getter for A
       GetA();

       @ManyToOne(fetch = FetchType.LAZY)
       //Getter for B
       GetB();
     }
}

Class B
{
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "abpk.b")
    @Cascade({ org.hibernate.annotations.CascadeType.ALL,
               org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    private List<AB> abList;
}

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

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

发布评论

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

评论(1

痴意少年 2024-12-04 18:04:52

这取决于您如何设置级联策略。如果您将其设置为 CASCADE_TYPE ALL,那么理论上它应该持续存在。

但无论如何,当你试图维持联系时,最好将关系的双方联系起来。

That depends on how you have set your cascade policies. If you have set it to CASCADE_TYPE ALL, then theoretically it should persists.

But in any case, it is better to wire up both sides of a relationship when you are trying to persist associations.

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