-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-one lazy relationship in hibernate
PostPosted: Fri Nov 03, 2006 8:33 pm 
Newbie

Joined: Sun Oct 15, 2006 11:20 pm
Posts: 13
Hi,

I have a mapping of 'Agent' like this:

Code:
<class name="com.mycompany.model.Agent"
      table="AGENTS" lazy="true">

      <id name="id" column="AGENT_ID">
         <generator class="native" />
      </id>

      <version name="version" column="version"
         type="java.lang.Integer" />

      <many-to-one name="phone" column="PHONE_ID"
         class="com.mycompany.model.Phone" />

   </class>


I have hibernate sql send to stdout so that I know what is the sql.
// see a sql statement here from hibernate
Agent agent = agentManager.getAgentByName('aName');
// do not see a sql statement here.
Phone phone = agent.getPhone();
[/code]

My question is why I don't see any sql statement when I do 'agent.getPhone()' the relationship is 'lazy', so I expect hibernate will query that only when I do agent.getPhone(), so I don't see that happening.

Can you please help me understand why?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 04, 2006 4:36 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Try doing a phone.getSomething(). thats the time when Hibernate will actually run the query. When you do a agent.getPhone(), all you are returned is the proxy object which was created when the Agent object was loaded. Only on loading a specific property of Phone will the actual SQL be run.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 04, 2006 2:41 pm 
Newbie

Joined: Sun Oct 15, 2006 11:20 pm
Posts: 13
Thanks.

What if I call phone.getId() in another query? Will the phone.getId() generate another query)?


Code:
   Query query = session
            .createQuery(
                  "from AREA as area where  area.country= :country and area.phone.id = :phoneid");
      
      
      List result = query.setString("country", country).setLong("phoneid",
                  phone.getId().longValue()).list();



and do you know which query is better (efficiency wise).

This:
Code:
List result = query.setString("country", country).setLong("phoneid",
                  phone.getId().longValue()).list();


or this:
Code:
List result = query.setString("country", country).setEntity("phone", phone)).list();



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