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: Exception when persisting collection
PostPosted: Wed Oct 15, 2008 12:17 am 
Newbie

Joined: Tue Jun 17, 2008 1:45 am
Posts: 4
Location: Melbourne, VIC, Australia
Hi All,

I am new to NHibernate (the last ORM I used in anger was Rails' ActiveRecord) and am having a devil of a time persisting collections.

This test works:

Code:
[Test]
public void NHibernateService_UpdateA_SavesAWithSingleC()
{
    ClassA a = service.GetA(existingId);
    ClassC c = a.BArray[0].C;

    var newA = new ClassA
                   {
                       BArray = new[]
                                    {
                                        new ClassB { C = c }
                                    }
                   };


    service.UpdateA(newA);
}


... whereas this test does not:

Code:
[Test]
public void NHibernateService_UpdateA_SavesAWithIdenticalC()
{
    ClassA a = service.GetA(existingId);
    ClassC c = a.BArray[0].C;

    var newA = new ClassA
                   {
                       BArray = new[]
                                    {
                                        new ClassB { C = c },
                                        new ClassB { C = c },
                                    }
                   };


    service.UpdateA(newA);
}


It fails with the following exception:

Code:
NHibernate.NonUniqueObjectException was unhandled by user code
  Message="a different object with the same identifier value was already associated with the session: 162, of class: NHibernateService.ClassC


The mappings are as follows:

Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHibernateService.ClassA, NHibernateService" lazy="false" table="CLASS_A">
    <id name="Id" column="ID" unsaved-value="0">
      <generator class="native" />
    </id>
    <bag name="BArray" cascade="all" inverse="false">
      <key column="A_ID" />
      <one-to-many class="NHibernateService.ClassB, NHibernateService" />
    </bag>
  </class>
  <class name="NHibernateService.ClassB, NHibernateService" lazy="false" table="CLASS_B">
    <id name="Id" column="ID" unsaved-value="0">
      <generator class="native" />
    </id>
    <many-to-one name="C" class="NHibernateService.ClassC, NHibernateService" column="C_ID" not-null="true" cascade="all" lazy="false" />   
    <many-to-one name="Parent" class="NHibernateService.ClassA, NHibernateService" column="A_ID" not-null="true" cascade="all" lazy="false" />
  </class>
  <class name="NHibernateService.ClassC, NHibernateService" lazy="false" table="CLASS_C">
    <id name="Id" column="ID" unsaved-value="0">
      <generator class="native" />
    </id>
  </class>
</hibernate-mapping>


And the object files are simple too:

Code:
[DataContract]
[Serializable]
public class ClassA
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public IList<ClassB> BArray { get; set; }

    [OnDeserialized]
    private void GenerateBacklinks(StreamingContext context)
    {
        if (BArray != null)
        {
            foreach (ClassB b in BArray)
            {
                b.Parent = this;
            }
        }
    }
}
[DataContract]
[Serializable]
public class ClassB
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public ClassC C { get; set; }

    public ClassA Parent { get; set; }
}
[DataContract]
[Serializable]
public class ClassC
{
    [DataMember]
    public int Id { get; set; }
}


Finally, the simple method on the WCF service that does the persistence:

Code:
public int UpdateA(ClassA target)
{
    using (ISession session = GetSession())
    {
        session.SaveOrUpdate(target);
        session.Flush();
        session.Close();
    }

    return target.Id;
}


Can anyone give me any pointers as to where I might be going wrong? I don't think that what I'm doing is so demanding, and this suggests I'm missing something pretty simple ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2008 7:45 pm 
Newbie

Joined: Tue Jun 17, 2008 1:45 am
Posts: 4
Location: Melbourne, VIC, Australia
Bumping ... still hoping for some advice.


Top
 Profile  
 
 Post subject: Fix
PostPosted: Wed Oct 29, 2008 9:19 pm 
Newbie

Joined: Tue Jun 17, 2008 1:45 am
Posts: 4
Location: Melbourne, VIC, Australia
I can fix the problem by walking the object tree after WCF deserialization, and ensuring that all instances of a particlar ClassC point to the same reference:

Code:
            var cs = new Dictionary<int, ClassC>();
            foreach (ClassB b in target.BArray)
            {
                // skip null or transient objects
                if (b.C != null && b.C.Id != 0)
                {
                    if (cs.ContainsKey(b.C.Id))
                    {
                        b.C = cs[b.C.Id];
                    }
                    else
                    {
                        cs.Add(b.C.Id, b.C);
                    }
                }
            }


Seems kinda ugly though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2008 5:30 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have you tried implementing GetHashCode() and Equals() so that both copies are treated as equal ?

_________________
--Wolfgang


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.