休眠:检查哪些实体的字段修改 [英] Hibernate: check which entity's fields are modified

查看:121
本文介绍了休眠:检查哪些实体的字段修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么:

What I have:

我已经休眠的实体,其中包含许多非瞬态字段,包​​括集合。用户可以一次更新单独的每个字段或某些字段组。

I've Hibernate entity, which contains many non-transient fields, including collections. User can update each field separately or some group of fields at once.

什么challange:

What a challange:

在处理,我应该检查哪些领域实体已被更改:

In handler I should check what field of the entity have been changed:

public void handle(Entity newVersion) {
  Session session = sessionFactory.openSession();
  Entity oldVersion = (Entity) session.get(Entity.class, entity.getId());
  List changedFields = compareChanges(oldVersion, newVersion);  //HOW TO CHECK WHICH FIELDS ARE CHANGED?
}

我想这样做,出于安全和通知的理由。意思是:

I want to do it for security and notification reasons. Means:


  1. 并不是所有用户都可以修改所有领域

  2. 我应该在某些领域通知的具体方式具体的用户更改。

什么问题:

What a problem:

我得到非常难看code。其实我遍历throught所有领域/收藏品和呼叫等于方法。

I get very ugly code. Actually I iterate throught all fields/collections and call equals method.

问:

Question:

必须是以下提供更优雅的方式来检查领域已被修改呢?怎么样?

P.S。

P.S.

@victorantunes提供了一个解决方案,但它似乎太COM prehensive我。可能是一些替代品?

@victorantunes provide a solution, but it seems too comprehensive for me. May be some alternatives?

推荐答案

你可以做的就是让一个Hibernate拦截器将像一个触发像创建,修改和更新的事件。
<一href=\"http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.html\">http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.html
使得该给定实体之前的任何点是将要被修改,并且持续存在,
1.You可以检查用户是否具有访问(你可以从会话或数据库中获取用户名)修改特定的领域,因此您可以授予访问保存或更新。
2.You可以通知其他用户关于只有当实体被修改

What you can do is make a Hibernate Interceptor that would act like a trigger in the events like create, modify and update. http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.html so that any point before the given entity is about to be modified and persisted, 1.You can check whether the user has the access (you can get username from session or database) to modify the particular field and accordingly you can grant the access to save or update. 2.You can notify the other user about only when the entity is modified.

通过这种方式可以使一个新的会话拦截范围在春天的实现ofHibernate 4
。会话的会话= s.withOptions()拦截器(新YourInterceptor()的openSession();

By this way you can make a new session-scope Interceptor in spring's implementation ofHibernate 4 Session session = s.withOptions().interceptor(new YourInterceptor().openSession();

这篇关于休眠:检查哪些实体的字段修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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