Some of our JUnit tests (which tested many-to-one navigation) failed when migrating to Hibernate 3. It turns out that lazy=true is now the default and adding the attribute lazy=false at class level caused the tests to succeed. But how do I get it working with lazy=true? Why does the trivial test class below fail in that case?
I can see in the debugger the CGLIB enhanced object which is returned from calling child.getParent(), but when a method is invoked on the returned parent the result is always null.
There must be something very elementary I am doing wrong here!
Hibernate version: 2.17 and 3b3
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
package="persistence.test"
default-cascade="save-update"
>
<class
name="ParentTest"
table="ParentTest"
lazy="true"
>
<id name="id"
column="id"
type="integer" >
<generator class="sequence">
<param name="sequence">id_gen</param>
</generator>
</id>
<property
name="someString"
type="string"
not-null="false"
/>
<set name="child1s"
lazy="true"
inverse="true"
order-by="parentTestId"
cascade="all-delete-orphan">
<key column="parentTestId" />
<one-to-many
class="persistence.test.Child1"
/>
</set>
</class>
<class
name="Child1"
table="Child1"
>
<id name="id"
column="id"
type="integer" >
<generator class="sequence">
<param name="sequence">id_gen</param>
</generator>
</id>
<many-to-one
name="parentTest"
column="parentTestId"
class="persistence.test.ParentTest"
cascade="none"
not-null="false"
unique="false" />
</class>
Code between sessionFactory.openSession() and session.close():
Code:
boolean auto = s.connection().getAutoCommit();
assertFalse( auto );
ParentTest p = new ParentTest();
p.setSomeString("foo");
Child1 c = new Child1();
c.setParentTest( p );
Transaction tx = s.beginTransaction();
s.saveOrUpdate( p );
s.saveOrUpdate( c );
tx.commit();
s.flush();
s.close();
s = factory.openSession();
s.load( c, c.getId() );
// FAILS!!
assertEquals( "foo", c.getParentTest().getSomeString() );
s.flush();
s.close();
Full stack trace of any exception that occurs:None
Name and version of the database you are using:postgres 7.4
[/code]