Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA how to make composite Foreign Key part of composite Primary Key

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 } 

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

like image 435
suraj bahl Avatar asked Jul 13 '15 14:07

suraj bahl


1 Answers

@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; } 

Discussed in JPA 2.1 spec, section 2.4.1.

like image 173
Brian Vosburgh Avatar answered Sep 17 '22 13:09

Brian Vosburgh