开发者

deleting a subclass, deletes the parent as well

开发者 https://www.devze.com 2023-01-28 03:06 出处:网络
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

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号