7

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

is this still the case with Hibernate 6.1 support for JSONB?
@TypeDef is deprecated in newer versions of Hibernate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.