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: IInterceptor OnLoad is not called by ISession.CreateCriteria
PostPosted: Wed Oct 22, 2008 10:05 am 
Newbie

Joined: Wed Oct 22, 2008 8:37 am
Posts: 4
Location: Belgium
Why is the OnLoad method of the IInterceptor only called as a consequence of ISession.Load(object id), and not as a consequence of ISession.CreateCriteria(typeof(T))?

I want to change the state of each object loaded from database so that the object graph always points to objects that are already in memory, rather than introducing duplicates. The OnLoad method of the IInterceptor:

public bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)

provides the state of each loaded object as an object[]. Unfortunately, OnLoad is not called when objects are fetched via ISession.CreateCriteria(typeof(T)).

NHibernate version: 2.0

_________________
Peter Rigole

Qmino BVBA
http://www.qmino.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2008 3:16 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
You should be able to provide references to objects already in memory by implementing IInterceptor.Instantiate. We did exactly that -- keep a cache of WeakReferences, periodically pruning dead references, to avoid endless hassles with NonUniqueObjectException when we try to reattach entities to new sessions and somehow something else has already put a different instance of the same type & id in the session.

Our initial testing with this approach came out clean, it eliminated the NonUniqueObjectException problem, but we never enabled this logic for production use. We were concerned that some field values might get leftover if we recycle instances this way -- if either NHibernate doesn't set every field on every load, and because we have some non-persisted fields in our entities that might not get cleared.

A better approach would be if the session's 1st level cache implementation could be replaced. This would would not only seem cleaner, but (I think) it would handle the case where a proxy is created. IInterceptor.Instantiate() doesn't get called until a proxy is initialized, so it can't prevent a different proxy instance from getting created.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2008 5:42 am 
Newbie

Joined: Wed Oct 22, 2008 8:37 am
Posts: 4
Location: Belgium
I did some testing and I have the impression that IInterceptor.Instantiate is never called, although a massive amount of objects are fetched from database and instantiated. The IInterceptor documentation is very brief on explaining the exact semantics of its methods. In which situation should Instantiate method get called upon?

_________________
Peter Rigole

Qmino BVBA
http://www.qmino.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2008 9:17 am 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
IInterceptor.Instantiate is probably not called because the entities returned by the fetches are uninitialized proxies.


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.