简体   繁体   English

Hibernate加入现有的多对多关系

[英]Hibernate Make a join to existing many to many relationship

I have a table that have a Many to One relationship to a Many to Many relationship. 我有一个表,该表具有多对多关系。 I would like to know how to represent that with Hibernate annotations, couldn't manage to find anything that could help me. 我想知道如何用Hibernate注释来表示,无法找到任何可以帮助我的东西。

Here are my classes : 这是我的课程:

CompanyEntity.java : CompanyEntity.java:

@Entity
@Table(name="TR_SOCIETES")
public class CompanyEntity {

    @Id
    @Column(name="Id")
    private String _id;
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name="TR_SOCIETESCONTACTS",
            joinColumns = {@JoinColumn(name="Societe")},
            inverseJoinColumns = {@JoinColumn(name="Contact")}

    )
    private Collection<ContactEntity> _contacts;
}

ContactEntity.java : ContactEntity.java:

@Entity
@Table(name="TR_CONTACTS")
public class ContactEntity {
    @Id
    @Column(name = "Id")
    private String _id;

    @ManyToMany(mappedBy = "_contacts")
    private Collection<CompanyEntity> _companies;
}

Thoses tables are linked by the relation : TR_SOCIETESCONTACTS 那些表通过以下关系链接: TR_SOCIETESCONTACTS

And here is my problem. 这是我的问题。 I have a table " Imputation " that must link to one TR_SOCIETESCONTACTS in order to reference a company and a contact but I can't manage to do it. 我有一个表“ Imputation ”,该表必须链接到一个TR_SOCIETESCONTACTS才能引用公司和联系人,但我无法做到这一点。

Do anyone have an idea? 有人有主意吗?

Thank you :) 谢谢 :)

您可以将Imputation关系与CompanyEntityContactEntity建立关系(如果这符合您的域模型)……或者-我认为这更现实-您可以创建一个新实体CompanyContacts ,该实体将映射到TR_SOCIETESCONTACTS表,多对一从CompanyEntityContactEntityImputation每个实体到此实体的关系..如果您需要向TR_SOCIETESCONTACTS关系本身添加任何状态字段,这也将是唯一的解决方案

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

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