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?
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With