-->
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: Accessing proxy identifier getter loads entity
PostPosted: Sat Dec 31, 2005 8:12 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:1.0.1

I've just discovered that accessing a proxy's identifier getter method is not performing as described in the Hibernate documentation:

Quote:
Certain operations do not require proxy initialization

* equals(), if the persistent class does not override equals()
* hashCode(), if the persistent class does not override hashCode()
* The identifier getter method


In fact, it's loading the record from the DB when the identifier getter is touched.

Is this behavior the result of the differences between the Java and DynamicProxy implementations of proxying? Has it been done deliberately, or it this a bug?

I've noticed that there's a GetIdentifier method on the Session object, but this tests to see whether the session is open, so the identifier for a disconnected proxy can't be obtained. Also, I've found the NHibernateProxyHelper.GetIdentifier method, but it's asking for the IClassPersister for the object, and I don't know what it is, so I've been unable to use it so far.

Is there another way to do this that will allow you to access the identifier for a proxy regardless of whether or not it's associated with a session?

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 01, 2006 10:57 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
This should definitely work as described if identifier's access is set to property. nosetter should also work, but I'm not sure about it. access="field" won't work.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 01, 2006 12:27 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Hmm.

I've set default-access to field.camelcase - as I understand it NHibernate is supposed to use the property for reading and only use the field for writing, so does this qualify as "access=field" from your previous comment?

Anyway, regardless, is there another way to get access to the identifier from a proxy without loading or having a session?

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 01, 2006 12:38 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Ah, my mistake. I guess it's no-setter that uses the property for read the the field for write. I *could* use no-setter under some circumstances, but I'd rather use field access in general.

Still, is there a way to get the identifier without initializing the proxy?

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 01, 2006 1:51 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You could use the code from NHibernateProxyHelper:
Code:
INHibernateProxy proxy = ( INHibernateProxy ) obj;
LazyInitializer li = NHibernateProxyHelper.GetLazyInitializer( proxy );
return li.Identifier;

This will not work for real objects, only for proxies (loaded or not).

Or use ISession.GetIdentifier if you have a session that contains the persistent object (GetIdentifier will cause an exception if the session is closed or doesn't contain the queried object).

Or use NHibernateProxyHelper.GetIdentifier, passing it the IClassPersister for the object. The persister can be found by calling ISessionFactory.GetClassMetadata(someclass) and casting the result to IClassPersister. But the last solution is the least preferable because it relies heavily on NH internals.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 01, 2006 2:39 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Thanks Sergey,

That's just the info I was looking for. I'll change my identifiers to use nosetter wherever I can and can then use the NHibernateProxyHelper when I can't.

Cheers,

Symon.


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.