I have some problems trying to get a bag collection of a class to be loaded through custom sql.
Here’s the xml mappings I have for my class
<hibernate-mapping>
<class name="alekso.npe.model.Utente" table="VNPEZZ_UTE_MAT" lazy="false">
<id name="matricola" column="C_UTE_MAT">
</id>
<property name="nome" column="T_NOM" />
<property name="cognome" column="T_COG" />
<property name="email" column="T_EML" />
<bag name="ruoli" table="VNPEZX_UTE_APP_TRN"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key column="C_UTE_MAT" />
<one-to-many class="alekso.npe.model.Ruolo" />
<loader query-ref="rolesQuery"/>
</bag>
</class>
<class name="alekso.npe.model.Ruolo" table="VNPEZH_TIP_USR"
lazy="false" where="C_APP = 'NPE'">
<id name="codice" column="C_TIP_USR">
</id>
<property name="nome" column="T_DES_TIP_USR"/>
</class>
<sql-query name="rolesQuery">
<return alias="role" class="alekso.npe.model.Ruolo"></return>
<load-collection alias="ruoli" role="alekso.npe.model.Ruolo" ></load-collection>
<![CDATA[select {ruolo.*}
from NPEA.vnpezx_ute_app_trn permesso join NPEA.vnpezh_tip_usr ruolo
on ruolo.c_tip_usr = permesso.c_tip_usr
where permesso.c_app = 'NPE'
and permesso.c_ute_mat = :matricola ]]>
</sql-query>
</hibernate-mapping>
But when I run the application I get an error:
org.hibernate.HibernateException: Errors in named queries: rolesQuery in the buildSessionFactory phase.
Can you tell me what’s wrong with this mapping?
I tried both with and without the <return> tag inside the <sql-query> but it still don’t work
I found out the error… the line
<load-collection alias="ruoli" role="alekso.npe.model.Ruolo" ></load-collection>inside the custom query definition gives the error.Without that line, everything works fine (almost… the collection of roles is lazy loaded even if I set lazy=”false” everywhere).
But that line I removed is on the documentation, so I’d like to know why it’s wrong.
I’ll leave the question open for other to give some hints on this.