title: Retrieve data from many-to-many relationship - Hibernate
body: <p>I have a 'User' class:</p>
  
  <pre><code>public class User implements Serializable { 
  @Id 
  @GeneratedValue
  int id;
  
  String nome;
  
  @Column(unique = true)
  String email;
  
  @ManyToMany
  @JoinTable (name = "user_roles", joinColumns=
      { @JoinColumn (name = "user_id")}, inverseJoinColumns=
      { @JoinColumn (name = "role_id")})
  private List&lt;Role&gt; roles;
  </code></pre>
  
  <p>I have a 'Role' class:</p>
  
  <pre><code>public class Role implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Id 
  @GeneratedValue
  int id;
  
  @Column(unique = true)
  String role;
  </code></pre>
  
  <p><strong>Relationship</strong> :An User has roles, and roles has users. I have a many-to-many relationship here. The relation is unidirectional, and my dominant entity is User.</p>
  
  <p>In my database has a table named "user_roles", created automatically. I have a User registered with two roles. </p>
  
  <p>When I retrieve the User from my database using a "UserDao" class, and I try to access the roles of the user, I get NullPointerException. I got all others informations (name, email, etc), but the roles I can not access.</p>
  
  <p>My 'UserDao' class:</p>
  
  <pre><code>public class UserDaoImpl implements UserDao{
  
  private static final transient Logger log = LoggerFactory.getLogger(UserDaoImpl.class);
  private final String HIBERNATE_CFG = "data.cfg.xml";
      @Override
      public int insertUser (User user){
      int code = 0;
  
      Session session = new HibernateUtil(HIBERNATE_CFG).getSessionFactory().getCurrentSession();
      try {
          session.beginTransaction();
          session.save(user);
          session.getTransaction().commit();
          code = 1;
      }
      catch (Exception e) {
          log.error(e.getMessage());
          session.getTransaction().rollback();
          code = 0;
      }
  
      return code;
  } 
  
  @Override
  public List&lt;User&gt; getAll() {
      Session session = new HibernateUtil(HIBERNATE_CFG).getSessionFactory().getCurrentSession();
      List&lt;User&gt; users = null;
  
      try
      {
          session.beginTransaction();
          String queryString = "SELECT * FROM USER;";
          SQLQuery consulta = session.createSQLQuery(queryString);
          consulta.setResultTransformer(Transformers.aliasToBean(User.class));
          users = (List&lt;User&gt;) consulta.list();
          session.getTransaction().commit();
  
      }
      catch(ConstraintViolationException e)
      {
          log.error(e.getMessage());
          session.getTransaction().rollback();
      }
      return users;
  }
  </code></pre>
  
  <p>I tried using iterator:</p>
  
  <pre><code>List roles = user.getRegras();
      for (Iterator it = roles.iterator(); it.hasNext();){
          Role r = (Role) it.next();
          out.println("Role:" + r.getRole());
      }
  </code></pre>
  
  <p>I tried to instantiate the variable "roles" in the 'User' class like below. But every time the variable returns size 0:</p>
  
  <pre><code>@ManyToMany
  @JoinTable (name = "user_roles", joinColumns=
  { @JoinColumn (name = "user_id")}, inverseJoinColumns=
  { @JoinColumn (name = "role_id")})
  private List&lt;Role&gt; regras = new ArrayList&lt;Role&gt;();
  </code></pre>
  
  <p>I tried getting the roles from Object User, but I receive a NullPointerException when the method size() is called.</p>
  
  <pre><code>List&lt;Role&gt; roles = (List&lt;Role&gt;)user.getRoles();
  out.println("size roles: "  + roles.size());
  </code></pre>
  
  <p>I'm trying to acess my table 'user_roles' to retrieve the data from database, but I can't dothis. Can anyone help me?</p>
  
  <p>Thanks in Advance</p>