views:

761

answers:

1

Dear all,

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

A: 

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.

Gregory Mostizky