Hi Guys,
I have been using NHibernate for about 2 weeks now. For the most part, some Googling and asking around solves my issues.
I am currently running into an issue trying to map the following: Parent Child (PK) GrandChild (PK,FK) <- Child.PK
Child table has a ChildId as primary key, GrandChild uses ChildId as ForeignKey as well as Primary key.
The Child entity looks like this: public class child{ public virtual SomeOtherObjects Other{get;set;} public virtual GrandChild GrandChild{get;set;} }
mapping looks like this: Child <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="db.dbo"> <class name="Entities.Child, domain" table="Child">
<id name="ChildId" column="ChildId" > <generator class="native" /> </id>
<property name="SomeProperty" column="SomeProperty" />
<one-to-one name="GrandChild" class="Entities.GrandChild,domain" cascade="save-update" lazy="false" />
</class> </hibernate-mapping>
GrandChild <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="db.dbo"> <class name="Entities.GrandChild, domain" table="GrandChild">
<id name="ChildId" column="ChildId" > <generator class="assigned" /> </id> <property name="SomeProperty" column="SomeProperty" />
</class> </hibernate-mapping>
So basically when I do a SaveOrUpdate(childObj), I get an sql error basically NHibernate wants to insert 0 into the GrandChild. Since Child owns the PK, I want to map it so NHibernate knows to insert the child before it does GrandChild.
Can someone please point me to the right direction?
What got me started looking is I was saving parent and get a transient object not saved exception so I started digging around and decided to attempt to save Child by writing a repository. So right now I think the relationship between Child and GrandChild is what's killing me.
|