Reputation: 794
I've got table with column of type "jsonb". In entity I set type String for this column with attribute converter:
@Convert(converter = JSONBConverter.class)
@Column(name = STATE_COLUMN, nullable = false)
private String getState() {
return state;
}
And my converter looks like:
@Converter
public class JSONBConverter implements AttributeConverter<String, Object> {
@Override
public Object convertToDatabaseColumn(String attribute) {
PGobject result = new PGobject();
result.setType("json");
try {
result.setValue(attribute);
} catch (SQLException e) {
throw new IllegalArgumentException("Unable to set jsonb value");
}
return result;
}
@Override
public String convertToEntityAttribute(Object dbData) {
if (dbData instanceof PGobject) {
return ((PGobject) dbData).getValue();
}
return StringUtils.EMPTY;
}
}
I got dialect set to: org.hibernate.dialect.PostgreSQL95Dialect
I thought it was gonna work. But I get an error with:
org.postgresql.util.PSQLException: Nieznana warto�� Types: 1�936�628�443
As I debugged it gets targetSqlType
in PgPreparedStatement
class setObject method 1936628443 - what indicates on Object type which is taken from my AttributeConverter class which is assigned in SqlTypeDescriptorRegistry
class.
I've got:
postgresql version 42.2.1
hibernate version 5.2.10.Final
Upvotes: 7
Views: 7933
Reputation: 1525
AttributeConverter
+ json/jsonb
do not play well together because you need to bind the JSON object at the PreparedStatement
level.
You have to declare a Hibernate Type to get JSONB working.
See this article for a detailed tutorial of how you can do that.
Upvotes: 3