删除子类也会删除父类
我遇到的情况是每个子类使用一个表进行关联。在我尝试从子类表中删除记录之前,映射都很好。删除原因不仅是子类被删除,还包括父类。我可以理解这个功能可能是设计使然,但是有没有办法只删除子类呢?
这是我的示例代码。
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
}
}
现在,当我查询记录时,一切看起来都很好,
Child child = session.CreateCriteria<Parent>().Add(Restrictions.Eq("ParentId", 1)).UniqueResult<Parent>();
但是当我删除子项时,执行的 SQL 包括对引用父项或子项的所有表的更新,然后删除子项,然后删除父项。
session.Delete(child);
我只想删除子对象,这可能吗?
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 delete 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是不可能的。
用 OOP 术语思考一下:如果您有一个继承自
Animal
的Dog
类对象,那么“删除狗,但保留动物”是否有意义?这让我想到了下一点:如果您可以删除对象的“部分”,那么您不应该使用子类,而是使用关联(可能是一对一或多对-一)
No, it's not possible.
Think about it in OOP terms: if you have an object of the class
Dog
that inherits fromAnimal
, 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)