nhibernate - 通过更新父对象或显式创建来创建子对象?
nhibernate - Create child by updating parent, or create explicitly?
创建子对象的首选方法是什么?
- 将子项添加到其父项的集合中并调用父项的更新,或者
- 显式创建子项,将子项添加到父项的集合,并更新父项?
我正在为#1 苦苦挣扎。这已经够难了,我认为 #2 更可取(更少 "magic")。
不确定 "preferred method" (可能取决于谁更喜欢)。但我可以分享我的方法
如果 parent 是根实体,那么我们应该一次性保存所有参考树:session.SaveOrUpdate(parent)
。
例如Employee
有 collection 个 Educations
。在这种情况下,它应该是这样的
- 创建
Education
将其引用给员工,
Add()
到 collection employee.Educations
。
- Have/make 逆映射,cascade="all-delete-orphan" ...
- NHibernate 将在 session.SaveOrUpdate(parent)
上执行我们期望的操作
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);
如果 parent 不是根,则只有关系 (Employee
has collection of Subordinates
of type Employee
),保持持久分离
创建子对象的首选方法是什么?
- 将子项添加到其父项的集合中并调用父项的更新,或者
- 显式创建子项,将子项添加到父项的集合,并更新父项?
我正在为#1 苦苦挣扎。这已经够难了,我认为 #2 更可取(更少 "magic")。
不确定 "preferred method" (可能取决于谁更喜欢)。但我可以分享我的方法
如果 parent 是根实体,那么我们应该一次性保存所有参考树:session.SaveOrUpdate(parent)
。
例如Employee
有 collection 个 Educations
。在这种情况下,它应该是这样的
- 创建
Education
将其引用给员工, Add()
到 collectionemployee.Educations
。- Have/make 逆映射,cascade="all-delete-orphan" ...
- NHibernate 将在 session.SaveOrUpdate(parent) 上执行我们期望的操作
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);
如果 parent 不是根,则只有关系 (Employee
has collection of Subordinates
of type Employee
),保持持久分离