简体   繁体   English

映射JPA复合外键

[英]Mapping JPA Composite Foreign Key

I'm new with JPA, and want to create a Database with this relation : 我是JPA的新手,并且想要创建一个具有以下关系的数据库:

|Participant|
|id : INT (PK) | id_event : INT (PK, FK) |

|Event|
|id : INT (PK) |

I'm totally lost and barely figure the syntax of the examples I found :/ 我完全迷路了,几乎看不出我发现的示例的语法:/

But I understood I need to create an other class to contain the two pieces of the PK, which leads to another question : can this class be an inner-class (for optimisation purposes) ? 但是我知道我需要创建另一个类来包含PK的两个部分,这导致另一个问题:这个类可以是一个内部类(出于优化目的)吗?

I hope I'm not asking too much but I really want to get it. 我希望我不要问太多,但我真的很想得到它。

Your entities might be like this: 您的实体可能是这样的:

@Entity
public class Participant {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(fetch = FetchType.LAZY)    // or any other relation
    private List<Event> events;

    // fields, constructors, getters, setters
}

@Entity
public class Event {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    // fields, constructors, getters, setters
}

In this case JPA will create 3 tables with the following queries (SQL dialect will vary from DB to DB, in this case I used H2 database): 在这种情况下,JPA将使用以下查询创建3个表(SQL方言因数据库而异,在这种情况下,我使用H2数据库):

CREATE TABLE Event (
  id bigint GENERATED BY DEFAULT AS IDENTITY,
  PRIMARY KEY (id)
);

CREATE TABLE Participant (
  id bigint GENERATED BY DEFAULT AS IDENTITY,
  PRIMARY KEY (id)
);

CREATE TABLE Participant_Event (
  Participant_id bigint NOT NULL,
  events_id      bigint NOT NULL
)

Participant_Event is automatically created join table to link participants and events. Participant_Event是自动创建的联接表,用于链接参与者和事件。

Here is a good example of understanding JPA entity relations . 这是理解JPA实体关系的一个很好的例子。

For a OneToMany relation you need the below entities and tables: 对于OneToMany关系,您需要以下实体和表:

  • Participant 参加者
  • Event 事件

The Event entity is simple: Event实体很简单:

@Entity
public class Event {
    @Id
    private Long id;

    // fields, constructors, getters, setters
}

The entity Participant has to hold the composite key (aka two pieces of the PK), so, every Participant is only linked once with an Event. 实体Participant必须持有复合密钥 (又称PK的两个密钥 ),因此,每个Participant仅与事件链接一次。

@Entity
public class Participant {
    @EmbeddedId
    private EventParticipantPK id;

    @OneToMany(fetch = FetchType.LAZY)
    private List<Event> events;

    // fields, constructors, getters, setters
}

The composite key is declared as an EmbeddedId . 复合键被声明为EmbeddedId

The EventParticipantPK should be like: EventParticipantPK应该类似于:

@Embeddable
public class EventParticipantPK {
    @Column (name = "PARTICIPANT_ID")
    private Long participantId;

    @Column (name = "EVENT_ID")
    private Long eventId;

    // fields, constructors, getters, setters
}

I hope this helps. 我希望这有帮助。

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

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