简体   繁体   English

使用联接表将JPA双向@ManyToOne关系映射到多个表

[英]Mapping a JPA bidirectional @ManyToOne relationship to more than one table using join tables

New with JPA and ORM, so this question may be old hat. JPA和ORM的新功能,因此这个问题可能有些陈旧。 I have the following tables: 我有以下表格:

CREATE TABLE INSTITUTION (
    inst_id BIGINT PRIMARY KEY,
    :
    :
);

and

CREATE TABLE PERSON (
    pers_id BIGINT PRIMARY KEY,
    :
    :
);

each of the above tables has a one-to-many relationship to another table, CONTACTINFO 上述每个表与另一个表CONTACTINFO具有一对多关系

CREATE TABLE CONTACTINFO (
    cont_id BIGINT PRIMARY KEY,
    :
    :
);

In the RDBMS tables, the relationship can be mapped with two join tables: 在RDBMS表中,该关系可以与两个联接表映射:

JOIN_CONTACTS_PERSON ( cont_id, pers_id )
JOIN_CONTACTS_INSTITUTION ( cont_id, inst_id )

In SQL, the relationship can be established by joining the desired tables using the correct join table, eg: 在SQL中,可以通过使用正确的联接表联接所需的表来建立关系,例如:

SELECT *
  FROM Person AS p
  JOIN Join_Contacts_Person AS jt ON p.pers_id = jt.pers_id
  JOIN Contacts AS c ON jt.cont_id = c.cont_id;

In JPA, the owning side of a one-to-many relationship must be the 'many' side, in this case, the Contacts table. 在JPA中,一对多关系的拥有方必须是“许多”方,在这种情况下,就是“联系人”表。 But there doesn't appear to be any way to map to more than one Join Table from the 'many' side using JPA annotations. 但是似乎没有任何方法可以使用JPA注释从“许多”端映射到多个Join Table。

My questions are: 1) is it not possible to map a bidirectional many-to-one association through more than one join table? 我的问题是:1)是否不可能通过多个连接表来映射双向多对一关联? ... 2) Is it also not possible to map a unidirectional many-to-one relationship through two or more join tables? ... 2)是否还不可能通过两个或多个联接表来映射单向多对一关系? ... and ... 3) would a possible work-around be using the @ManyToMany annotation for two unidirectional mappings and make the "one" side the owning side of each relationship? ... and ... 3)可能的解决方法是将@ManyToMany批注用于两个单向映射,并使“一个”面成为每种关系的拥有面?

But there doesn't appear to be any way to map to more than one Join Table from the 'many' side using JPA annotations 但是似乎没有任何方法可以使用JPA注释从“许多”侧映射到多个联接表。

You have a misconception here. 您在这里有一个误解。 You seem to think there is one ManyToOne association, and you would like to map it on several join tables. 您似乎认为存在一个ManyToOne关联,并且您希望将其映射到多个联接表上。 That's not the case. 事实并非如此。 You have two different associations here, and each one has its own join table: 您在这里有两个不同的关联,每个关联都有自己的联接表:

  1. one person has many contacts, and this is mappped using the join table between contact and person 一个人有很多联系人,这是使用联系人和人之间的联接表映射的
  2. one institution has many contacts, and this is mapped using the join table between insitution and person 一个机构有很多联系人,这是使用机构和人员之间的联接表进行映射的

So your Contact entity would look like the following: 因此,您的Contact实体如下所示:

@Entity
public class Contact
    @Id
    private Long id;

    @ManyToOne
    @JoinTable(name = "JOIN_CONTACTS_PERSON", 
               joinColumns=
                   @JoinColumn(name="CONT_ID"),
               inverseJoinColumns=
                   @JoinColumn(name="PERS_ID"))
    private Person owningPerson;

    @ManyToOne
    @JoinTable(name = "JOIN_CONTACTS_INSTITUTION", 
               joinColumns=
                   @JoinColumn(name="CONT_ID"),
               inverseJoinColumns=
                   @JoinColumn(name="INST_ID"))
    private Institution owningInstitution;

    // ...
}

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

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