-->
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: Getting lazy object identifier without fetching first.
PostPosted: Thu Feb 03, 2005 10:09 pm 
Newbie

Joined: Thu Jan 13, 2005 6:24 pm
Posts: 12
Simple question but can't seem to find answers anywhere. If I have a simple person object with a personType object associated one-to-one and using lazy fetching strategy. Is there a way I can retrieve the personType identifier without fetching the personType object first? Obviously hibernate has the identifier because it needs it to later initialize the lazy object, so I take it there should way of getting to it but can't seem to find how. Any help would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 04, 2005 3:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
obj.getId() .... accessing the id property of an ojbect doesnt initialize it.

if you have a session there should also be a getIdentifier(o) that does the same, but with a dependency to hibernate.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 04, 2005 11:30 am 
Newbie

Joined: Mon Dec 20, 2004 11:53 am
Posts: 8
Here's a link to the faq:

http://hibernate.org/118.html#A22

How can I retrieve the identifier of an associated object, without fetching the association?

Just do it! The following code does not result in any SELECT statement, even if the item association is lazy.

Long itemId = bid.getItem().getId();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 04, 2005 11:44 am 
Newbie

Joined: Thu Jan 13, 2005 6:24 pm
Posts: 12
I just realized I asked the wrong question. What I am interested is the opposite. If I create a new Person object, can I set the pesonType identifier if I already know it, without first having to query for a personType object and then doing setPersonType() ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 04, 2005 2:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
A oneliner ,)

new Person().setPersonType((PersonType)session.load(PersonType.class, personTypeId));


its sesson.load that does the magic....it does not hit the db, and return a proxy
session.get hits the db and return a solid object.

_________________
Max
Don't forget to rate


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.