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: How to get a reference to proxy if I have reference to ...
PostPosted: Fri Nov 21, 2008 5:31 pm 
Regular
Regular

Joined: Tue Oct 16, 2007 9:45 am
Posts: 93
Hibernate version: 2.0 GA

... proxied class. I am running in situation that I need to get reference to instance of a proxy when I have reference to a proxied object. I am adding a LoadCollection listener, and collection.Owner is a proxied class and instance of owner is proxy, so when I do bidirectional relationship synchronization this might fail

Assert.IsTrue(ReferenceEquals(invoice, invoice.lineItems[1].invoice)

because the "invoice" is a proxy and invoice.lineItems[1].invoice is the instance of the collection.Owner. Since I have real class I wonder if I could look up somehow what is the proxy instance for it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 6:05 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We are running into the same problem.

To describe the problem in broader terms: for entity classes that pass on a reference to itself ("this") through method calls or events, whenever an instance of that class gets proxied, outside code gets a reference to the inner entity that the proxy forwards to.

With my particular problem and exp2000's, it happens during bidirectional relationship sychronization (an entity with a collection property passes itself as the collection's owner). But a more common scenario is with PropertyChanged events, since the entity is passed as the sender to the event handlers. I would assume a lot of people implement INotifyPropertyChanged and would therefore have this problem checking for reference equality.

We need to check for reference equality, because we do a lot of reattachment to new sessions and need to distinguish between truly different instances of the same type with the same ID to avoid NonUniqueObjectException. But we also don't want to consider an entity and a proxy to that same instance as different.

We're going to work around this by defining a MemoryInstanceId property in our entity base class which gets uniquely generated and assigned in the constructor, and compare that. Still, it would be nice if NHibernateUtil had something like an AreUnproxiedInstancesEqual() method, or if the Unproxy() method on SessionImpl (? I know I've seen it somewhere) was made part of the ISession interface.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 9:20 pm 
Regular
Regular

Joined: Tue Oct 16, 2007 9:45 am
Posts: 93
Nels, check this link, this is actually working for me very well. It passed all of unit tests

http://groups.google.com/group/nhusers/ ... 2483a59c5e


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 10:44 am 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
From the link:

Quote:
If you re-load the object from the session, you'll get the proxy back (and
it won't hit the database). In the past I've added a 'This' property on my
domain class to do this:
Code:
private Pepe This
{
    get
    {
        return Session.Load<Pepe>(typeof(Pepe), Id);
    }
}


http://forum.hibernate.org/viewtopic.php?t=982610


Hmmm... interesting. Unfortunately, this puts knowledge of NHibernate into my POCO, and also requires that my entity is already attached to the current session.


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.