The PostgreSQL JDBC driver provides org.postgresql.util.HStoreConverter with / from String and byte[] conversions. You can use it to implement your own JPA 2.1 Converter :
import javax.persistence.AttributeConverter; import javax.persistence.Converter; import java.util.Map; import org.postgresql.util.HStoreConverter; @Converter public class MyHStoreConverter implements AttributeConverter<Map<String, String>, String> { @Override public String convertToDatabaseColumn(Map<String, String> attribute) { return HStoreConverter.toString(attribute); } @Override public Map<String, String> convertToEntityAttribute(String dbData) { return HStoreConverter.fromString(dbData); } }
Then use it with the JPA Convert annotation in your entity:
@Entity public class MyEntity { @Convert(converter = MyHStoreConverter.class) private Map<String, String> hstoreAttribute; }
This is an implementation that uses only JPA standards, so it must be agnostic to the JPA provider. However, using Converter to map to a general Map<> previously blocked by the Hibernate HHH-8804 error, but this was fixed in Hibernate 5.0.0 and 4.3.11.
Adam michalik
source share