Hi All,

Please consider the mapping below:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Messaging.Core" namespace="Messaging.Core.Common">
  <class name="BaseTest" table="BaseTest">
    <id name="Id" column="Id" type="Guid">
      <generator class="assigned"/>
    </id>
    <discriminator  />
    <property name="Prop1" />
    <subclass name="Derived1">
      <property name="Prop2" />
      <subclass name="SubDerived1" >
        <property name="Prop4" />
        <many-to-one name="Ref" cascade="all" fetch="join" />
      </subclass>
    </subclass>
    <subclass name="Derived2" >
      <property name="Prop3" type="String" />
    </subclass>
  </class>
</hibernate-mapping>

The object model...

    public class BaseTest : EntityBase
    {
        public virtual string Prop1 { get; set;}
    }

    public class Derived1 : BaseTest
    {
        public virtual string Prop2 { get; set; }
    }

    public class Derived2 : BaseTest
    {
        public virtual string Prop3 { get; set; }
    }

    public class SubDerived1 : Derived1
    {
        public virtual string Prop4 { get; set; }
        public virtual Derived2 Ref { get; set; }
    }

The test....

 [Test]
        public void subclass_cascade_ref_test()
        {
            SubDerived1 sd = new SubDerived1();
            sd.Prop1 = "1";
            sd.Prop2 = "3";
            sd.Ref = new Derived2();
            sd.Ref.Prop3 = "2";

            //Repository.Save(sd.Ref); //SURELY I DON'T HAVE TO
PERSIST THE CHILD FIRST?!?
            Repository.Save(sd);

            var retrieved = Repository.Get<SubDerived1>(sd.Id);
            Assert.IsNotNull(retrieved.Ref);
            Assert.AreEqual("2", retrieved.Ref.Prop3);

        }

I had expected setting cascade=all on the many-to-one relationship
would mean the child object would be inserted and then the parent.
However, I get the errors below. Explicitly saving the child first
works but I must be missing something... how do I get the child to be
auto inserted?


System.Data.SqlClient.SqlException: The INSERT statement conflicted
with the FOREIGN KEY SAME TABLE constraint "FKB4ACABACAFA06F00". The
conflict occurred in database "MotorTexter", table "dbo.BaseTest",
column 'Id'.
The statement has been terminated.

NHibernate.Exceptions.GenericADOException: could not insert:
[Messaging.Core.Common.SubDerived1][SQL: INSERT INTO BaseTest (Prop1,
Prop2, Prop4, Ref, class, Id) VALUES (?, ?, ?, ?,
'Messaging.Core.Common.SubDerived1', ?)]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to