-->
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: Session.get() in combination whith cache and lazy="fals
PostPosted: Thu Apr 21, 2005 11:06 am 
Session.get() in combination whith cache and lazy="false" broken???


I have trouble with Session.get() which is supposed to always return a non-proxied instance. Well it does not. Not even when using lazy="true". I also have caching enabled for the class. I have narrowed it down to the class org.hibernate.event.def.DefaultLoadEventListener

in the method proxyOrLoad (at around row number 227)

I actualy get a proxy back even though I have no proxies enabled. I might add that it works when the cache does not contain a value.

Is this as expected? Why does the cache usage change the behaviour?

Keep up the good work!

/Konstantin



Code:
      org.hibernate.event.def.DefaultLoadEventListenerin.proxyOrLoad  (at around row number 227)

      Object proxy = persistenceContext.getProxy(keyToLoad);
            if ( proxy != null ) { // <-- Execution enters this part /Konstantin
               log.trace("entity proxy found in session cache");
               Object impl;
               if ( options.isAllowProxyCreation() ) {
                  impl = null;
               }
               else {
                  //force initialization of the proxy
                  impl = ( (HibernateProxy) proxy ).getHibernateLazyInitializer().getImplementation();
               }
               // return existing or narrowed proxy
               Object narrowed = persistenceContext.narrowProxy( proxy, persister, keyToLoad, impl );
               return narrowed;
            }
            else if ( options.isAllowProxyCreation() ) {
               log.trace("creating new proxy for entity");
               // return new uninitialized proxy
               proxy = persister.createProxy( event.getEntityId(), event.getSession() );
               persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey(keyToLoad);
               persistenceContext.addProxy(keyToLoad, proxy);
               return proxy;
            }



mapping excrept:

<class name="CompositeElement" table="CMT_PROD_PAGE_ELEMENT"
lazy="false"
>

<cache usage="nonstrict-read-write" />
...

Hibernate version:3.01


Top
  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 2:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
get does not promise you will get a non-proxied class it just promise to fail if the entity is not in the db

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 2:59 am 
It does at least in Hibernate 2. I checked the book Hibernate in Action p141:

""...On the other hand, get() never returns a proxy."

But it did some more investigation I found the source for why proxies where enabled in this case. It was in the same class. in the method onLoad starting at row 54. Instead of triggering a loadtye of GET it triggered an INTERNAL_LOAD.

Sol I guess caching changes the behaviour of Hibernate?

/Konstantin


Top
  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 3:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hmm - the javadoc (and code) states the following:

"Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. (If the instance, or a proxy for the instance, is already associated with the session, return that instance or proxy.)"

And that javadoc is the same in both H2.1 and H3.

The text in HiA is a bug IMO. I've asked us to update the errata with this info.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 3:33 am 
Cool! Then I know.


Top
  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 3:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
..and just to be precise the thing get() actually promises is the following:

It will return null instead of throwing an exception if the object cannot be found first in the session and *then* in the db.
Meaning if you have already queried or load'ed the object as an proxy get will just return that one.

-max

_________________
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.  [ 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.