Hi,
I'm trying to realize a bidirectional one-to-many relationship but I'm always getting a org.hibernate.NonUniqueObjectException!
I want to have a band with several bandmembers:
Code:
public class Band {
@OneToMany (cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)//(mappedBy="artist")
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn (name="memberId")
@org.hibernate.annotations.IndexColumn(name = "musicianId")
private List<MusicianData> members = null;
}
public class BandMember {
@ManyToOne
@JoinColumn (name="memberId", nullable=false, insertable=false, updatable=false)
private Band band;
}
Then I get a Band from the database and close the session:
Code:
band = (band)session.get("Band", id);
In a new session I add a list of BandMembers and set their associated Band to the band:
Code:
for(int i = 0; i < bandMembers.size(); i++) {
band.getMembers().add(bandMembers.get(i));
bandMembers.get(i).setBand(band);
session.save(bandMembers.get(i));
}
session.flush();
Afterwards I close the session again.
The Bandmembers and the Band should now be persistent, shouldn't they?
In the database I cannot detect any changes, though.
Next I change other attributes of the Band, which should be in detached state after my understanding - there's no session open.
At last, I open again a new session and make an update for the Band:
Code:
session.update(band);
Here I get the following exception:
Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [BandMember#0]
But it would work fine if i made the onetomany-attributes transient.
Any help would be great!!!
thanks, Stefan