c# nhibernate id 是否在删除时自动重置?
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 属性 在提交后更新为未保存的值。
但是当我删除持久关系时,数据库中的关系行被删除,但是关系'ID 属性 没有重置为未保存的值。
这种行为正常吗?还是我的 nh 映射不正确?
要回答这个问题:
But when I delete a persisted relation, the relation row in database is deleted, but the relation'ID property is not resetted to the unsaved-value.
unsaved-value
是用来比较的设置。 不是设置值。
NHibernate 只需要这个设置来了解传递给 ISession 的实体是否应该被视为 Transient 或 Existing in DB。
The unsaved-value
is just a constant setting - used just for comparison and decision: is object transient or existing.
文档:
<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
(optional - defaults to a "sensible" value): An identifier property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from transient instances that were saved or loaded in a previous session.
我有一个关于使用 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 属性 在提交后更新为未保存的值。 但是当我删除持久关系时,数据库中的关系行被删除,但是关系'ID 属性 没有重置为未保存的值。
这种行为正常吗?还是我的 nh 映射不正确?
要回答这个问题:
But when I delete a persisted relation, the relation row in database is deleted, but the relation'ID property is not resetted to the unsaved-value.
unsaved-value
是用来比较的设置。 不是设置值。
NHibernate 只需要这个设置来了解传递给 ISession 的实体是否应该被视为 Transient 或 Existing in DB。
The
unsaved-value
is just a constant setting - used just for comparison and decision: is object transient or existing.
文档:
<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
(optional - defaults to a "sensible" value): An identifier property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from transient instances that were saved or loaded in a previous session.