Search code examples
hibernateenums

Using Enum in Hibernate causes select followed by an update statement


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


Solution

  • 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.