Ahmet DAL
Ahmet DAL

Reputation: 4682

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

Upvotes: 1

Views: 11001

Answers (2)

Ahmet DAL
Ahmet DAL

Reputation: 4682

    @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

Upvotes: 4

PVR
PVR

Reputation: 2524

It is not of creating alias. You are passing an object to hibernate on which it can not make any criteria. You need to create bidirectional mapping for that.Or else if you your requirement is just to fetch the the list of UserAccountList of particular UserGroup class you can follow the below code.

@Override
@Transactional
public List<UserGroup> findUserGroupOf(long userGroupId) {
    Criteria criteria = getSession().createCriteria(UserGroup.class);
    criteria.add(Restrictions.eq("id",userGroupId));
    criteria.createAlias("userAccountsRole", "uar");
    criteria.setFetchMode("uar.userAccountList",FetchMode.JOIN);
    return criteria.list();

}

Upvotes: 0

Related Questions