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

Hibernate ManyToMany with Join table problems on update

Christ-OFF
1#
Christ-OFF Published in 2009-07-27 14:17:53Z

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

@ManyToOne(
targetEntity=EventDto.class,
cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(
name = "EVENTS_PLACES",
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

Christopher

Gregory Mostizky
2#
Gregory Mostizky Reply to 2009-07-27 14:33:50Z

Here is a nice example

Just try to copy what they have there into your own code and it should work. If it still doesn't, please post full listings for two classes and the code that causes the error and maybe I can see what the problem is.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO