Aldeguer Aldeguer -4 years ago 65
Java Question

HQL query take attribute from wrong class

I'm trying to execute the following query :

returnValue = (Long) super.createQuery("Select Count(*) From User u Left Outer Join u.rolesRelated r "
+ "Where r.role.id = :roleId And u.id = :userId")
.setParameter("userId", userId)
.setParameter("roleId", roleId)
.getSingleResult();


But I'm getting this error :

org.hibernate.QueryException: could not resolve property: role.id of: com.sp.common.core.model.User


This property does not exists since role.id is in user.rolesRelated, not in user. This is my hibernate mapping:

<class name="com.sp.common.core.model.User" table="user">
/* more meaningless properties */
<set cascade="all" name="rolesRelated" sort="unsorted" table="sec_rperfil_usuario"
lazy="false" fetch="join">
<key column="id_usuario" />
<composite-element class="com.sp.common.core.model.UserRole">
<many-to-one name="role" class="com.sp.common.core.model.Role"
column="id_perfil" cascade="none" lazy="false" fetch="join" />
</composite-element>
</set>
</class>


Does anyone know what is wrong here?

Thanks in advance

Answer Source

Try doing another explicit join on Role.

SELECT COUNT(*) FROM User u LEFT OUTER JOIN u.rolesRelated rr 
LEFT OUTER JOIN rr.role r WHERE r.id = :roleId And u.id = :userId
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download