-->
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.  [ 9 posts ] 
Author Message
 Post subject: lazy loading issue
PostPosted: Wed Nov 01, 2006 6:11 am 
Newbie

Joined: Wed Nov 01, 2006 5:55 am
Posts: 3
Hibernate version:3.0
JBoss version :4.0.3
I have two classes User and CustomerInfo and their corresponding hbm.xmls
and their is one to one mapping in between these two relations as


user.hbm.xml
<hibernate-mapping>
..
..
<!-- bi-directional one-to-one association to CustomerInfo -->
<one-to-one
name="CustomerInfo"
property-ref="user"
cascade="all"
lazy="no-proxy"
/>

</hibernate-mapping>

and the file
customerinfo.hbm.xml
<hibernate-mapping>
..
..
<!-- bi-directional one-to-one association to User -->

<many-to-one
name="user"
column="user_id"
unique="true"
not-null="true"
/>
</hibernate-mapping>


When i fire query on table user the select query for customerinfo is also gets fired(no lazy loading).So can any one help me so that the select query for customerInfo should not get fired


In short how to achieve lazy laoding in case of one to one mapping
<one-to-one
lazy="false|no-proxy|proxy"
/>
nothing works ! Why hibernate people removed lazy=true in case of one-to-one mapping !

Thnx and regards
Amit Deshpande


Top
 Profile  
 
 Post subject: specify the fetch mode
PostPosted: Wed Nov 01, 2006 2:32 pm 
Newbie

Joined: Fri Oct 27, 2006 6:53 pm
Posts: 3
In addition to making lazy = proxy, you need to specify the fetch mode for this association. You can either use fetch="join" on the <one-to-one> mapping or you can set the fetch mode in your queries that return users (e.g., criteria.setFetchMode("customerInfo", FetchMode.JOIN) ).

_________________
jim buswell


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 10:28 am 
Newbie

Joined: Wed Nov 01, 2006 5:55 am
Posts: 3
thnx for your reply

fetch = "join|select"
when one specifies fetch = "join" Hibernate retrieves the associated instance or collection in the same SELECT, using an OUTER JOIN.

And one more thing to add
when i really tried it in my application it seems that it is not working means the select query for other table(customer info ) get's fired also.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 05, 2006 7:37 am 
Newbie

Joined: Tue Apr 18, 2006 11:05 am
Posts: 5
hi amit,
i guess you probaly want to have lazy loading for one-to-one mapping in case of hibernate. typically you have one-to-one-or-zero kind of mapping right ?
i have the same issue with me.
can any one help us in mapping one-to-one ( more precisely one-to-one-or-zero ) in hibernate?

Thanks
~pp


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 05, 2006 1:04 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Amit,

here's what you mappings should look like:

Code:
<!-- bi-directional one-to-one association to CustomerInfo -->
<one-to-one
  name="CustomerInfo"
  property-ref="user"
  cascade="all"
  lazy="proxy"
  constrained="true"
/>

<!-- bi-directional one-to-one association to User -->

<many-to-one
   name="user"
   column="user_id"
   unique="true"
   not-null="true"
   lazy="proxy"
/>
</hibernate-mapping>


Note that on CustomerInfo, we have declared lazy="proxy" (which is equivilent to the old lazy="true") and constrained="true". The attribute lay="no-proxy" only works if you use bytecode instrumentation. If you don't know what I mean, then you're probably not using it. The constrained attribute is also important as if it is set to false, proxying is impossible, and this the association is always eagerly loaded.

Although jbuswell suggested using fetch="join", this will not solve your problem. When you use fect="join", you effectively disable lazy-loading as the entities are loading 2 objects in one SQL call. You should use the default fetch mode of select.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 1:32 am 
Newbie

Joined: Tue Apr 18, 2006 11:05 am
Posts: 5
Quote:
<!-- bi-directional one-to-one association to CustomerInfo -->
<one-to-one
name="CustomerInfo"
property-ref="user"
cascade="all"
lazy="proxy"
constrained="true"
/>

<!-- bi-directional one-to-one association to User -->

<many-to-one
name="user"
column="user_id"
unique="true"
not-null="true"
lazy="proxy"
/>
</hibernate-mapping>


hi damnhandy ,
the mappping you suggested above did not work.
BTW
from http://www.hibernate.org/162.html ... i have used this mapping

<many-to-one name="CustomerInfo"
class="CustomerInfo"
column="user_id"
unique="true"
insert="false"
update="false"
cascade="all"/>

and
<one-to-one
name="user"
class="User"
constrained="true"/>

which worked !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 2:23 pm 
Newbie

Joined: Wed Nov 08, 2006 11:14 am
Posts: 2
hi everyone!

I have similar problem with lazy loading. I have two tables with mapping like this:

Code:
<class name="User" table="User">
        <id name="id" column="id">
            <generator class="sequence">
                <param name="sequence">tp_seq</param>
            </generator>
        </id>

        <properties name="uniqueProps">
            <property name="firstName" column="firstName" type="string"/>
            <property name="lastName" column="lastName" type="string"/>
            <property name="middleName" column="middleName" type="string"/>
        </properties>
...
</class>

<class name="Customer" table="Customer">
        <id name="id" column="id">
            <generator class="sequence">
                <param name="sequence">tp_seq</param>
            </generator>
        </id>

            <property name="firstName" column="firstName" type="string"/>
            <property name="lastName" column="lastName" type="string"/>
            <property name="middleName" column="middleName"

<many-to-one name="user" class="User" property-ref="uniqueProps">
            <column name="firstName"/>
            <column name="lastName"/>
            <formula>Igor</formula>
</many-to-one>
...
</class>

So, I want select customer with lazy loading of appropriate user and select user with lazy loading of appropriate customer. It's doesn't work in this case. I think the problem is that the relation between these classes not uses primary key, and I have to use "property-ref" attribute.
Does anyone can help me?!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 5:04 am 
Newbie

Joined: Wed Nov 01, 2006 5:55 am
Posts: 3
This is a different issue.U want lazy loading if i am not wrong.

I think this will not work because you have to specify that primary key of User in Customer's hbm.xml file
Try to keep that id of User table in Customer table so that u will get records from user table when you are fetching records for Customer.


thnx and regards
Amit Deshpande


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 5:36 am 
Newbie

Joined: Wed Nov 08, 2006 11:14 am
Posts: 2
First af all, thanks a lot for reply.
My DB has been already designed before me, so I can't change it. The tables I use in this case relate to each other through fields that are not a primary key and a foreign key, they are just unique.
The question is, if I'm not use primary key in a relation, can I have lazy loading?


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