在只读同义词上禁用Hibernate自动更新刷新 [英] Disable Hibernate auto update on flush on read only synonyms

查看:141
本文介绍了在只读同义词上禁用Hibernate自动更新刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表和两个具有相同表的数据库,但其中一个是另一个的符号链接,只能在此表上读取。



我使用Hibernate将表映射到Java,我使用spring根据一些输入条件将Entity Manager的数据源设置为两个数据库之一。



当我连接到第二个数据库时,我只调用只读操作(选择),但似乎Hibernate尝试将某些东西刷新回数据库,并且它无法告诉更新是否被允许在这个视图。



如何禁用此更新仅用于第二个数据源并保持第一个正常?



更新:
查看堆栈跟踪,似乎在这里开始刷新:

 

org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate .impl.SessionImpl.flush(SessionImpl.java:1027)org.hiber中的

at org.hibernate.ejb.AbstractEntityManagerImpl $ 1.beforeCompletion(AbstractEntityManagerImpl.java:504)
... 55 more

这与hibernate.transaction.flush_before_completion属性有关吗?我可以将它设置为false为第二个数据源吗?

解决方案

很可能你的实体变得脏从数据库加载,Hibernate认为它需要存储更改。如果您的访问器(get和set方法)不返回由Hibernate设置的完全相同的值或引用,则会发生这种情况。



在我们的代码中,这发生在列表中,开发者创建了新的列表实例,因为他们不喜欢他们在setter中获得的类型。 b

如果您不想更改代码,请将映射更改为字段访问权限。



您还可以阻止Hibernate存储更改设置FlushMode永远不会在会话中,但这只能隐藏在其他情况下仍会发生的真正问题,会导致不必要的更新。


I have a table and two databases which have the same table, but one is a symlink of the other one and only read is permitted on this table.

I have mapped the table to Java using Hibernate and I use spring to set the Entity Manager's data source as one of the two databases based on some input criteria.

I call only read only operations (selects) when I am connected to the second database, but it seems Hibernate tries to flush something back to the database and it fails telling update is not allowed on this view.

How do I disable this update only for the second datasource and keep it normal for the first one?

Update: Looking at the stack trace, the flush seems to be started here:


          at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
          at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
          at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
          at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
          at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
          ... 55 more

Is this related to hibernate.transaction.flush_before_completion property? Can I set it to false for the second data source?

解决方案

Most probably your entities become "dirty" the same moment they are loaded from the database, and Hibernate thinks that it needs to store the changes. This happens, if your accessors (get and set methods) are not returning the exact same value or reference that had been set by Hibernate.

In our code, this happened with lists, developers created new list instances because they didn't like the type they got in the setter.

If you don't want to change the code, change the mapping to field access.

You can also prevent Hibernate of storing changes by setting FlushMode to never on the session, but this only hides the real problem which will still occur in other situations an will lead to unnecessary updates.

这篇关于在只读同义词上禁用Hibernate自动更新刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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