简体   繁体   中英

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.

Here are my classes :

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 :

@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

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.

Do anyone have an idea?

Thank you :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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