-->
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.  [ 4 posts ] 
Author Message
 Post subject: proxy getId => why sql is generated !
PostPosted: Thu Apr 16, 2009 4:31 am 
Newbie

Joined: Tue Apr 14, 2009 4:03 pm
Posts: 3
Hello,


I've a persistent object that reference other entity objects with manyToOne or oneToOne associations in LAZY fetch mode.
When I load this object (with entityManager.find for example), Hibernate create a proxy for each associated object. It's normal
But if I get the Id from an associated object, a sql staement is generated !
I thought hibernate didn't need sql statement to get Id from a proxy (I see in debug mode that the property id of the proxy is populated)
Does anyboby have an explanation ?

Remark: with code below, I get Id without SQL statement
((HibernateProxy)<objet persistent>.get<objet associƩ>().getId()).getHibernateLazyInitializer().getIdentifier();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 7:31 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

if the realtions are set to lazy hibernate don't retrieves them, while it creates the proxy. Normally the proxy contains only the primary key and the @Basic attributes plus
@OneToOne (cause the default is eagger here).

In your case you changed the default or the @OneToMany relations aren't loaded by the default.

To make it easier I focus on the @OneToOne case.
So your Proxy don't contains that object, cause it was set to lazy. If you try to retrieve the id, you normally have to get that Object before. So hibernate is forced to load it from the database, which implies an SQL-Query.

If your Object is persistent (e.g. cause you called save() before) , Hibernate where forced to load everything before to check if your Object is dirty or not. So if you request the id of an relationship, it was already loaded before, so no new SQLQuery is needed. If you have accessed the object before it is also already loaded.

org.hibernate.Session.getIdentifier() works only if the object was already loaded before.


Quote:
Return the identifier value of the given entity as associated with this session. An exception is thrown if the given entity instance is transient or detached in relation to this session.


In your case
org.hibernate.proxy.LazyInitializer
Quote:
Get the identifier held by the proxy


also you call that statement on the id (getId()), so the cast to HibernateProxy and the call of getHibernateLazyInitializer().getIdentifier() makes no sence for me.

"<objet persistent>.get<objet associƩ>()" will cause an SQL-Statement if not loaded before. Otherwise not.

Hibernate does not know, that you only need the ID, which is probably already loaded, cause the foreign key is part of the same table and not the whole object. (Only OneToOne or ManyToOne, OneToMany is mapped on the other side or by join-table)

I don't known, if you can retrieve the foreign key, without loading the object. but as long you don't use a natural key, it s very likely that it is worthless anyway.

Hope this helps.

Greetings Michael


Last edited by Urmech on Thu Apr 16, 2009 7:44 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 7:40 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
If access type is field, then hibernate will not know the getter method on for the field (even for id). so in this condition if you access the id using the getter method (property ), this will generate the sql statement.

solution.

either define access="property" for id (in associated class)

or

Using objRef.id (field access) to access the id.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 8:40 am 
Newbie

Joined: Tue Apr 14, 2009 4:03 pm
Posts: 3
thank you very mutch.
with property access type for id, no sql statement is generated when I call object.getId()
I use annotations, so I've just moved @id from property to getter method.


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