File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Bidirectional OneToMany Mapping with double Association Using @JoinTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Bidirectional OneToMany Mapping with double Association Using @JoinTable" Watch "Bidirectional OneToMany Mapping with double Association Using @JoinTable" New topic

Bidirectional OneToMany Mapping with double Association Using @JoinTable

ashirvad jain
Ranch Hand

Joined: Nov 09, 2006
Posts: 50
Hi All,

For one of my project, I am using Bidirectional OneToMany Mapping for two entities. I want to maintain User as Speaker & Participants.

1) Two Entities , User and Event,

2) I am using two association relation between these two entities as below,

User [ One- Many] ---->[ Speaker] ----> Event
[ManyToOne]<----[Speaker] <-----
Event [One- Many] ---->[ Participant] ----> User
[ManyToOne]<----[Participant] <-----

Class User{

private Lond Id;

public Collection<Event> getEvents() {
return events;

joinColumns={@JoinColumn(name="USER_ID", insertable=false,updatable=false)},
inverseJoinColumns={@JoinColumn(name="EVENT_ID", insertable=false,updatable=false)})

public Event getEvent() {
return event;

2) Class Event{

private Lond Id;

joinColumns={@JoinColumn(name="EVENT_ID", insertable=false,updatable=false)},
inverseJoinColumns={@JoinColumn(name="USER_ID", insertable=false,updatable=false)})

public User getUsers() {
return user;

public Collection<User> getUsers() {
return users;


1) I am getting success, when I create any user or event, it successfully updates the table(users,events,Speaker,Participants), But I am facing issues, when I am creating new Event entity
with the users which is already available in the previously created Event. it throws, exceptions.MySQLIntegrityConstraintViolationException, while creating new event.
How I can prevent this situation?

2) Is this design is good, Should I refactored it ? I am using User Entity as Speaker and participant, and thus all creating issues.

Please assist me resolve my issue, if need more details, then I can provide you.

James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Include the SQL log.

Your usage of the join table is pretty complicated. You could probably simply things by instead mapping an Entity to the join table.
You could also map it instead as a bi-directional ManyToMany, and just enforce the ManyToOne is your get/set methods.

What JPA provider are you using? If you are using EclipseLink, you may be able to use a DescriptorCustomizer to make the ManyToOne readOnly.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
I agree. Here's the link:
subject: Bidirectional OneToMany Mapping with double Association Using @JoinTable
jQuery in Action, 3rd edition