hibernate-reactive
hibernate-reactive copied to clipboard
Map as json column
It seems that a common use case is to add a field of type Map to an entity and then save it as a json column when a database supports it (PostgreSQL for example).
One can achieve this using a converter. For example:
@Convert(converter = MapJsonConverter.class)
private Map<String, Object> content;
The converter is trivial:
public class MapJsonConverter implements AttributeConverter<Map, JsonObject> {
@Override
public JsonObject convertToDatabaseColumn(Map map) {
return new JsonObject(map);
}
@Override
public Map convertToEntityAttribute(JsonObject jsonObject) {
return jsonObject.getMap();
}
}
@gavinking Would it make sense to have a built-in type for this?
Something like:
@Type(name="mapAsJson")
private Map<String, Object> content;
Well, I'm not very keen on adding built-in types to HR that don't exist in Hibernate itself...
Sure, but JsonObject is a vert.x thing
And a user can already create a field of type JsonObject with Hibernate Reactive. The problem is that, in practice, it's not very useful because when using something like RestEasy, the default conversion of a Json is a map.
This is not a critical issue but I think it would make users' life better.
Also, for ORM it's possible to add this mapping via hibernate-types but there is no equivalent for Hibernate Reactive