These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: CGLIB enhanced parent getter methods always return null
PostPosted: Mon Jan 31, 2005 11:25 pm 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
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]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 31, 2005 11:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
<hibernate-mapping default-lazy="..."/>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 01, 2005 6:58 am 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
That's fine as a workaround but my question is why does this code not work when lazy=true?

Why does child.getParent().getXXX() always return null?

Surely the invocation of getXXX() accessor on the cglib enhanced object should cause a lazy load and a SQL select.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 01, 2005 1:36 pm 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
After hours of investigation I found the cause - the entity accessor methods in our model were declared final.

As I then found in the documentation: "you may not use a CGLIB proxy for a final class or a class with any final methods."
http://www.hibernate.org/hib_docs/reference/en/html/performance.html#performance-proxies

Something to bear in mind for those migrating to Hibernate 3.

Perhaps Hibenate could check for the presence of final modifiers when generating CGLIB proxies and throw an exception instead of silently returning nulls at runtime?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.