Hibernate updates entity with JsonBinaryType although no change

131 Views Asked by At

When inside a transaction, using optimistic locking, an entity with this property:

 @Type(JsonBinaryType::class)
 @Column(name = "errors")
 var verrors: Map<String, List<Error>> = emptyMap()

the object revision on DB is always raised if there is content.

Strange is, that this only happens, when also inside a session-in-view coming from controller. Not when called directly.

Using Hibernate 6.3, hypersistence-utils-hibernate-63:3.7.0, Spring Boot 3.2.

After further analysis the root cause is that Hibernate finds the property to be dirty due to comparing the "old" string-only DB-saved Map-List structure with the "new" object-based structure (i.e. there is an Error object as value) in the session.

Expected: Object should not be updated. This is e.g. the case when making the transaction readOnly.

1

There are 1 best solutions below

0
Strinder On BEST ANSWER

It seems that Hypersistence Utils can only map simple Map<String,String> since the suggestion to implement equals/hashCode didn't also help.

After further research the solution is pretty simple, since Hibernate 6 supports this finally. So no need for Hypersistence Utils - just use:

import org.hibernate.annotations.JdbcTypeCode
import org.hibernate.type.SqlTypes
...
 @JdbcTypeCode(SqlTypes.JSON)
 @Column(name = "errors")
 var verrors: Map<String, List<Error>> = emptyMap()

and it works also for complex structures like this.