-->
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.  [ 2 posts ] 
Author Message
 Post subject: Bidirectionnal relation lost when persisted
PostPosted: Mon Dec 03, 2007 9:05 am 
Beginner
Beginner

Joined: Sun Jul 31, 2005 6:15 pm
Posts: 28
Hibernate version: 3.2.1 ga

Mapping documents:

Code:
@Entity
@Table(name = "user")
public class User implements java.io.Serializable {

......
   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
   @Cascade({ALL})
   public Set<Right> getRights() {
      return this.rights;
   }

   public void addRight(Right right){
      right.setUser(this);
      rights.add(right);
   }
.....
}


Code:
@Entity
@Table(name = "rights")
public class Right implements java.io.Serializable {
........
   @ManyToOne(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
   @JoinColumn(name = "id_user")
   public User getUser() {
      return this.user;
   }
........
}


Code between sessionFactory.openSession() and session.close():

Code:
user = new User(login,password);
user.addRight(getBasicRight());
session.save(user);


Name and version of the database : MYSQL 5.0.41-community-nt

The generated SQL (show_sql=true):

Code:
03-12-2007 13:46:55:234 3063 DEBUG org.hibernate.SQL  - insert into user (login_user, password_user, id_user) values (?, ?, ?)
03-12-2007 13:46:55:281 3110 DEBUG org.hibernate.SQL  - insert into rights (id_user, label, id) values (?, ?, ?)




Problem :

The two object User and Right are persisted, but the column id_user in the rights table is always equal to 0 as the relation between user and right was ignored.


What did I forget ?

Thanks a lot.

_________________
Michael Courcy


Top
 Profile  
 
 Post subject: I answer to myself
PostPosted: Mon Dec 03, 2007 5:38 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 6:15 pm
Posts: 28
That was a very nice one : I forgot to use the @GeneratedValue annotation with the @Id annotation.

I become aware of this when I decide to let hibernate reconstruct the schema for me. It was impossible to insert more than one row because the id value always fall back to zero.

Sorry for the noise.

_________________
Michael Courcy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.