This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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
Author

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{

@Column(name="USER_ID")
private Lond Id;

@OneToMany
@JoinTable(name="Speaker",
JoinColumns={@JoinColumn(name="USER_ID")},
inverseJoinColumns={@JoinColumn(name="EVENT_ID")}
)
public Collection<Event> getEvents() {
return events;
}

@ManyToOne
@JoinTable(name="Participants",
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{

@Column(name="EVENT_ID")
private Lond Id;

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

public User getUsers() {
return user;
}

@OneToMany
@JoinTable(name="Participants",
JoinColumns={@JoinColumn(name="EVENT_ID")},
inverseJoinColumns={@JoinColumn(name="USER_ID")}
)
public Collection<User> getUsers() {
return users;
}
}
}

Questions:

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.

Thanks
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Bidirectional OneToMany Mapping with double Association Using @JoinTable
 
Similar Threads
CollectionOfElements
Help With Association Object Mapping
JPA: incomplete SQL query
Orphan delete doesn't seem to be working properly
problem on delete beacuse of mapping view as a collection