休眠:永远不会调用 MyInterceptor#onFlushDirty

编程入门 行业动态 更新时间:2024-10-21 03:37:16
本文介绍了休眠:永远不会调用 MyInterceptor#onFlushDirty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题: 为什么 MyInterceptor#onFlushDirty 永远不会被调用?

Question: Why MyInterceptor#onFlushDirty is never called?

我在像

<bean id="myEntityManagerFactory" parent="abstractEntityManagerFactoryBean" abstract="true"> <property name="entityInterceptor"> <bean class="xxxx.MyInterceptor"/> </property> </bean> <bean id="abstractEntityManagerFactoryBean" class="xxxx.MyEntityManagerFactoryBean"/>

MyEntityManagerFactoryBean

public class MyEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean implements LoadTimeWeaverAware { private Interceptor entityInterceptor; public Interceptor getEntityInterceptor() { return entityInterceptor; } public void setEntityInterceptor(Interceptor interceptor) { entityInterceptor = interceptor; } }

我的拦截器:

public class MyInterceptor extends EmptyInterceptor { public MyInterceptor() { System.out.println("init"); // Works well } // PROBLEM - is never called @Override public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { if (entity instanceof File) { ..... } return false; } }

更新:[解释为什么自定义脏策略看起来不像我的方式]

每次我更改 Folder 实体中的某些内容时,我都希望更新 modified 时间戳,除了 folderPosition.同时folderPosition 应该是持久的而不是短暂的(意味着导致实体变脏).

I want update modified timestamp each time I change something in Folder entity EXCEPT folderPosition. In the same time folderPosition should be persistent and not transient (means cause entity to be dirty).

由于我使用 Spring Transactional 和 Hibernate 模板,所以有一些细微差别:

Due I use Spring Transactional and Hibernate Templates, there is some nuances:

1) 我无法在每个 setter 的末尾更新修改后的时间戳,例如:

1) I can't update modified timestamp at the end of each setter like:

public void setXXX(XXX xxx) { //PROBLEM: Hibernate templates collect object via setters, //means simple get query will cause multiple 'modified' timestamp updates this.xxx = xxx; this.modified = new Date(); }

2) 我不能手动调用 setModified,因为它有大约 25 个字段,并且每个字段的 setXXX 分散在整个应用程序中.而且我没有能力进行重构.

2) I can't call setModified manually, because it has about 25 fields, and setXXX for each field is scattered across whole app. And I have no power to make refactoring.

@Entity public class Folder { /** * GOAL: Changing of each of these fields except 'folderPosition' should cause * 'modified' timestamp update */ private long id; private String name; private Date created; private Date modified; private Integer folderLocation; @PreUpdate public void preUpdate() { //PROBLEM : change modified even if only location field has been changed! //PROBLEM: need to know which fields have been updated! modified = new Date(); } .... }

推荐答案

您需要扩展 findDirty 方法而不是 onFlushDirty.检查本教程的详细说明,并参考了一个 GitHub 工作示例.

You need to extend the findDirty method not onFlushDirty. Check this tutorial for a detail explanation with a reference to a GitHub working example.

更多推荐

休眠:永远不会调用 MyInterceptor#onFlushDirty

本文发布于:2023-11-26 20:56:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:永远不会   MyInterceptor   onFlushDirty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!