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.