-->
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.  [ 3 posts ] 
Author Message
 Post subject: Generic list property problem
PostPosted: Fri Dec 07, 2007 8:43 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I am having a problem with a property accessor on a POCO using generic lists. The property works fine on the POCO class directly, but not on the NHibernate generated proxy. (I'm using NHibernate 1.2.1.GA)

I used the following class definition:

Code:

    public class MyPoco
    {

        private int _id;
        private List<MyPoco> _transientRelatedItems = new List<MyPoco>();

        public virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public virtual IList<MyPoco> TransientRelatedItems
        {
            get { return _transientRelatedItems; }
        }

        public virtual void AddToRelatedParent(MyPoco relatedParent)
        {
            relatedParent._transientRelatedItems.Add(this);

            Trace.Assert(   relatedParent._transientRelatedItems == relatedParent.TransientRelatedItems,
                            "The accessor should return the same reference as the member variable");
        }

    }



And the following mapping file:

Code:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

    <class  name="GenericListProblemDemo.MyPoco, Runner"
            table="MyPoco">

        <id name="Id" column="Id">
            <generator class="native"/>
        </id>

    </class>

</hibernate-mapping>



... and when I execute the following code, the Trace.Assert() call fails:

Code:
    MyPoco persistentParent = session.Load<MyPoco>(1);
    MyPoco transientChild = new MyPoco();
    transientChild.AddToRelatedParent(persistentParent);


I've uploaded a complete example here: http://www.uploading.com/files/RXVQE3XA/NhGenericListProblem.zip.html
(just open the 'Command Prompt.bat', and type nant)


It looks like the overriden property accessor (in the NHibernate generated proxy) is returning a different collection to the original property accessor. Note, the collection is not mapped/persisted - it's meant to contain transient references.


Has anyone seen this before? Any help would be appreciated.

Thanks,
Richard


Top
 Profile  
 
 Post subject: Understanding NHibernate proxies ...
PostPosted: Mon Dec 10, 2007 9:30 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I've had a look through the NHibernate code (in particular the CastleLazyInitializer and it's IInterceptor.Intercept() method).

I understand why the above code fails now. I hadn't realised that NHibernate creates two classes when it creates my proxy object.

Out of interest, is there a reason that it needs to instantiate an instance of the real class? I would have thought the DP created class has all the method/properties/fields that the real one does since it inherits from it (making the instance of the real class redundant).

In addition, if the proxy class was the only instance created, then this would also mean that Identity Map would work from inside the domain classes (currently the 'this' pointer is a different reference to the proxy).

Any thoughts welcome.

Thanks,
Richard


Top
 Profile  
 
 Post subject: Understanding NHibernate Identity Map and proxies ...
PostPosted: Thu Jan 03, 2008 9:17 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I've put together a small blog entry to explain the problem I was having in more detail (hopefully it might be of use to someone else encountering the same problem):

http://broloco.blogspot.com/2008/01/nhibernate-identity-map-and-proxies.html

In addition, would it be possible to open a JIRA to investigate making the proxies self-proxy without creating a second instance? (Assuming this is even possible, and doesn't break any existing features.) I realise this isn't a bug, but a (small) change to the design of NHibernate.

Thanks,
Richard


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.