• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bidirectional OneToMany Mapping with double Association Using @JoinTable

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic