如何知道什么使得hibernate持久化对象变脏? [英] How to know what made a hibernate persisted object dirty?

查看:165
本文介绍了如何知道什么使得hibernate持久化对象变脏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用hibernate映射的对象有奇怪的行为。为了知道为什么对象行为奇怪,我需要知道什么使该对象变脏。有人可以帮忙,给我一个提示吗?



该对象是Java / Spring上下文中的Java类。所以我更喜欢一个针对Java平台的答案。



编辑:我想要访问Hibernate的脏状态,以及它在连接到会话的对象上的变化。我不知道一段代码如何帮助。



对于实际的问题:在由Transaction TransactionManager管理的事务内部,我对对象执行一些(读取)查询,而不对这些对象进行显式保存,它们将被保存由TransactionManager因为Hibernate认为这些(而不是全部)是脏的。现在我需要知道为什么Hibernate认为这些对象是脏的。

解决方案

我会使用拦截器。 onFlushDirty方法获取当前和之前的状态,以便您可以比较它们。实现Interceptor接口并扩展EmptyInterceptor,覆盖onFlushDirty。然后使用configuration.setInterceptor添加一个类的实例(Spring可能需要你这样做不同)。您也可以在会话中添加拦截器,而不是启动。



这是关于拦截器的文档。


An object I mapped with hibernate has strange behavior. In order to know why the object behaves strangely, I need to know what makes that object dirty. Can somebody help and give me a hint?

The object is a Java class in a Java/Spring context. So I would prefer an answer targetting the Java platform.

Edit: I would like to gain access to the Hibernate dirty state and how it changes on an object attached to a session. I don't know how a piece of code would help.

As for the actual problem: inside a transaction managed by a Spring TransactionManager I do some (read) queries on Objects and without doing an explicit save on these Objects they are saved by the TransactionManager because Hibernate thinks that some of these (and not all) are dirty. Now I need to know why Hibernate thinks those Objects are dirty.

解决方案

I would use an interceptor. The onFlushDirty method gets the current and previous state so you can compare them. Implement the Interceptor interface and extend EmptyInterceptor, overriding onFlushDirty. Then add an instance of that class using configuration.setInterceptor (Spring may require you to do this differently). You can also add an interceptor to the session rather than at startup.

Here is the documentation on interceptors.

这篇关于如何知道什么使得hibernate持久化对象变脏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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