-->
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: problem with FetchMode=LAZY and many-to-one association
PostPosted: Mon Mar 14, 2005 1:50 pm 
Newbie

Joined: Tue Mar 01, 2005 3:20 pm
Posts: 6
Hi:

I have 2 tables: user and agent. User table has agentId as a foreign key.
The association is defined as follows:
Code:
<hibernate-mapping package="com.acme.impl">
    <class name="UserImpl" table="APPUSER">
      <id name="userId" column="APPUSERID" unsaved-value="null" type="integer">
         <generator class="identity"/>
      </id>

       <property name="login" column="userName" type="string"/>
       <property name="lastName" column="lastName" type="string"/>
       <property name="firstName" column="firstName" type="string"/>
       <property name="lastLoggedOn" column="lastlogin" type="timestamp"/>
       <property name="agentId" column="agentId" type="integer"/>
       
       <many-to-one
           name="agent"
           class="AgentImpl"
           column="AGENTID"
           cascade="all"
           unique="true"
    </class>
</hibernate-mapping>
.

When a select is issued against the user table, I don't want a select issued against agent table, unless I specifically call getAgent(). To my understanding the following code should do the trick:

Code:
  public UserImpl getUser(final Integer userId) throws Exception
    {
        HibernateTemplate template = new HibernateTemplate(getSessionFactory());
        return (UserImpl) template.execute(new HibernateCallback()
        {
            public Object doInHibernate(Session session) throws HibernateException
            {
                return (UserImpl) session.createCriteria(UserImpl.class)
                         .add(Expression.eq("userId", userId))
                         [b].setFetchMode("agent", FetchMode.LAZY)[/b]
                         .uniqueResult();
            }
        }, false);
       
    }
.

But for each call to the user table a call to the agent table is also made.

What am I doing wrong?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 2:49 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
What version of hibernate are you using?

You need to mark the associated class (agent) as lazy or provide a proxy for it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 2:55 pm 
Newbie

Joined: Tue Mar 01, 2005 3:20 pm
Posts: 6
Thanks for the reply.

The version is 2.1.8

I am new to hibernate, so I am reading a book (Hibernate in action). It recommends using FetchMode instead of lazy=true, so no proxies are used (page 236, chapter 6).


Top
 Profile  
 
 Post subject: problem with lazy loading
PostPosted: Thu Mar 17, 2005 8:06 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 9:24 am
Posts: 28
roskha wrote:
Thanks for the reply.

The version is 2.1.8

I am new to hibernate, so I am reading a book (Hibernate in action). It recommends using FetchMode instead of lazy=true, so no proxies are used (page 236, chapter 6).



I have exactly the same problem as you and I'm also reading the HIA book, same chapter as you.
They say "For a criteria query, you canexplicitely disable outer join fetching by calling setFetchMode("bids",FetchMode.LAZY)"
They do never ever mention to use proxy .

That's what I'm doing and it's not working at all.

So if someone can tells what't to be done to settle lazy fetching. It would be great.

Meissa


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 8:26 am 
Newbie

Joined: Wed Mar 09, 2005 5:03 am
Posts: 7
Code:
<!-- Declares an association between two entities (Or from a component, component element,
etc. to an entity). -->

<!ELEMENT many-to-one (meta*,column*)>
  <!ATTLIST many-to-one name CDATA #REQUIRED>
  <!ATTLIST many-to-one access CDATA #IMPLIED>
  <!ATTLIST many-to-one class CDATA #IMPLIED>
  <!ATTLIST many-to-one column CDATA #IMPLIED>
  <!ATTLIST many-to-one not-null (true|false) "false">
  <!ATTLIST many-to-one unique (true|false) "false">
  <!ATTLIST many-to-one cascade (none|all|save-update|delete) #IMPLIED>    <!-- default: none -->
  <!ATTLIST many-to-one outer-join (true|false|auto) "auto">
  <!ATTLIST many-to-one update (true|false) "true">   <!-- only supported for many-to-one of a class (not component) -->
  <!ATTLIST many-to-one insert (true|false) "true">   <!-- only supported for many-to-one of a class (not component) -->
  <!ATTLIST many-to-one foreign-key CDATA #IMPLIED>
  <!ATTLIST many-to-one property-ref CDATA #IMPLIED>
  <!ATTLIST many-to-one index CDATA #IMPLIED>

Are you sure it is possible to use lazy initialization with many-to-one element?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 12:47 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 9:24 am
Posts: 28
Jacob wrote:
[code]
<!-- Declares an association between two entities (Or from a component, component element,
etc. to an entity). -->

Are you sure it is possible to use lazy initialization with many-to-one element?


Christian ?
Gavin ?

Won't you deliver us ?


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.