nhibernate - 通过更新父级创建子级,还是显式创建? [英] nhibernate - Create child by updating parent, or create explicitly?

查看:20
本文介绍了nhibernate - 通过更新父级创建子级,还是显式创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建子对象的首选方法是什么?

What is the preferred method for creating children objects?

  1. 将子项添加到其父项上的集合并调用父项上的更新,或
  2. 显式创建子级,将子级添加到父级的集合中,然后更新父级?

我正在为#1 苦苦挣扎.而且这已经够难了,我认为 #2 更可取(不那么魔法").

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

推荐答案

不确定什么是首选方法"(这可能取决于谁更喜欢).但我可以分享我的方式

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

如果父实体是根实体,那么我们应该一次性保存所有参考树:session.SaveOrUpdate(parent).

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

例如EmployeeEducations 的集合.在这种情况下,它应该是这样的

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

  1. create Education 给员工参考,
  2. Add() 到集合 employee.Educations.
  3. 具有/使映射反向,cascade="all-delete-orphan" ...
  4. NHibernate 会在 session.SaveOrUpdate(parent) 上做我们期望的事情
  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)

xml 中的一些片段:

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

和教育

<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" />
...

流利:

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()
          ...

现在是 C# 关系:

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

// DAO
session.SaveOrUpdate(employee);

如果父级不是 root,则只有一个关系 (EmployeeEmployee 类型的 Subordinates 集合), 保持持久化分离

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

这篇关于nhibernate - 通过更新父级创建子级,还是显式创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆