I have a problem concerning an inconsistency (in my program) on entity deletion using Nhibernate.
I have to store entities and binary relations that bond them.
Here is the model:
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;
}
}
my mapping looks like:
<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>
I can persist both entity and relation instances. When I delete a persisted entity (which is not bound to any relation), its ID property is updated to the unsaved-value after commit. 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.
Is this behavior normal? Or it is my nh mapping that is incorrect?