如何查找被引用的对象是否可以被删除? [英] How to find if a referenced object can be deleted?

查看:207
本文介绍了如何查找被引用的对象是否可以被删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Customer的对象,它将在其他表中用作外键。



问题是我想知道是否可以删除一个客户(也就是说,它没有在其他表中引用)。 b
$ b

这对Nhibernate是可能的吗?

解决方案

在所引用表FK列中的 Customer PK值。
有很多方法可以去做:


  1. 如同kgiannakakis指出的那样,尝试删除if抛出的异常是回滚。有效但丑陋,没有用处。这也要求您在数据库中设置CASCADE =RESTRICT。此解决方案的缺点是您必须尝试删除对象,以确定您不能


  2. 映射引用客户作为集合,然后对于每个集合,如果它们的 Count> 0 然后不允许删除。这是很好的,因为只要映射完成,这对于模式更改是安全的。这也是一个不好的解决方案,因为额外的选择将不得不作出。

  3. 有一个方法执行查询,如 bool IsReferenced(客户定制)。好,因为你可以有一个单一的查询,你会用你想要的。不太好,因为它可能容易出错,由于模式和/或域的变化(取决于你将要做的查询的类型:sql / hql / criteria)。

  4. 这个类的计算属性与它自己的映射元素像< property name =IsReferencedtype =longformula =sql-query引用表/> 。好,因为它是一个快速解决方案(至少和你的数据库一样快),不需要额外的查询。不太好,因为它容易出现模式的变化,所以当你改变你的数据库时,你不能忘记更新这个查询。

  5. 疯狂的解决方案:创建一个模式使得计算的束缚观点。在你想要的时候查询。好,因为它的模式绑定,不太容易模式更改,好,因为查询是快速的,不太好,因为你还需要做一个额外的查询(或者你在解决方案4映射这个视图的结果。)




<2,3,4也很好,因为您也可以将此行为投影到您的用户界面(不允许删除)



就我个人而言,我会选择4,3,5这个偏好


I have an object called "Customer" which will be used in the other tables as foreign keys.

The problem is that I want to know if a "Customer" can be deleted (ie, it is not being referenced in any other tables).

Is this possible with Nhibernate?

解决方案

What you are asking is to find the existence of the Customer PK value in the referenced tables FK column. There are many ways you can go about this:

  1. as kgiannakakis noted, try to do the delete and if an exception is thrown rollback. Effective but ugly and not useful. This also requires that you have set a CASCADE="RESTRICT" in your database. This solution has the drawback that you have to try to delete the object to find out that you can't

  2. Map the entities that reference Customer as collections and then for each collection if their Count > 0 then do not allow the delete. This is good because this is safe against schema changes as long as the mapping is complete. It is also a bad solution because additional selects will have to be made.

  3. Have a method that performs a query like bool IsReferenced(Customer cust). Good because you can have a single query which you will use when you want. Not so good because it may be susceptible to errors due to schema and/or domain changes (depending on the type of query you will do: sql/hql/criteria).

  4. A computed property on the class it self with a mapping element like <property name="IsReferenced" type="long" formula="sql-query that sums the Customer id usage in the referenced tables" />. Good because its a fast solution (at least as fast as your DB is), no additional queries. Not so good because it is susceptible to schema changes so when you change your DB you mustn't forget to update this query.

  5. crazy solution: create a schema bound view that makes the calculation. Make the query on it when you want. Good because its schema-bound and is less susceptible to schema changes, good because the query is quick, not-so-good because you still have to do an additional query (or you map this view's result on solution 4.)

2,3,4 are also good because you can also project this behavior to your UI (don't allow the delete)

Personally i would go for 4,3,5 with that preference

这篇关于如何查找被引用的对象是否可以被删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆