All,

I have the following entities:

public class Parent
{
        public Guid Id;

        public string Description;

        public IList<Child> Children;
}

public class Child
{
        public Guid Id;

        public Guid ParentId;

        public string Description;
}

I have the following mappings:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
access="field">
        <class name="ClassLibrary1.Parent, ClassLibrary1" table="dbo.
[TEMP_Parent]" lazy="false" >
                <id name="Id">
                        <generator class="guid.comb" />
                </id>
                <property name="Description" />
                <bag name="Children" cascade="all" lazy="false"  >
                        <key column="ParentId" foreign-key="true"/>
                        <one-to-many class="ClassLibrary1.Child"  />
                </bag>
        </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
access="field">
        <class name="ClassLibrary1.Child, ClassLibrary1" table="dbo.
[TEMP_Child]" lazy="false">
                <id name="Id">
                        <generator class="guid.comb" />
                </id>
                <property name="ParentId" />
                <property name="Description" />
        </class>
</hibernate-mapping>


When I run the following test I get an error:

[Test]
public void Test1()
{
        var _parent = new Parent();
        _parent.Description = "Parent desc";
        _parent.Children = new List<Child>();
        _parent.Children.Add(new Child
        {
                Description = "child desc 1"
        });
        _parent.Children.Add(new Child
        {
                Description = "child desc 2"
        });

        var _repository = new Repository(this.c_sessionManager);
        _repository.Save(_parent);
}

NHibernate: INSERT INTO dbo.[TEMP_Parent] (Description, Id) VALUES
(@p0, @p1);@p0 = 'Parent desc', @p1 = 8c7f22c3-1a86-478a-
b8a7-9e8300cdc91b
NHibernate: INSERT INTO dbo.[TEMP_Child] (ParentId, Description, Id)
VALUES (@p0, @p1, @p2);@p0 = 00000000-0000-0000-0000-000000000000, @p1
= 'child desc 1', @p2 = db3c4259-58bb-4ca0-80e5-9e8300cdc924
12:29:14,711 ERROR [   7] AbstractBatcher [(null)]- Could not execute
command: INSERT INTO dbo.[TEMP_Child] (ParentId, Description, Id)
VALUES (@p0, @p1, @p2)
System.Data.SqlClient.SqlException (0x80131904): The INSERT statement
conflicted with the FOREIGN KEY constraint "PK_TEMP_Child_ForeignKey".
The conflict occurred in database "OLCTG_v2", table "dbo.TEMP_Parent",
column 'Id'.
The statement has been terminated.

The childs foreign key back to the parent is never populated.
Any suggestions to resolve this issue?

Cheers,
Billy Stack

-- 
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