升级到NHibernate 3后,一对一关系失败的儿童的持续性
我正在升级一个旧的NHibernate 1.2解决方案,我已经把它升级到NHib 3.1。我们在维持亲子关系方面遇到了问题。这给了我们一个错误: NHibernate.StaleObjectStateException:行被另一个事务更新或删除(或未保存的值映射不正确) 此代码在NHib 1.2中工作,但在3.1中不工作 我们保存的代码非常类似于下面的代码:升级到NHibernate 3后,一对一关系失败的儿童的持续性,nhibernate,nhibernate-mapping,Nhibernate,Nhibernate Mapping,我正在升级一个旧的NHibernate 1.2解决方案,我已经把它升级到NHib 3.1。我们在维持亲子关系方面遇到了问题。这给了我们一个错误: NHibernate.StaleObjectStateException:行被另一个事务更新或删除(或未保存的值映射不正确) 此代码在NHib 1.2中工作,但在3.1中不工作 我们保存的代码非常类似于下面的代码: Film f = NewFilm(); Recipe r = new Recipe("2", TimeSpan.FromMinutes(1
Film f = NewFilm();
Recipe r = new Recipe("2", TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(15));
f.Recipe = r;
SaveAndFlush(f, r); //custom code that saves f then saves r then flushes through the session.
然而,如果我们保存r,那么f和flush就可以了
我想知道为什么会发生这种情况,为什么NHib版本之间会发生变化。这是赛森认为实体现在是暂时的吗?它是否以不同方式处理外键id生成器
另一方面,配方的ID不等于电影的ID,我希望它能做到这一点
HMB文件更新为包含完整文件
电影:
配方:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo">
<class name="Application.Core.Domain.Recipe,Application.Core" table="tbl_Recipe" lazy="false">
<id name="Id" column="HeaderId" type="System.Guid" access="field.camelcase-underscore">
<generator class="foreign">
<param name="property">Content</param>
</generator>
</id>
<list inverse="false" lazy="true" name="RecipeIngredients" access="field.camelcase-underscore" cascade="all-delete-orphan">
<key column="RecipeId" />
<index column="PositionInRecipe"/>
<one-to-many class="Application.Core.Domain.RecipeIngredient, Application.Core" />
</list>
<property name="Serves" column="Serves" type="System.String"/>
<property name="PreparationTime" column="PreparationTime" type="TimeSpan"/>
<property name="CookingTime" column="CookingTime" type="TimeSpan"/>
<property name="OvenTemperature" column="OvenTemperature" type="Application.Data.UserTypes.TemperatureType, Application.Data"/>
<one-to-one name="Content" class="Application.Core.Domain.Content, Application.Core" constrained="true" access="field.camelcase-underscore"/>
</class>
</hibernate-mapping>
内容
如果对“子”实体进行级联保存,则只需保存父实体。你不需要救这个孩子
因此,在这里您应该尝试仅保存文档中的“f”。:
主键关联不需要
额外表列;如果两行是
由协会关联,然后
两个表行共享同一主表
键值。如果你想要两个物体
通过主键关联
协会,你必须确保
它们被分配了相同的标识符
价值
对于主键关联,请添加
以下映射到员工和
分别是一个人
现在我们必须确保
PERSON和中相关行的键
雇员表是相等的。我们使用
特殊NHibernate标识符
被称为“国外”的发电策略:
雇员
...
新保存的Person实例为
然后分配相同的主密钥
值作为引用的员工实例
用那家公司的员工财产
人
另一方面,配方的ID与电影的ID不相等,我希望它能做到这一点。
对于ID相等,您应该使用generator class=“foreign”这似乎不会影响1.2版本。它们是用正确的关联保存的。你能添加胶片和配方的完整映射吗?这也不行。那么,深层次可能存在问题?
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo">
<class name="Application.Core.Domain.Recipe,Application.Core" table="tbl_Recipe" lazy="false">
<id name="Id" column="HeaderId" type="System.Guid" access="field.camelcase-underscore">
<generator class="foreign">
<param name="property">Content</param>
</generator>
</id>
<list inverse="false" lazy="true" name="RecipeIngredients" access="field.camelcase-underscore" cascade="all-delete-orphan">
<key column="RecipeId" />
<index column="PositionInRecipe"/>
<one-to-many class="Application.Core.Domain.RecipeIngredient, Application.Core" />
</list>
<property name="Serves" column="Serves" type="System.String"/>
<property name="PreparationTime" column="PreparationTime" type="TimeSpan"/>
<property name="CookingTime" column="CookingTime" type="TimeSpan"/>
<property name="OvenTemperature" column="OvenTemperature" type="Application.Data.UserTypes.TemperatureType, Application.Data"/>
<one-to-one name="Content" class="Application.Core.Domain.Content, Application.Core" constrained="true" access="field.camelcase-underscore"/>
</class>
</hibernate-mapping>
<one-to-one name="Person" class="Person"/>
<one-to-one name="Employee" class="Employee" constrained="true"/>
<class name="Person" table="PERSON">
<id name="Id" column="PERSON_ID">
<generator class="foreign">
<param name="property">Employee</param>
</generator>
</id>
...
<one-to-one name="Employee"
class="Employee"
constrained="true"/>
</class>