In my Spring Boot application, I have an entity class like this:
public class MyEntity {
String id;
Map<String,String> paramsMap;
//Many members omitted
}
And the relevant JPA ORM XML snippet is
<entity class="MyEntity" access="FIELD">
<table name="myentity">
<!--couple of unique-constraints-->
</table>
<attributes>
<element-collection name="paramsMap" fetch="EAGER">
<collection-table name="my_entity_params">
<join-column name="id" />
</collection-table>
</element-collection>
</attributes>
</entity>
This approximates the answer to this question
The problem I have is that entities of class MyEntity
need to be updated, including the contents of the params
-map. When this happens, Oracle DB returns an exception indicating the primary key-unique constraint of table my_entity_params
has been violated (PostgreSQL silently fails to update some of the params).
An approximation of how the update happens is:
public void updateParam(String id, String paramName, String paramValue){
MyEntity old=repository.findById(id)
old.getParamsMap().put(paramName, paramValue);
repository.save(old);
}
Where repository
implements the interface PagingAndSortingRepository.
If paramsMap
isn't modified before calling save
, the update succeeds. I have tried first setting the paramsMap
, saving and then saving the actual map, which has resulted in the same error.
Using one-to-many
won't work because the map doesn't point to a complex type. In a similar vein, Eclipse foundation wiki's How to map collections of Basic or Emeddable values using an ElementCollection mapping article doesn't shed light on how to use element-collection with a map.
Inserting a map-key-column
element under element-collection
seems to fix the issue in Postgres, but the issue remains when connected to an Oracle database.
How do I get Hibernate to update the map contents correctly?