hibernate-reactive icon indicating copy to clipboard operation
hibernate-reactive copied to clipboard

Map as json column

Open DavideD opened this issue 2 years ago • 4 comments

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;

DavideD avatar Feb 04 '22 12:02 DavideD

Well, I'm not very keen on adding built-in types to HR that don't exist in Hibernate itself...

gavinking avatar Feb 04 '22 12:02 gavinking

Sure, but JsonObject is a vert.x thing

DavideD avatar Feb 04 '22 12:02 DavideD

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.

DavideD avatar Feb 04 '22 12:02 DavideD

Also, for ORM it's possible to add this mapping via hibernate-types but there is no equivalent for Hibernate Reactive

DavideD avatar Feb 04 '22 12:02 DavideD