I have a situation where I'm using a table per subclass for association. The mappings are fine until I try and delete a record from the subclass table. The delete cause's not only the subclass to be deleted, but also the parent. I can understand this feature may be by design, but is there anyway to just dele开发者_开发技巧te the subclass?
Here's my sample code.
public class ParentClassMap : ClassMap<Parent>
{
public ParentClassMap ()
{
Table("tblParent");
Id(x => x.ParentId).Column("ParentId").GeneratedBy.Identity()
... other properties
}
}
public class ChildClassMap : SubClassMap<Child>
{
public ChildClassMap()
{
Table("tblChild");
KeyColumn("ParentId");
... other properties
}
}
Now when I query for a record, everything seems fine
Child child = session.CreateCriteria<Parent>().Add(Restrictions.Eq("ParentId", 1)).UniqueResult<Parent>();
But when I delete the child, the sql executed includes updates to all tables that reference either Parent or Child, then the deletion of Child then Parent.
session.Delete(child);
I only want to delete the Child Object, is this possible?
No, it's not possible.
Think about it in OOP terms: if you have an object of the class Dog
that inherits from Animal
, does it makes sense to "delete the dog, but leave the animal"?
Which brings me to the next point: if you can delete "part" of an object, then you should not be using a subclass, but an association (probably one-to-one or many-to-one)
精彩评论