|
This issue seems to be a bit more serious. I'm trying to create a generic converter to store arbitrary fields as JSON instead of serialized LOBs. I don't need to be able to query on data in the field object and this makes it much easier to debug because the format will be identical to what goes over the REST API. The problem is that it cannot determine the class for T when T is either a standard or templated class (like List<Foo>)
Hibernate UserTypes have the same issue with templated types, but will work with standard objects. However, that requires creating empty classes to wrap all Lists/Sets/Maps and is Hibernate specific instead of generic JPA
public class JSONAttributeConverter<T> implements AttributeConverter<T, String> { private final Class<T> clazz;
@SuppressWarnings("unchecked") public JSONAttributeConverter() { clazz = (Class<T>) ((ParameterizedType) getClass(). getGenericSuperclass()).getActualTypeArguments()[0]; }
@Override public String convertToDatabaseColumn(final T object) { return object == null ? null : new JsonUtility<>(clazz).toString(object); }
@Override public T convertToEntityAttribute(final String value) { return value == null ? null : new JsonUtility<>(clazz).readFromString( value); }
}
|