-->
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: ManyToMany, bidirectional (not work as bidirectional).
PostPosted: Tue May 15, 2007 3:56 am 
Newbie

Joined: Sun Dec 17, 2006 9:28 pm
Posts: 3
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 !!!



Top
 Profile  
 
 Post subject: Problem solved.
PostPosted: Tue May 15, 2007 10:33 am 
Newbie

Joined: Sun Dec 17, 2006 9:28 pm
Posts: 3
I am sorry to trouble you. All answer are valued by credit points. Problem was linked to transaction propagation.


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.