Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

nhibernate - Create child by updating parent, or create explicitly?

What is the preferred method for creating children objects?

  1. Adding the child to a collection on it's parent and calling update on the parent, or
  2. Creating the child explicitly, add the child to a collection on the parent, and updating the parent?

I'm struggling with #1. And it's hard enough that I'm thinking #2 is preferable (less "magic").

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not sure what would be the "preferred method" (it could depend on who does prefer). But I can share my way

If the parent is a root entity, then we should save all the reference tree in in one shot: session.SaveOrUpdate(parent).

E.g. Employee has collection of Educations. In such case, it should go like this

  1. create Education give it reference to employee,
  2. Add() it to collection employee.Educations.
  3. Have/make the mapping inverse, cascade="all-delete-orphan" ...
  4. NHibernate will do what we expect on session.SaveOrUpdate(parent)

Some snippets in xml:

<class name="Employee" table="..." lazy="true" 
  optimistic-lock="version" dynamic-update="true" batch-size="25" >

  <cache usage="read-write" region="ShortTerm"/>
  <id name="ID" column="Employee_ID" generator="native" />

  <bag name="Educations" lazy="true" inverse="true" 
       batch-size="25" cascade="all-delete-orphan" >
    <key column="Employee_ID" />
    <one-to-many class="Education" />
  </bag>
  ...

And the Education

<class name="Education" table="..." lazy="true" batch-size="25>
  <cache usage="read-write" region="ShortTerm"/>
  <id name="ID" column="Education_ID" generator="native" />

  <many-to-one not-null="true" name="Employee" class="Employee" 
             column="Employee_ID" />
...

in fluent:

public class EmployeeMap : ClassMap<Employee>
{
  public EmployeeMap()
  {
      BatchSize(25)
      ...
      HasMany(x => x.Educations)
        .AsBag()
        .BatchSize(25)
        .LazyLoad() 
        .Cascade.AllDeleteOrphan() 
        .Inverse()
        .KeyColumn("Employee_ID")

public class EducationMap : ClassMap<Education>
{
  public EducationMap()
  {
     ...
     References(x => x.Employee, "Employee_ID")
          .Not.Nullable()
          ...

And now the C# relations:

// business
var employee = ...;
var education = new Education
{
   Employee = employee, 
   ...
};
employee.Educations.Add(education);

// DAO
session.SaveOrUpdate(employee);

If the parent is not root, there is just a relation (Employee has collection of Subordinates of type Employee), keep the persisting separated


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...