[英]c# nhibernate id automatically reset on delete or not?
关于使用Nhibernate删除实体的不一致(在我的程序中)我有一个问题。
我必须存储绑定它们的实体和二元关系。
这是模型:
public interface IPersistent
{
int ID { get ; set;}
}
public virtual class Entity : IPersistent {
//relations where the entity appears on the left side
public ISet<Relation> LeftRelations { get;}
//relations where the entity appears on the right side
public ISet<Relation> RightRelations { get;}
}
public virtual class Relation : IPersistent {
public Entity LeftEntity { get;}
public Entity RightEntity { get;}
public RelationType Type { get;}
// must be called before ISession.Delete()
public void ClearRelation(){
LeftEntity.LeftRelations.Remove(this);
RightEntity.RightRelations.Remove(this);
LeftEntity = null;
RightEntity = null;
}
}
我的映射看起来像:
<class name="Entity" table="entity">
<id column="id_entity" name"ID" unsaved-value="-1" type=Int32>
<generator class ="native">
<param name ="sequence">seq_task_id_entity</param>
</generator>
</id>
<set name="LeftRelations" table ="relation" generic="true"
cascade="all" inverse="true">
<key column="left_entity" foreign-key="id_entity"/>
<one-to-many class ="Relation"/>
</set>
<set name="RightRelations" table ="relation" generic="true"
cascade="all" inverse="true">
<key column ="right_entity" foreign-key ="id_entity"/>
<one-to-many class ="Relation"/>
</set>
</class>
<class name="Relation" table="relation" >
<id column="id_relation" name"ID" unsaved-value="-1" type=Int32>
<generator class ="native">
<param name ="sequence">seq_task_id_relation</param>
</generator>
</id>
<many-to-one name ="LeftEntity" class="Entity" cascade="all"
update="true" not-null="true">
<column name ="left_entity" sql-type="integer" not-null ="true"></column>
</many-to-one>
<many-to-one name ="RightEntity" class ="Item" cascade="all"
update="true" not-null="true">
<column name ="right_entity" sql-type="integer" not-null ="true"></column>
</many-to-one>
</class>
我可以持久化实体和关系实例。 当我删除持久化实体(未绑定到任何关系)时,其ID属性在提交后更新为unsaved-value。 但是当我删除持久关系时,数据库中的关系行被删除,但关系'ID属性不会重置为unsaved-value。
这种行为是否正常? 或者这是我的nh映射不正确?
要回答这个问题:
但是当我删除持久关系时,数据库中的关系行被删除,但关系'ID属性不会重置为unsaved-value 。
unsaved-value
是用于比较的设置。 它不打算设定价值 。
NHibernate只需要这个设置来理解传递给ISession的实体是否应该被视为Transient或在DB中存在。
unsaved-value
只是一个常量设置 - 仅用于比较和决策:是对象瞬态或现有的。
文档:
<id
name="PropertyName" (1)
type="typename" (2)
column="column_name" (3)
unsaved-value="any|none|null|id_value" (4)
access="field|property|nosetter|ClassName(5)">
...
...
(4)unsaved-value
(可选 - 默认为“合理”值) :标识符属性值,指示实例是新实例化的(未保存),将其与在先前会话中保存或加载的瞬态实例区分开来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.