简体   繁体   English

休眠/ jpa double与一个实体的双向一对一关系

[英]hibernate/jpa double OneToOne Bidirectional Relationship to one entity

I have the following structure that I can't figure out how to put in the correct hibernate mapping. 我有以下结构,无法弄清楚如何放入正确的休眠映射。

public class Company{
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "details_x")
    private Details detailsX;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "details_y")
    private Details detailsY;
}

and

public class Details{
    ....
    private Type type; //Enum that could be x or y

    @OneToOne
    private Company company;
}

Company has exactly on details for x and one for y. 公司有x的详细信息和y的详细信息。 A details can only be used by one company in either x or y. 一个公司只能在x或y中使用详细信息。 Database wise I'm think the best set-up is to have 2 columns on tbl_company reference the Pk of Details. 在数据库方面,我认为最好的设置是在tbl_company上有2列引用详细信息的Pk。 But this results in the relation from Details to Company not working. 但这导致从“详细信息”到“公司”的关系不起作用。

Anyone any suggestions on how to best map this? 有人对如何最好地映射此有任何建议吗?

Edit: added type to Details 编辑:将类型添加到详细信息

You have to define mappedBy property which will say that i am not the owner like below. 您必须定义mappedBy属性,它将说我不是下面的所有者。

public class Details{
    @OneToOne(mappedBy = "detailsX", cascade = CascadeType.ALL, 
                  fetch = FetchType.LAZY, optional = false)
    private Company company;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM