You need to add the Child to ChildList. Otherwise, how would NHibernate find it?
Diego On Fri, Feb 5, 2010 at 13:07, CSLem <[email protected]> wrote: > I have what I think is a VERY simple mapping. This particular one uses > DB generated Ids for all tables and, depending on how I set up inverse/ > cascade I can get saves to work all the time but either inserts or > deletes fail. Insert sometimes fails with a Cannot insert NULL value > in ChildId column or appears to work but issues no INSERT to the SQL > database. When I get the inserts to work the deletes fail due to the > foreign key relationship on the child. > > I always want the Parent to manage the relationship to the DB. That > is, for this application, there is never a need to try to insert/ > update/delete except through the parent so when the update statement > is issued it is always session.saveorupdate(Parent); I would expect > that to issue the necessary INSERT Parent..., INSERT Child...; etc > with the correct ID or the necessary UPDATE/DELETE statements. > > I am using the NHibernate V2.1.0.4000 and NHibernate.Linq V1.0.0.4000, > although I don't know that Linq matters on this side of the equation. > > The current classes/maps are as follows and this version manages > everything except the insert. > > public class Parent { > public virtual int ParentId { get; set; } > public virtual DateTime? LastModified { get; set; } > public virtual IList<Child> ChildList { get; set; } > } > > public class Child { > public virtual int ChildId { get; set; } > public virtual Parent Parent { get; set; } > public virtual DateTime? Created { get; set; } > public virtual string CreatedBy { get; set; } > public virtual CommentType CommentType { get; set; } > public virtual string Comment { get; set; } > public virtual DateTime? LastModified { get; set; } > } > > <?xml version="1.0" encoding="utf-8" ?> > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" > assembly="NewAssembly.Core" > namespace="NewAssembly.Core"> > > <class name="Parent" table="t_Parent"> > <id name="ParentId" type="string" unsaved-value="0"> > <generator class="native"></generator> > </id> > <timestamp name="LastModified"/> > <bag name="ChildList" inverse="true” cascade="all-delete-orphan"> > <key column="ParentId"/> > <one-to-many class="Child"/> > </bag> > </class> > </hibernate-mapping> > > <class name="Child" table="t_Child"> > <id name="ChildId"> > <generator class="native"/> > </id> > <timestamp name="LastModified"/> > <property name="Created"/> > <property name="CreatedBy"/> > <property name="CommentType" column="Type"/> > <property name="Comment"/> > <many-to-one name="Parent" column="ParentId"/> > </class> > </hibernate-mapping> > > When the Child object is constructed I do set the Parent property. On > an insert, however, the ParentId is obviously not generated yet and > the call to session.SaveOrUpdate(Parent) issues an INSERT to SQL for > the parent record and, depending on settings, either no INSERT for the > child or an INSERT with a NULL value constraint error. > > What am I missing to get all three operations (insert, save, delete) > working? > > -C- > > -- > 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]<nhusers%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > -- 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.
