本文介绍了是什么导致休眠生成更新子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate从View(定义如下)中选择数据:

I am using Hibernate to select data from a View (defined as follows) :

from test where trim(id)='1111'

但是hibernate生成以下 update子句:

but hibernate generates the following update clause:

update test set id=?, dd=? where id='1111'

有人有什么建议吗?看来这是Hibernate的错误?

Anyone has any suggestion ? It seems it's a bug of Hibernate?

推荐答案

Hibernate有时可以在后台为您生成更新!

例如,如果对象的状态与数据库不同步,则会发生这种情况.您可以根据需要覆盖此行为(请参见下面的第二个项目符号).但是通常,它可能会影响应用程序逻辑中的一些错误.

This happens, for example, if an object's state is out of sync with the database. You can override this behaviour (see the 2nd bullet below) as necessary... But often times it can effect some mistakes in your application's logic.

您的情况:可能有两种原因:

1)Hibernate会根据您将其作为输入发送的HQL,对公共方法(即getter和setter)进行自省以设置映射文件中的字段.那么...这对您的问题有何影响?好吧... 如果您的Java Bean的吸气剂中包含一些复杂的逻辑,那么在最终执行此调用时,由于这些方法已被调用,该逻辑可能会导致休眠状态生成一些更新.

1) Hibernate introspects the public methods (i.e. getters and setters) to set the fields in your mapping file, based on the HQL you send it as input. So... how does this effect your issue ? Well... if there is some sophisticated logic in your getters for your java beans, that logic could be causing hibernate to generate some updates, when you ultimately do this call, since those methods are being invoked.

2)在某些已知的极端情况下,当您调用select语句(即不道德的交易)时,Hibernate确实会进行更新.有时可以通过在.hbm.xml文件中的适当位置设置"update = false"来解决这些问题.

2) There are some known corner cases where Hibernate does updates when you call select statements (i.e. dirty transactions). These can be fixed sometimes by setting "update=false" in your .hbm.xml files, in the appropriate places ...

另请参见:在Hibernate中使用枚举导致选择后跟一条更新语句

这篇关于是什么导致休眠生成更新子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:51