Hi,
I have a problem with a bidirectional one-to-many relationship.
I have an Artist (can be a Band) wich can consist many Musicians.
Here is a Member (parent class of Artist):
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class MemberData {
private long memberId;
public MemberData() {
}
@Id
@Column(name="memberId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Here is the Artist extending a Member:
Code:
@Entity
@Table (name="ArtistData")
public class ArtistData extends MemberData {
private List<MusicianData> members = new ArrayList<MusicianData>();
public ArtistData() {
super();
}
@OneToMany (cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn (name="memberId")
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@org.hibernate.annotations.IndexColumn(name = "musicianId")
public List<MusicianData> getMembers() {
return members;
}
public void setMembers(List<MusicianData> members) {
this.members = members;
}
}
Here is the Musician:
Code:
@Entity
@Table(name="MusicianData")
public class MusicianData {
private long musicianId;
private ArtistData artist;
public MusicianData() {
}
@Id
@Column (name="musicianId")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@ManyToOne (fetch = FetchType.EAGER)
@JoinColumn (name="memberId", nullable=true, insertable=false, updatable=false)
public ArtistData getArtist() {
return artist;
}
public void setArtist(ArtistData artist) {
this.artist = artist;
}
}
My problem is that I don't want to use autogenerated IDs for either the MemberData or the MusicianData. So I don't have the @GeneratedValue Annotation included and set the IDs manually before making an Object persistent.
It works fine with MemberData. I save an Artist who extends MemberData to the database with
Code:
session.save(artist);
It is written to the database with my manually generated ID then.
But when I add a List of Musicians to the Artist and then do one of the following
Code:
session.merge(artist);
or
Code:
session.update(artist);
or
Code:
session.saveOrUpdate(artist);
the Musicians are written correctly to their databasetable EXCEPT that their IDs are values from 0...numberofmusicians instead of my manually generated IDs.
The next time I want to add Musicians I get a ConstraintViolationException with the message "Could not execute JDBC batch update" caused by a BatchUpdateException with the message "Column 'musicianId' cannot be null".
So why does Hibernate not take the values in musicianId like it does with the values in memberId? The musicianIds are definitely not null when calling merge/update!
The database tables of MemberData and MusicianData are completely equal regarding the primary key settings.
Any help would be great!!!
Thanks,
Stefan