-->
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.  [ 6 posts ] 
Author Message
 Post subject: Bi-directional relationships and lazy initialization
PostPosted: Fri Oct 15, 2004 11:31 am 
Newbie

Joined: Tue Aug 26, 2003 12:31 pm
Posts: 14
Hibernate version: 2.1.6

I have a situation where I have a one-to-many bi-directional relationship like this (abbreviated):

Code:
A User class that has a lazy relationship to portlets:

<hibernate-mapping>
    <class
        name="com.matrix.portal.bo.user.Person"
        table="User"
        dynamic-update="false"
        dynamic-insert="false"
        discriminator-value="P"
    >

        <id
            name="id"
            column="User_ID"
            type="int"
            unsaved-value="0"
        >
            <generator class="native">
            </generator>
        </id>

        <set
            name="portlets"
            table="User_PFunc_Link"
            lazy="true"
            inverse="true"
            cascade="save-update"
            sort="unsorted"
        >
    </class>

</hibernate-mapping>

And a Portlet class that maps something like this:

<hibernate-mapping>
    <class
        name="com.matrix.portal.bo.PortletFunction"
        table="PortletFunction"
        dynamic-update="false"
        dynamic-insert="false"
        lazy="true"
    >
....
    </class>



When one side of the mapping is marked as lazy="true" then I can use Hibernate.initialize() to init the proxies. However, when I have both sides marked as lazy and I want to retrieve the collection from the parent object I have to do something like this:

Code:
// Init the collection
Hibernate.initialize(user.getPortlets());
user.getPortlets().size();
            
// Now init each element in the collection since it is marked as a proxy
Iterator iter = user.getPortlets().iterator();
while (iter.hasNext()) {
        PortletFunction element = (PortletFunction) iter.next();
        Hibernate.initialize(element);
}



Is this the best way to do this, or should it be avoided with a bi-directional relationship?

Regards,

Mark[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 11:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
check fetch keyword using hql

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 1:10 pm 
Newbie

Joined: Tue Aug 26, 2003 12:31 pm
Posts: 14
Anthony,

I figured that would be the response. I use the fetch keyword in HQL statements for this purpose. I was concerned with code that looked like my example above where I retrieve collections that have elements that are proxies. Thanks for verifying what I needed to know.

Thanks,

Mark


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 1:30 pm 
Newbie

Joined: Tue Aug 26, 2003 12:31 pm
Posts: 14
Anthony,

Actually, this code:

Code:
StringBuffer sb = new StringBuffer(50);
sb.append("from User user inner join fetch user.portlets
  where user.lastName = 'Test'");
User user = (User)session.find(sb.toString()).get(0);


produces the same problem as my code in the first posting. Is there something I can do to fully initialize both sides of the relationship with an HQL statement. To reiterate this will get the collection, but the elements in the collection are not initialized. How can I use HQL to initialize the elements in the collection?

Regards,

Mark


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 2:05 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
this should work.... trace sql and look if the collection table appears in it

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 2:12 pm 
Newbie

Joined: Tue Aug 26, 2003 12:31 pm
Posts: 14
Anthony,

Sorry for the confusoin. This does work. I did not have my environment restarted properly.

Thanks,

Mark


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