获取原始实体触发的休眠事件 [英] Get original entity triggered the Hibernate Event

查看:90
本文介绍了获取原始实体触发的休眠事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Hibernate上使用带有JPA 2的Spring Boot 2.我想记录一些审核信息,但不仅要记录实际实体,还要记录有关父实体的信息.

I'm using Spring Boot 2 with JPA 2 on top of Hibernate. I would like to log some auditing information but not only for the actual entity but also about the parent entity.

例如:我有一个Employee实体,该实体具有Address实体.因此,如果Address实体发生更改,我也想记录Employee已更改. Employee是被保存的实体,即使它不脏.

For example: I have an Employee entity that has an Address entity. So if the Address entity changes I also want to log that the Employee was changed. The Employee was the entity that was saved even if it is not dirty.

我发现了一些类似的问题: Hibernate版本控制父实体 仅当子实体了解父实体时,此方法才有效.

I have found some similar problems: Hibernate versioning parent entity This works only if the child entity knows about the parent.

我还发现了这一点: https://thoughts-on-java.org/hibernate-tips-increase-version-parent-entity-updating-child-entity/ 该解决方案是针对一种情况的具体实现,但我想要一个通用解决方案.

I also found this: https://thoughts-on-java.org/hibernate-tips-increase-version-parent-entity-updating-child-entity/ This solution is a concrete implementation for one case, but I would like a generic solution.

我的想法是找到原始实体,如果它的任何子实体很脏,那么也要标记原始实体.

My idea was to find the original entity and if any of it's child entities are dirty then mark the original entity as well.

推荐答案

我遇到了几乎相同的问题.我希望当子实体更改时,所属实体的"lastModifiedDate"和"Version"字段会更新.我正在使用Spring Data JPA审核.

I had almost the same problem. I wanted the owning entity's "lastModifiedDate" and "Version" fields update when the child entities change. I am using Spring Data JPA Auditing.

我已经测试了很多东西并实现了许多自定义代码.甚至我都在尝试实现自定义的脏检查机制.通过遵循以下

I have tested so many things and implemented lots of custom code. Even I was trying to implement a custom dirty check mechanism. I was able to fix parent versioning by following this blog post but nothing worked for auditing.

最后,使用休眠字节码增强Maven插件并启用 DirtyTracking 可以解决父版本控制和审核的问题.现在,当我编辑子级时,拥有实体的"lastModifiedDate"和版本"会自动更新,而无需任何自定义代码.请记住,我只测试过ManyToOne/OneToMany关系,但是我认为这也应该适用于其他关系.另外,我正在通过JpaRepository(使用Spring Data)从拥有方更新实体

Finally, Using Hibernate Bytecode Enhancement Maven plugin and enabling DirtyTracking fixed the issue of both parent versioning and auditing for me. Now the owning entity's "lastModifiedDate" and "version" updates by itself and without any custom code when I edit the child. Bear in mind that I have only tested ManyToOne/OneToMany relation but I think this should work for other relations as well. Also, I am updating the entity from the owning side via JpaRepository (using Spring Data)

要启用休眠字节码增强功能,请将此插件添加到您的POM文件中:

To enable Hibernate Bytecode Enhancement, add this plugin to your POM file:

<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${hibernate.version}</version>
<executions>
    <execution>
        <configuration>
            <failOnError>true</failOnError>
            <enableDirtyTracking>true</enableDirtyTracking>
            <enableAssociationManagement>false</enableAssociationManagement>
            <enableExtendedEnhancement>false</enableExtendedEnhancement>
            <enableLazyInitialization>false</enableLazyInitialization>
        </configuration>
        <phase>compile</phase>
        <goals>
            <goal>enhance</goal>
        </goals>
    </execution>
</executions>

现在,当您查找目标"文件夹时,实体现在通过使用SelfDirtinessTracker来实现 ManagedEntity和SelfDirtinessTracker ,这些实体会跟踪脏字段,因此无需休眠即可检查所有实体以查找脏对象那些.除了为我修复父级审核和版本控制外,这还可以提高性能.希望它也能解决您的问题.

Now when you look up the "target" folder, the entities now implement ManagedEntity and SelfDirtinessTracker by using SelfDirtinessTracker, the entities keep track of the dirty fields and there's no need for hibernate to check all entities to find dirty ones. This can also improve performance beside fixing the parent auditing and versioning for me. Hope it fixes your problem too.

这篇关于获取原始实体触发的休眠事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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