在Hibernate中使用枚举会导致select,后跟update语句 [英] Using Enum in Hibernate causes select followed by an update statement

查看:228
本文介绍了在Hibernate中使用枚举会导致select,后跟update语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有枚举属性的映射实体。
每当我在这样的实体上运行select语句时,通过登录日志文件,结果就是紧随其后的更新。
例如,如果我的结果集包含100条记录,那么我有:

  [INFO org ... select。 $] 
[INFO org ... update ... where id =?]
[INFO org ... update ... where id =?]

....重复100次



如果我将该属性标记为update =假问题消失了。
枚举是通过一个枚举转换器类分配的,我从一本着名的书中复制了这个类。
所以我不知道我是否只是复制和粘贴代码。这里是如何在hbm文件中声明的。

 < typedef class =mypackage.HbnEnumConvertername =the_type> ; 
< param name =enumClassname> mypackage.Type< / param>
< / typedef>你可以指出一个方向调查这个吗?

另外,在hibernate字段中有update = false的后果是什么?



谢谢

解决方案

如果对象标记为脏,Hibernate将生成更新。



查看日志,看看hibernate是否将对象标记为脏。



如果您有类似于下面的内容。

  class Entity {

public YourEnum getEnum(){
return yourEnum == null ?YourEnum.SOME_VALUE:......;
}
}

或者也许在您返回的Enum.equals方法中false等于(null,null)case?



equals方法应该返回true,如果比较的对象是相同的,否则为hibernate使用equals方法来决定对象是否是脏或不脏。


I have a mapped entity wich has an enum property. By loking at log file, whenever I run a select statement on such entity, the result is an immediately following update. For example if my result set contains 100 records, then I have:

[INFO org... select...]
[INFO org... update... where id=?]
[INFO org... update... where id=?]

.... repeated 100 times

If I mark the property as update=false the problem disappear. The enum is assigned trough an enum converter class, which I copied from a well known book. So I don't know if I just copy and paste the code. Here it is how is declared on hbm file.

<typedef class="mypackage.HbnEnumConverter" name="the_type">
    <param name="enumClassname">mypackage.TheType</param>
</typedef>

Can you point out a direction to investigate this ? Beside, what are the consequences of having update=false on hibernate field ?

thanks

解决方案

Hibernate will generate an update if your object marked as dirty.

See logs to see if hibernate marks your object as dirty.

if you have something similar to below.

 class Entity{

   public YourEnum getEnum() {
    return yourEnum==null?YourEnum.SOME_VALUE:....;
   }
 }

Or maybe in your Enum.equals method you are returning false for probably equals(null,null) case?

equals method should return true if the compared objects are same and false otherwise as hibernate uses equals method to decide if the object is dirty or not.

这篇关于在Hibernate中使用枚举会导致select,后跟update语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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