Hi I am trying to write a query and am struggling to get it to do what I want.. I know my association here isn't the greatest, but it has to do for now. Here is my mapping for this object:
Code:
<hibernate-mapping>
<class name="UserType" table="adminUserType">
<id name="id" type="long" unsaved-value="null">
<generator class="identity"/>
</id>
<property name="description"/>
<set name="roles" table="adminUserType_Role" lazy="true">
<key>
<column name="userType_id" not-null="true"/>
</key>
<many-to-many class="Role">
<column name="role_id" not-null="true"/>
</many-to-many>
</set>
<property name="name"/>
<property name="rootPath"/>
<set name="users" cascade="none" lazy="true" inverse="true">
<key column="userType_id"/>
<one-to-many class="User"/>
</set>
<set name="userTypesWithPermissionToAssign" table="adminUserType_UserType" lazy="true" sort="UserTypeNameComparator">
<key>
<column name="userType_id" not-null="true"/>
</key>
<many-to-many class="UserType">
<column name="userType2_id" not-null="true"/>
</many-to-many>
</set>
<property name="userClassName"/>
</class>
</hibernate-mapping>
So basically each UserType object contains a circular reference to a set of UserType's it has access to work with. The query I am trying to execute needs to select a subset of userTypesWithPermissionToAssign from a given userType id. Here is what I've tried so far to no avail:
Code:
Query userTypesQuery =
session.createQuery(
"select elements(ut.userTypesWithPermissionToAssign) from UserType ut " +
"left join fetch ut.userTypesWithPermissionToAssign userType where ut.id=:ID and userType.userClassName=:USERCLASSNAME");
userTypesQuery.setParameter("ID", loggedInUser.getUserType().getId());
userTypesQuery.setParameter("USERCLASSNAME", loggedInUser.getUserType().getUserClassName());
List userTypesList = userTypesQuery.list();
This appears to work except it is returning multiple objects of each of the actual usertypes it should return... I suppose I could just put it into a set and eliminate the repeates but I'd rather just have it return the correct amount. When I try to use left join fetch distinct user.userTypesWithPermissionToAssign i just get errors.
Thanks in advance!
-David