Java Hibernate:检查哪些实体的字段被修改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19656882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:11:18  来源:igfitidea点击:

Hibernate: check which entity's fields are modified

javahibernatedesign-patternsjpaarchitecture

提问by VB_

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.

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

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. Not all users can modify all fields
  2. I should notify specific users in specific ways on some fields change.
  1. 并非所有用户都可以修改所有字段
  2. 我应该在某些字段更改时以特定方式通知特定用户。

What a problem:

有什么问题:

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

我得到非常难看的代码。实际上,我遍历所有字段/集合并调用equals方法。

Question:

题:

May be Hibernate provide more elegant way to check what fields have been modified? How?

可能是 Hibernate 提供了更优雅的方式来检查哪些字段已被修改?如何?

P.S.

聚苯乙烯

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

@victorantunes 提供了一个解决方案,但对我来说似乎太全面了。可能是一些替代品?

采纳答案by Alok

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.htmlso 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.

您可以做的是制作一个 Hibernate Interceptor,它在创建、修改和更新等事件中充当触发器。 http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.html以便给定实体之前的任何一点即将被修改和持久化,1.您可以检查用户是否有访问(您可以从会话或数据库中获取用户名)以修改特定字段,因此您可以授予访问权限以保存或更新。2.只有当实体被修改时,您才能通知其他用户。

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();

通过这种方式,您可以在 Spring 的 Hibernate 4 Session session = s.withOptions().interceptor(new YourInterceptor().openSession(); 的实现中创建一个新的会话范围拦截器;

回答by victorantunes

If what you want is to be able to check which fields have been modified, it may be worth taking a look at Hibernate Envers, which works as an auditing tool that creates a separate table containing the changes that were made to your business logic table.

如果您想要的是能够检查哪些字段已被修改,那么可能值得一看 Hibernate Envers,它用作审计工具,可创建一个单独的表,其中包含对您的业务逻辑表所做的更改。

http://www.jboss.org/envers

http://www.jboss.org/envers

I started using it for some internal auditing and it's really simple and works like a charm.

我开始使用它进行一些内部审计,它真的很简单,而且很有魅力。

Here's a quick tutorial:

这是一个快速教程:

https://thenewcircle.com/s/post/115/easy_auditing_versioning_for_your_hibernate_entities_with_envers

https://thenewcircle.com/s/post/115/easy_auditing_versioning_for_your_hibernate_entities_with_envers

回答by Bimalesh Jha

I think Hibernate interceptors can be used http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.htmlyou can compare old and new values of the entity fields.

我认为可以使用 Hibernate 拦截器 http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.html你可以比较实体字段的新旧值。