Home Hibernate ManyToMany with Join table problems on update
Reply: 0

Hibernate ManyToMany with Join table problems on update

user1756 Published in March 22, 2018, 9:41 pm

I am trying to make a ManyToMany association work for all CRUD operations
I have to entities : Places and Events Places can hold multiple events and an events can take place in multiple places

In first case I had

In class PlaceDto

cascade = { CascadeType.PERSIST, CascadeType.MERGE })
joinColumns = { @JoinColumn(name = "PLACE_ID") },
inverseJoinColumns = { @JoinColumn(name = "EVENT_ID") })
private List events;

In class PlaceDto

@JoinTable(name = "EVENTS_PLACES", joinColumns = @JoinColumn(name = "EVENT_ID"), inverseJoinColumns = @JoinColumn(name = "PLACE_ID")) private List places;

in this case on updating a place the link between the place and its event was erased
with DELETE FROM EVENTS_PLACES where ... statement

Second case
So after reading some docs, I changed PlaceDto to

@ManyToMany ( mappedBy = "events",
cascade = { CascadeType.PERSIST, CascadeType.MERGE },
fetch = FetchType.LAZY,
targetEntity = FundDto.class)
private List places;

when updating a place everything seems fine but when I try to create an event it tries also to create a place
=> which leads to a primary key violation

I have hashcode and equals overidden using ecplise ....

Thanks for your help

Please to not hesitate to point me to a page where a fully working and tested ManyToMany relation is showned and explained


You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299075 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO