English 中文(简体)
nHibernate one-to-many inserts but doesnt update
原标题:

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key.

Has anyone ever had a one-to-many where the child object gets inserted but not updated resulting in a row in my table with a null in the foreign key column?

I want the default behaviour for a standard one-to-many. I don t want to have to add the parent as a property to the child.

Thanks.

问题回答

This would happen if you didn t have cascade="save-update" on your set/bag

or if you set your session s FlushMode to None or Commit and saved the child using your childRepository and neglected to save the object containing the collection using its repository.

I think you have to set parent reference in child item.

class Parent {
  public virtual IList<Child> Children;
}

class Child {
  public virtual Parent Parent;
}

Parent p = new Parent();
Child c = new Child();
c.Parent = p;
p.Children = new List<Child>();
p.Children.Add(c);

Now when you save this transient object p you will have the right foreign key in the child table.





相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签