Not able to save hstore in postgresql

Viewed 893

I use spring boot, spring data jpa with hibernate (5.2.10) implementation. Postgresql 9.6

In my entity object I have

@Column(columnDefinition = "hstore")
@Convert(converter = MapToStringConveter.class)
private Map<String, String> fields = new HashMap<>();


@Converter
public class MapToStringConveter implements AttributeConverter<Map<String, String>, String>, Serializable {

    @Override
    public String convertToDatabaseColumn(Map<String, String> attribute) {
        return HStoreConverter.toString(attribute);
    }

    @Override
    public Map<String, String> convertToEntityAttribute(String dbData) {
        if (dbData != null) {
            return HStoreConverter.fromString(dbData);
        }

        return null;
    }
}

HSToreConverter come from:

org.postgresql.util.HStoreConverter;

When i try to save my entity I get

org.postgresql.util.PSQLException: ERROR: column "fields" is of type hstore but expression is of type character varying Indice : You will need to rewrite or cast the expression.

Fields is correctly feeded

Edit

Hibernate:

update
    public.products 
set
    fields=? 
where
    id=?

DEBUG 41735 --- [nio-8080-exec-5] tributeConverterSqlTypeDescriptorAdapter : Converted value on binding : 2016-12-15T11:05:36.171528 -> Thu Dec 15 11:05:36 EST 2016
DEBUG 41735 --- [nio-8080-exec-5] tributeConverterSqlTypeDescriptorAdapter : Converted value on binding : 2017-08-16T10:28:32.615 -> Wed Aug 16 10:28:32 EDT 2017
DEBUG 41735 --- [nio-8080-exec-5] tributeConverterSqlTypeDescriptorAdapter : Converted value on binding : {year=2015, apn=canon, legacy_identifier=1} -> "year"=>"2015", "apn"=>"canon", "legacy_identifier"=>"1"
DEBUG 41735 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper   : could not execute statement [n/a]

org.postgresql.util.PSQLException: ERROR: column "fields" is of type hstore but expression is of type character varying
  Indice : You will need to rewrite or cast the expression.
  Position : 86
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2477) ~[postgresql-42.1.4.jar:42.1.4]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2190) ~[postgresql-42.1.4.jar:42.1.4]

Edit

With this project (without spring data-jpa)

https://github.com/phstudy/jpa-converter-sample

that work fine

is there any setup to get it working with spring data-jpa?

0 Answers
Related