NH 3.2, default proxy, field interception, and non-unique objects

44 views
Skip to first unread message

Cheetah

unread,
Aug 2, 2011, 12:15:41 PM8/2/11
to nhu...@googlegroups.com
I'm trying to upgrade from NH 3.1 where I was using the Castle proxy factory with mappings containing lazy properties (so field interception proxies are at play here).

A notable change that is causing me much pain is that in NH 3.2, if a field interception proxy is at play, there are two instances of every persistent object!  There is the field interception proxy instance, and there is an instance which it wraps.  This is causing me two major problems:

1) Any property mapped with an access="field.whatever" strategy has the field set only on the proxy, not on the wrapped object
2) Any method invocation is passed down to the wrapped object, it is not invoked on the proxy.

This produces two failure scenarios:

1) Method invocations cannot see the field values set by the access strategy, because they were set on a different instance.  So collections mapped that way all look empty from within method bodies.

2) Method invocations that somehow expose "this" often eventually cause a NonUniqueObjectException to be thrown.  For example, a method on a parent object that is attaching a child (populating bidirectional relationships among other work) puts a reference to the wrapped instance into the child.  When attempting to save the child, StatefulPersistenceContext.CheckUniqueness sees that the child's reference to its parent is not the same instance as the proxy instance the session knows about, and so throws the exception.

I managed to work around failure #1 with a custom version of the FieldAcessor class that sets the field on both the proxy and the wrapped instance.  It's an ugly hack, which disables the IL optimized version of the getter, but it seems to work.

I cannot see any way to deal with failure #2, because I can't see how the wrapped instance can even find out if the proxy instance exists, much less get a reference to it.

Fabio Maulo

unread,
Aug 6, 2011, 7:34:54 AM8/6/11
to nhu...@googlegroups.com
If you have a failing test a JIRA issue is appreciate.

James Nail

unread,
Aug 22, 2011, 7:34:46 PM8/22/11
to nhusers
Fabio,
I think this issue might be related to https://nhibernate.jira.com/browse/NH-2772
on some level.

It looks like the changes in 3.2 broke existing (pre-3.2) Lazy
Properties/proxy-based implementations.

Shatl

unread,
Apr 23, 2013, 12:18:53 PM4/23/13
to nhu...@googlegroups.com
Hi,

just run into the issue #2 with NHibernate 3.3.3.4000.

Getting NonUniqueObjectException when saving bi-directional associations, set with helper method like this:
public virtual void SetChild(Child child) {
  Child = child;
  child.Parent = this;
}

Are there any workarounds / estimations when it could be fixed?

I cameup with moving this code to extension methods, but I'm not very happy this this solution.

Thanks
Reply all
Reply to author
Forward
0 new messages