Answer the question
In order to leave comments, you need to log in
What are the ways to translate fields of jpa-entity classes into json-type postgres tables?
Hello, tell me what are the ways to translate fields of jpa-entity classes (specifically, we use hibernate) into json-type properties of postgres tables. Ideally, I would like to have a field with the type of some pojo class and, when saving the entity through the repository, have automatic conversion to the json-type field of the table and vice versa.
Answer the question
In order to leave comments, you need to log in
You can use the standard AttributeConverter from JPA. Something like this:
public class ShippingAddressConverter implements AttributeConverter<ShippingAddressData, String> {
private final ObjectMapper objectMapper = new ObjectMapper();
private final JavaType shippingAddressType =
this.objectMapper.getTypeFactory().constructSimpleType(ShippingAddressData.class, null);
@Override
public String convertToDatabaseColumn(ShippingAddressData attribute) {
if (attribute == null) {
return null;
}
try {
return this.objectMapper.writeValueAsString(attribute);
} catch (JsonProcessingException e) {
throw new PersistenceException("Failed to serialize shipping address", e);
}
}
@Override
public ShippingAddressData convertToEntityAttribute(String dbData) {
if (dbData == null) {
return new ShippingAddressData();
}
try {
return this.objectMapper.readValue(dbData, shippingAddressType);
} catch (IOException e) {
throw new PersistenceException("Failed to deserialize shipping address", e);
}
}
}
@Column(name = "shipping_address")
@Convert(converter = ShippingAddressConverter.class)
private ShippingAddressData shippingAddress;
CREATE CAST (varchar AS jsonb) WITH INOUT AS IMPLICIT;
CREATE CAST (text AS jsonb) WITH INOUT AS IMPLICIT;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question