I have a fairly standard unidirectional many-to-many relationship between a user table and a role table. I am attempting to retrieve a specific user and the roles for that user.
Any ideas why the following hql query would return me a collection of 4 of same OpUser object? Note that this particular user has four roles in the db. I tried using "distinct" to no effect.
Is there a better way? I tried getting just the user with no join and doing Hibernate.initialize() but it only initialized the collection, not the contents (just as the docs state).
Code:
------
Code:
// get the list of OpUsers for given loginId (should only be 1)
Query q = session.createQuery("from OpUser as opuser " +
" join fetch opuser.roles as roles " +
" where opuser.loginId = :loginId");
q.setString("loginId", loginId);
users = q.list();
// if users found and there is only 1 (THIS FAILS)
if (users != null && users.size() == 1)
{
// get the user
foundUser = users.get(0);
//Hibernate.initialize(foundUser.getRoles());
}
Config:
----
using JBoss 4.0.2 and Hibernate (3.0.1 comes w/ this JBoss version I think)
Relevent mapping:
Code:
<class name="OpUser" table="op_user">
<id name="userId"
type="java.lang.Integer" column="user_id">
<generator class="assigned" />
</id>
<property name="loginId"
type="java.lang.String" column="login_id"
not-null="true" length="30" />
...
<set name="roles" table="user_role"
lazy="true" cascade="none">
<key> <column name="user_id" /> </key>
<many-to-many class="OpRole" column="role_id" />
</set>
</class>
<class name="OpRole" table="op_role">
<id name="roleId" type="java.lang.Integer"
column="role_id">
<generator class="sequence">
<param name="sequence">op_role_role_id_seq</param>
</generator>
</id>
...
</class>