nhibernate - 通过更新父项创建子项,还是明确创建? [英] nhibernate - Create child by updating parent, or create explicitly?
问题描述
创建子对象的首选方法是什么?
- 将子对象添加到父对象的集合中, ,或
- 显式创建子项,将子项添加到父项的集合中,并更新父项?
我正在努力争取#1。而且,我认为#2是比较好的选择(不太魔术)。
不知道会是什么首选方法(可能取决于谁更喜欢)。但我可以分享我的方式如果父项是一个根实体,那么我们应该保存所有的参考树: session。 SaveOrUpdate(父)
。
员工
收集了 Educations
。在这种情况下,它应该是这样的
- 创建
教育
给它参考雇员 -
Add()
它到集合employee.Educations
。
-
- $ / $>
- NHibernate将会执行我们期望的会话。 SaveOrUpdate(parent)
xml中的一些代码片段:
< class name =Employeetable =...lazy =true
optimistic-lock =versiondynamic-update =truebatch-size =25 >
< cache usage =read-writeregion =ShortTerm/>
< id name =IDcolumn =Employee_IDgenerator =native/>
< bag name =Educationslazy =trueinverse =true
batch-size =25cascade =all-delete-orphan>
< key column =Employee_ID/>
<一对多课程=教育/>
< / bag>
...
和教育
< class name =Educationtable =...lazy =truebatch-size =25>
< cache usage =读写region =ShortTerm/>
< id name =IDcolumn =Education_IDgenerator =native/>
< many-to -one not-null =truename =Employeeclass =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()
...
$ b $现在的C#关系:
// business
var employee = ...;
var education = new Education
{
Employee = employee,
...
};
employee.Educations.Add(education);
// DAO
session.SaveOrUpdate(employee);
如果父项不是root,则只有一个关系(员工
收集员工
)
What is the preferred method for creating children objects?
- Adding the child to a collection on it's parent and calling update on the parent, or
- 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").
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
- create
Education
give it reference to employee, Add()
it to collectionemployee.Educations
.- Have/make the mapping inverse, cascade="all-delete-orphan" ...
- 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
这篇关于nhibernate - 通过更新父项创建子项,还是明确创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!