简体   繁体   English

JPA与同一实体的多对多关系,带有额外的列

[英]Jpa many-to-many relation to same entity with extra columns

Im facing an issue with The following: 我面临以下问题:

public class Person {

    private long id;
    private String name;
    private List<Relation> relations;
    ....
}

public class Relation {

     private Person person;
     private Person relatedWith;
     private String relationType;
}

The goal is to annotate these classes with JPA so any given person can have a relation with any other person.(thus manytomany) The relation table should have a combined primary key consisting of the person.id and the relatedWith.id . 目标是使用JPA注释这些类,以便任何给定的人都可以与任何其他人建立关系。(因此,tomtomany)关系表应具有由person.idrelatedWith.id组成的组合主键。

How can i do this? 我怎样才能做到这一点? Thanks for your help! 谢谢你的帮助! Richard 理查德

您不应使用组合的主键,而应使用生成的主键(例如,关系表的序列或自动增量列),然后将有两个外键返回到Person表。

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

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