-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with many-to-one lazy=true
PostPosted: Sat Apr 01, 2006 2:04 pm 
Newbie

Joined: Wed Aug 18, 2004 8:44 am
Posts: 12
I think there is a bug on the many-to-one relationship when lazy is set to true.

Hibernate version: 3.1.3

Mapping documents:
Code:
<hibernate-mapping package="br.ufal.tci.arco.core" default-access="field">

    <class name="Community" table="community">
        <id name="uid" length="64"/>

        <property name="state" type="character"/>
        <property name="creationDate" type="java.util.Date"/>
        <property name="name" type="string" unique="true" not-null="true"/>
        <property name="description" type="string" not-null="true"/>
        <property name="picturePath" type="string"/>
        <property name="pictureName" type="string"/>

        <many-to-one name="communityParent" column="parentId" class="Community"/>

        <many-to-one name="creator" column="actorId" class="Actor"/>
       
        <many-to-one name="category" column="categoryId" class="Category"/>

</hibernate-mapping>

Name and version of the database you are using:
mysql 4.1

The class Community extends an abstract class AbstractPersistentable that has the uid, state and creationDate attribute. The uid is generated on the constructor of the object.

The problem is on the many-to-one relationship. All the objects and collections comes with its correct UID. But when the object on the many-to-one relation is initialized the UID is not set. This means the object has a different id from the one in the database. If the many-to-one relation is lazy=false the id is set correctly. And notice the default-access=field. All other attributes is set correctly, but the uid stays the one generated in the constructor. A different one every time.

I think this is a bug from hibernate. Any clues?? Theres nothing wrong with the classes. I have at least three classes with this kind of relation and all many-to-one relation when initialized keeps the wrong id.

THANKS!

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 02, 2006 11:42 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Sounds like you're using member fields, rather than accessors. All uses of uid, even within the Community class, must go via accessors. The only methods allowed refer to the member fied are the accessor and mutator. This applies to all members of all mapped classes (assuming that you're using the property proxying method, rather than field). hashCode and equals are the methods that most frequently fall victim to this mistake.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 1:58 pm 
Newbie

Joined: Wed Aug 18, 2004 8:44 am
Posts: 12
I'm sorry, but I don't think I undestand what you said. This means I should never use field access?! Hibernate's documentation doesn't mention any problemns with that kind of access. And I dont have any problemns as I said in my post. Just with the many-to-one relationship and only when it's lazy=true.

So, can you explain better what's the problemn??

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 5:59 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yes, if you're using the normal access level (property), you should never use field access except in the getter and setter methods. This includes inside the equals() and hashCode() methods. Hibernate's docs mention this in a roundabout way, and I think they should make it clearer, but there you go. Here's one of way it's mentioned.

Section 19.1, "Fetching Strategies":
refdocs wrote:
Proxy fetching - a single-valued association is fetched when a method other than the identifier getter is invoked upon the associated object.

This is the default for access="property", the default.

refdocs wrote:
Lazy attribute fetching - an attribute or single valued association is fetched when the instance variable is accessed. This approach requires buildtime bytecode instrumentation and is rarely necessary.

This is the default for access="field", which presumably you have not turned on.

I think that the documentation for access and default-access should describe their effects on user code as well as on hibernate code, because it does not emphasize the potential pitfalls enough.. imo.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 6:18 pm 
Newbie

Joined: Wed Aug 18, 2004 8:44 am
Posts: 12
Thank you very much!!
Now it's clearer hehe


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.