I’m using a Oracle database, version > 21c. I used to have a 1→N relation: 1 entity with N fields. In order to reduce the number of inserts, I decided to put those fields in a JSON column.
In my entity, I have decorated the fields object with @JdbcTypeCode(SqlTypes.JSON).
In order to batch the inserts and still have a surrogate key, I use a SequenceGenerator with pooled algorithm optimization, with the default allocationSize of 50.
Everything works: the records are inserted correctly and in batch, but I’ve noticed that every insert is followed by an update on thee whole entities. Even with @DynamicUpdate on the entity, it still updates the JSON property and the id.
This behavior does not occur if I comment out the JSON property.
Is this correct? It’s annoying because the extra update causes an unnecessary extra round trip to the DB.
Is the only solution to avoid this situation to use a Native Query and batch insert, bypassing Hibernate?
You need to correctly implement equals() for the data type that you store as JSON to avoid Hibernate ORM treating the value as dirty, or better yet, make the data type an @Embeddable. Also see the documentation about aggregate embeddables.
Hi, and thank you for the suggested solution.
Implementing equals for the data type I intended to store successfully resolved the issue with the unnecessary update.
However, I wasn’t able to make the data type an @Embeddable. When I annotate it as such, Hibernate throws the following exception during persistence:
org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [java.lang.Boolean ] by reflection for persistent property
(where <field package name> refers to the fully qualified path of the field).
I should probably have mentioned earlier that the field I’m trying to serialize as JSON is actually a list of this data type.
As suggested, I reviewed the documentation on aggregate embeddables and the entire Domain Model chapter (sections 3.1–3.6), but I couldn’t find any indication of how to properly define this data type as an @Embeddable. It’s possible I’m missing something.