Is there any way how to support persistent mapping of java.time.ZoneId to string in Hibernate 5.1.1. It saves the ZoneId in binary form right now.
I've just upgraded to Grails 3.2.1 which has Hibernate 5.1.1. Saving of java.time.Instant for example works fine however java.time.ZoneId is stored only in binary form.
I think there is no support from Hibernate. So how can I code my own mapping. I've tried to use Jadira Framework but it is not possible as there are some conflicts (exceptions) when starting the grails app.
You can use a custom attribute converter as defined by JPA 2.1. Declare the converter class like so:
@Converter
public static class ZoneIdConverter implements AttributeConverter<ZoneId, String> {
@Override
public String convertToDatabaseColumn(ZoneId attribute) {
return attribute.getId();
}
@Override
public ZoneId convertToEntityAttribute(String dbData) {
return ZoneId.of( dbData );
}
}
And then refer to it from the entity attribute of type ZoneId
:
@Convert(converter = ZoneIdConverter.class)
private ZoneId zoneId;
The converter will automatically be invoked when persisting/loading the zoneId
attribute.
You can use Hibernate types library and then just write
@Column
private ZoneId zoneId;
in your entity classes. You have to mark the entity class with this annotation:
@TypeDef(typeClass = ZoneIdType.class, defaultForType = ZoneId.class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With