在 Hibernate 中使用 Enum 会导致 select 后跟更新语句
我有一个具有枚举属性的映射实体。 通过查看日志文件,每当我在此类实体上运行 select 语句时,结果都是立即更新。 例如,如果我的结果集包含 100 条记录,那么我有:
[INFO org... select...]
[INFO org... update... where id=?]
[INFO org... update... where id=?]
.... 重复 100 次
如果我将属性标记为 update=false 问题就会消失。 枚举是通过枚举转换器类分配的,我从一本著名的书中复制了该类。 所以我不知道是否只是复制并粘贴代码。这是在 hbm 文件中声明的方式。
<typedef class="mypackage.HbnEnumConverter" name="the_type">
<param name="enumClassname">mypackage.TheType</param>
</typedef>
你能指出一个调查这个问题的方向吗?此外,在 hibernate 字段上 update=false 会产生什么后果?
谢谢
I have a mapped entity which has an enum property.
By looking 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 through 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的对象标记为脏,Hibernate 将生成更新。
查看日志以查看休眠状态是否将您的对象标记为脏。
如果你有类似下面的东西。
或者也许在您的 Enum.equals 方法中,您可能为 equals(null,null) 情况返回 false ?
如果比较的对象相同,则 equals 方法应返回 true,否则返回 false,因为 hibernate 使用 equals 方法来确定对象是否脏。
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.
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.