Hi!
Hibernate version: Hibernate Core 3.2.3 GA, Hibernate Annotations 3.3.0 GA; Spring 2.04; MySQL 5.037, InnoDB; jdk 1.5.0.11
Can't solve a problem myself, can't find answer in old topics. Bidirectional mapping does not work as one can expect.
Code:
@Entity
public class User extends BaseModel {
private static final long serialVersionUID = 1L;
...
private List<UserRole> userRoleList;
...
@ManyToMany(targetEntity = UserRole.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinTable(name = "USERS_ROLES", joinColumns = {@JoinColumn(name = "USER_ID")}, inverseJoinColumns = {@JoinColumn(name = "USER_ROLE_ID")}
public List<UserRole> getUserRoleList() {
return userRoleList;
}
public void setUserRoleList(List<UserRole> userRoleList) {
this.userRoleList = userRoleList;
}
....
}
// -----------------------------------------------------
@Entity
public class UserRole extends BaseStatus {
private static final long serialVersionUID = 1L;
private List<User> userList;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "userRoleList", targetEntity = User.class, fetch = FetchType.LAZY)
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
...
}
// -----------------------------------------------------
// In DAO
...
getHibernateTemplate().flush();
User user = getHibernateTemplate().get(User.class, 1L); // Works fine
List<UserRole> userRoleList = user.getUserRoleList() // Works fine, userRoleList != null
UserRole example = new UserRole();
example.setUserRole("USER_ROLE_ADMINISTRATOR");
UserRole userRole = (UserRole) getHibernateTemplate().findByExample(example, 0, 1).get(0); // Works fine.
// TROUBLES HERE !!!
List<User> adminList = userRole.getUserList(); // TROUBLE! adminList == null !!!