我有一个新员工的员工输入屏幕,在提交时被员工模型绑定器拦截。员工拥有“业务部门”和“业务部门”。越控业务部门是最近添加的,并且是我的问题的原因。对象引用一个未保存的瞬态实例问题
这里是部分员工的映射:
<class name="Employee" table="Employee">
<id name="Id" column="id">
<generator class="native" />
</id>
...
<many-to-one name="BusinessUnit" class="BusinessUnit" column="businessUnitId" />
<many-to-one name="OverrideBusinessUnit" class="BusinessUnit" column="overrideBusinessUnitId" not-null="false" />
...
</class>
这里是业务单位映射:
<class name="BusinessUnit" table="BusinessUnit" lazy="true">
<id name="Id" column="id">
<generator class="native" />
</id>
<property name="Description" column="description"/>
<many-to-one name="guidelineApprover" class="Employee" column="guidelineApproverId" cascade="none" fetch="join" access="field" />
<many-to-one name="increaseApprover" class="Employee" column="increaseApproverId" cascade="none" fetch="join" access="field" />
</class>
在员工模型绑定提交表单后,我使用NHibernate获得两个业务部门和数据库覆盖业务部门。这是代码:
模型中的粘合剂从数据库中获取业务单元:
string attemptedBusinessUnitId = valueProvider.GetValue(key).AttemptedValue;
Int32.TryParse(attemptedBusinessUnitId, out businessUnitId);
employee.BusinessUnit = businessUnitRepository.Get(businessUnitId);
modelState.SetModelValue(key, valueProvider.GetValue(key));
模型中的粘合剂从数据库中获取覆盖业务部门:
string attemptedOverrideBusinessUnitId = valueProvider.GetValue(key).AttemptedValue;
Int32.TryParse(attemptedOverrideBusinessUnitId, out overrideBusinessUnitId);
employee.OverrideBusinessUnit = businessUnitRepository.Get(overrideBusinessUnitId);
modelState.SetModelValue(key, valueProvider.GetValue(key));
我现在获取模式设置为“提交”。我的问题是,我开始下面的错误后,我加入了“覆盖业务部门”多到一个和我尝试执行employeeRepository.Save(员工):
object references an unsaved transient instance - save the transient instance before flushing.
Type: BusinessUnit, Entity: BusinessUnit
如果我设置级联= “all”这个字段,我只是得到另一个类似的异常,但是需要保存Employee类型的实例EmployeeEmpty的临时实例。任何人都可以通过查看代码片段告诉我如何避免此异常(最好不涉及级联)?
编辑:
employeeRepository.Save(员工)只是调用session.SaveOrUpdate(员工)。所以我所要做的就是在分配给它之后保存员工,这是我从数据库中检索到的两个业务单位字段。
我加了一点信息。这对于同一个班级有两对多的关系,这两个都是从数据库中重新水化并分配给该员工的。当我刚刚处理“businessUnit”多对一时,我没有这个问题。此问题仅在我多次添加“businessUnitOverride”时引入。 – SideFX
我不认为这可能是一个问题,在映射意义上看起来不错。但从设计意义上来说,对同一班级的多对多映射并不是一个好主意。既然他们已经很多,为什么你想要然后在两个单独的名单? –
此外,映射相当混乱。从映射文件中,我可以看到员工和业务单位之间存在多对多关系(准则申请者和增加申请者)。我会建议您再次修改映射,即使您设法在不这样做的情况下摆脱此错误,最终可能会导致问题。在这种情况下,您可能需要GuidelineApprover和IncreaseApprover来扩展Employee,然后使用BussinessUnit进行多对多映射。对于bussinessUnit和overrideBussinessUnit,Employee端应该做同样的事情。 –