-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: mapping without autogenerated ids does not work
PostPosted: Fri Jan 11, 2008 8:56 am 
Newbie

Joined: Wed Oct 24, 2007 6:39 am
Posts: 7
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


Top
 Profile  
 
 Post subject: Re: mapping without autogenerated ids does not work
PostPosted: Fri Jan 11, 2008 11:01 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 9:24 am
Posts: 28
SilentSnake wrote:
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 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;
   }
}


Stefan



To make your relation bidirectional you have to set the "mappedBy" attribute in the @OneToMany annotation in order to define the field that own the relationship.
I think also that the @JoinColumn has nothing to do here.

Meissa


Top
 Profile  
 
 Post subject: Re: mapping without autogenerated ids does not work
PostPosted: Fri Jan 11, 2008 11:02 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 9:24 am
Posts: 28
SilentSnake wrote:
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 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;
   }
}


Stefan



To make your relation bidirectional you have to set the "mappedBy" attribute in the @OneToMany annotation in order to define the field that own the relationship.
I think also that the @JoinColumn has nothing to do here.

Meissa


Top
 Profile  
 
 Post subject: Re: mapping without autogenerated ids does not work
PostPosted: Fri Jan 11, 2008 11:03 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 9:24 am
Posts: 28
SilentSnake wrote:
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 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;
   }
}


Stefan



To make your relation bidirectional you have to set the "mappedBy" attribute in the @OneToMany annotation in order to define the field that own the relationship.
I think also that the @JoinColumn has nothing to do here.

Meissa


Top
 Profile  
 
 Post subject: Re: mapping without autogenerated ids does not work
PostPosted: Mon Jan 14, 2008 4:58 am 
Newbie

Joined: Wed Oct 24, 2007 6:39 am
Posts: 7
Quote:
To make your relation bidirectional you have to set the "mappedBy" attribute in the @OneToMany annotation in order to define the field that own the relationship.
I think also that the @JoinColumn has nothing to do here.

Meissa


Under http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html#d0e1136 it is described as I've done it to get a bidirectional relationship with the One-to-many side as the owning side. So this should work properly, shouldn't it?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.