JPA如何制作复合主键的复合外键部分 [英] JPA how to make composite Foreign Key part of composite Primary Key

查看:78
本文介绍了JPA如何制作复合主键的复合外键部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下列表格,我如何将它们映射到JPA实体:

I have following tables how can i map them to JPA entities:

TABLE Event {
EventID
SourceID
....Other event fields
PK (EventID, SourceID)
}

TABLE MEETING {
MeetingID
EventID
SourceID
...Other meeting fields
PK(MeetingID,EventID, SourceID)
FK(EventID, SourceID) //FK with Event table
}

Event表与会议表具有一对多的关系。如何在JPA中映射这种双向关系?

The Event table has one-to-many relationship with Meeting table. How can i map this bi-directional relationship in JPA?

推荐答案

@Embeddable
public class EventID {
    public int eventID;
    public int sourceID;
}

@Entity
public class Event {
    @EmbeddedId
    public EventID id;

    @OneToMany(mappedBy="event")
    public Collection<Meeting> meetings;
}

@Embeddable
public class MeetingID {
    public EventID eventID; // corresponds to ID type of Event
    public int meetingID;
}

@Entity
public class Meeting {
    @EmbeddedId
    public MeetingID id;

    @MapsId("eventID")
    @JoinColumns({
        @JoinColumn(name="EventID", referencedColumnName="EventID"),
        @JoinColumn(name="SourceID", referencedColumnName="SourceID")
    })
    @ManyToOne
    public Event event;
}

在JPA 2.1规范中讨论,2.4.1。

Discussed in JPA 2.1 spec, section 2.4.1.

这篇关于JPA如何制作复合主键的复合外键部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆