Hi, I have a problem with my hibernate configuration. I can´t get an object using a many-to-one relationship.
I have two tables in my DB, with this mappings files:
Code:
<hibernate-mapping package="zonaprint.hibernate.mappings">
<class
name="Tbusuario"
table="tbusuario"
>
<id
name="Id"
type="integer"
column="idUser"
>
<generator class="native"/>
</id>
<set name="Tbcoleccions" inverse="true">
<key column="idUser"/>
<one-to-many class="Tbcoleccion"/>
</set>
</class>
</hibernate-mapping>
and
Code:
<hibernate-mapping package="zonaprint.hibernate.mappings">
<class
name="Tbcoleccion"
table="tbcoleccion"
>
<id
name="Id"
type="integer"
column="idColeccion"
>
<generator class="native"/>
</id>
<property
name="Nombre"
column="nombre"
type="string"
not-null="true"
length="100"
/>
<many-to-one
name="Usuario"
column="usuario"
class="Tbusuario"
not-null="true"
>
</many-to-one>
</class>
</hibernate-mapping>
The problem appears when I try to get the object Tbusuario from an instance of a Tbcoleccion. The code is:
Code:
Tbcoleccion coleccion = new Tbcoleccion();
session.load(coleccion,new Integer(2));
Tbusuario user = coleccion.getUsuario();
This code doesn´t throws any exception, but it fails because the object Tbusuario it returns has all its fields null. Also I have seen that the object that returns is Tbusuario$$EnhancerByCGLIB$$.
While i was writing this lines I encountered a solution.
This is one way to solve this problem, It is adding lazy="false" in every many-to-one relationship at hibernate mapping files. I don´t know if its an hibernate bug (my version is 3.1.3).
I hope this will help someone who could have the same problem I had.
[/code]