休眠检查删除约束 [英] Hibernate check deletion constraints

查看:68
本文介绍了休眠检查删除约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring-Hibernate来控制应用程序中的模型。这些模型相互链接(一对多,多对多,几乎各种关系),现在删除一个由其他实体使用的实体时出现问题。问题是我想显示详细消息,以准确告知其他哪些对象(类型,名称)正在使用我要删除的实体。

例如: Car->人,房子->人;然后,当我删除一个有汽车和房屋的人时,消息将显示有与该人相关的汽车(名为福特野马)和房屋(名为MyHouse)。

1.在那里Hibernate的任何方法都支持此要求吗?我想没有针对此特定要求的实现。

2.如果没有任何可用于该问题的实用程序,我正在考虑以下解决方案:

-在每个实体类中(即Person) ),我将定义所有检查方法,以检测从该实体到其他实体的链接,例如:

I am using Spring - Hibernate to control models in my application. The models are linked to each other (one-to-many, many-to-many, almost kinds of relationships), and now I have a problem when deleting one entity which is being used by other entities. The problem is I want to show the detail message that informs exactly what other objects (type, name) are using the entity that I am going to delete. It's not the common message about Constraint violation that Hibernate throws me.
For example: Car --> Person, House --> Person; then when I delete one Person who has a car and house, the message will show "There are Car (named Ford Mustang) and House (named MyHouse) linked to this Person".
1. So is there any method from Hibernate support this requirement? I guess there's no implementation for this specific requirement.
2. If not any utility available for this problem, I am thinking about below solutions:
- in each entity class (i.e Person), I will define all checking method that detect linking from the this entity to other entities, for example:

class Person {
     // Properties
     // Checking methods, return type and name of linked objects
     public Map<String, String> getLinkedCars() {
         // Query to DB to get linked cars
         // return a Map contain Class name along with object name <Car, Ford Mustang>
     }

     public Map<String, String> getLinkedHouses() {
         // Query to DB to get linked houses
         // return a Map contain Class name along with object name <House, MyHouse>
     }
}

-然后在删除Person实体之前在服务中,我将使用反射机制从检查方法(其名称以 getLinkedXXX开头)中收集结果,并生成详细错误消息。

那么,该解决方案好吗?关于性能以及MVC的约定(因为我必须在模型类内部查询数据)?

感谢您的帮助。

-and then, in service before deleting Person entity, I will use reflection mechanism to collect results from checking methods (whose name is started with "getLinkedXXX"), and build the detail error messages.
So is this solution good? About the performance, and the convention of MVC (because I have to query data inside model class)?
Thank you for your help.

推荐答案

一种(不是那么简单)的方法是扫描您的实体类以查找 @OneToMany @ManyToMany 带注释的字段并执行检查,因此可以向用户提供整洁的错误消息。以下示例代码假定您仅注释字段,而不注释getters方法,例如:

One (not so simple) approach is to scan your entity class for @OneToMany or @ManyToMany annotated fields and perform checking so neat error message can be provided to user. Following sample code assumes you only annotate the field, not the getters method, eg:

public class Person {
  @OneToMany(..)
  private List<House> houses;
  //...
}

首先获取所有字段的列表使用反射:

First get the list of all fields using reflection:

Fields[] fields = Person.class.getDeclaredFields();

然后迭代并检查 @OneToMany @ManyToMany 批注

for(Field f : fields) {
   if( f.getAnnotation(OneToMany.class) != null || 
       f.getAnnotation(ManyToMany.class) != null) {
     // Here you know f has to be checked before the person is deleted ...
   }
}

字段的值可以使用以下方式获得特定人物对象的图像:

The value of a field of a particular person object can be obtained using:

Person p = // fetch a person ..
Field f = // assume f is the "List<House> houses" field
List<House> houses = (List<House>) f.get(p);

这篇关于休眠检查删除约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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