Search code examples
javahibernatedb2hibernate-annotationshibernate-criteria

Hibernate create alias on many to many list


I have four class; UserGroup, UserAccount, Role, UserGroupRoleRelation and my db is IBM DB2

@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<UserGroupRoleRelation> userAccountsRole = new ArrayList<UserGroupRoleRelation>();


}




@Entity
@Table(name = "ROLE")
public class Role implements Serializable {

    @Id
    @Column(name = "ROLE_ID")
    @GeneratedValue
    private Long id;

    ......

    @OneToMany(mappedBy = "role")
    private List<UserGroupRoleRelation> userAccountInGroup = new ArrayList<UserGroupRoleRelation>();


}


@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<UserAccount> userAccountList = new ArrayList<UserAccount>();

    @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<UserGroupRoleRelation> rolesInGroup = new ArrayList<UserGroupRoleRelation>();
}

I wanna find usergroups of a useraccount and i prepared a method with criteria. its like;

    @Override
    @Transactional
    public List<UserGroup> findUserGroupOf(UserAccount userAccount) {
        Criteria criteria = getSession().createCriteria(UserGroup.class);
        criteria.createAlias("userAccountsRole", "userAccountsRole");
        criteria.add(Restrictions.eq("userAccountsRole.userAccountList", userAccount));
        return criteria.list();

    }

But when i try to get result of that method, DB2 gives to me DB2 SQL Error: SQLCODE=-313, SQLSTATE=07004, SQLERRMC=null, DRIVER=3.63.75

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?

Thank


Solution

  •     @Override
        @Transactional
        public List<UserGroup> findUserGroupOf(UserAccount userAccount) {
            Criteria criteria = getSession().createCriteria(UserGroup.class);
            criteria.createAlias("userAccountsRole", "userAccountsRole");
            criteria.createAlias("userAccountsRole.userAccountList", "userAccountList");
            criteria.add(Restrictions.eq("userAccountList.id", userAccount.getId()));
            return criteria.list();
    
        }
    

    It works for me. I mean criteria on "id". But I don't understand why I cant check equality on object instead of id when there is ManyToMany list