title: Hibernate create alias on many to many list
body: <p>I have four class; UserGroup, UserAccount, Role, UserGroupRoleRelation and my db is IBM DB2</p>
  
  <pre><code>@Entity
  @Table(name = "USER_GROUP")
  public class UserGroup implements Serializable {
  
      @Id
      @Column(name = "USER_GROUP_ID")
      @GeneratedValue
      private Long id;
  ......
  ..
      @OneToMany(mappedBy = "userGroup", cascade = CascadeType.ALL, orphanRemoval = true)
      private List&lt;UserGroupRoleRelation&gt; userAccountsRole = new ArrayList&lt;UserGroupRoleRelation&gt;();
  
  
  }
  
  
  
  
  @Entity
  @Table(name = "ROLE")
  public class Role implements Serializable {
  
      @Id
      @Column(name = "ROLE_ID")
      @GeneratedValue
      private Long id;
  
      ......
  
      @OneToMany(mappedBy = "role")
      private List&lt;UserGroupRoleRelation&gt; userAccountInGroup = new ArrayList&lt;UserGroupRoleRelation&gt;();
  
  
  }
  
  
  @Entity
  @Table(name = "USER_GROUP_ROLE_LINE", uniqueConstraints = @UniqueConstraint(columnNames = { "ROLE_ID", "USER_GROUP_ID" }))
  public class UserGroupRoleRelation {
  
      @Id
      @GeneratedValue
      @Column(name = "RELATION_ID")
      private Long relationId;
  
      @ManyToMany(cascade = CascadeType.ALL)
      @JoinTable(name = "USER_ACCOUNT_USER_GROUP_ROLE_LINE", joinColumns = { @JoinColumn(name = "RELATION_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") }, uniqueConstraints = @UniqueConstraint(columnNames = { "USER_ID", "RELATION_ID" }))
      private List&lt;UserAccount&gt; userAccountList = new ArrayList&lt;UserAccount&gt;();
  
      @ManyToOne
      @JoinColumn(name = "USER_GROUP_ID")
      private UserGroup userGroup;
  
      @ManyToOne
      @JoinColumn(name = "ROLE_ID")
      private Role role;
  }
  
  
  @Entity
  @Table(name = "USER_ACCOUNT")
  public class UserAccount implements Serializable {
  
      @Id
      @Column(name = "USER_ID")
      @GeneratedValue
      private Long id;
  .....
  
      @ManyToMany(mappedBy = "userAccountList", cascade = CascadeType.ALL)
      private List&lt;UserGroupRoleRelation&gt; rolesInGroup = new ArrayList&lt;UserGroupRoleRelation&gt;();
  }
  </code></pre>
  
  <p>I wanna find usergroups of a useraccount and i prepared a method with criteria. its like;</p>
  
  <pre><code>    @Override
      @Transactional
      public List&lt;UserGroup&gt; findUserGroupOf(UserAccount userAccount) {
          Criteria criteria = getSession().createCriteria(UserGroup.class);
          criteria.createAlias("userAccountsRole", "userAccountsRole");
          criteria.add(Restrictions.eq("userAccountsRole.userAccountList", userAccount));
          return criteria.list();
  
      }
  </code></pre>
  
  <p>But when i try to get result of that method, DB2 gives to me <code>DB2 SQL Error: SQLCODE=-313, SQLSTATE=07004, SQLERRMC=null, DRIVER=3.63.75</code></p>
  
  <p>Probably its about creating alias on many to many relation. I dont know what should i do to create alias on many to many. How can I get result of that function?</p>
  
  <p>Thank</p>