Как сохранить сущность, содержащую поле Hstore, используя OpenJPA - PullRequest
0 голосов
/ 26 июня 2019

Использование реализации OpenJPA (PostgreSQL):

Я пытаюсь сохранить сущность, связывающуюся с полем Hstore:

@Entity
@Table(name = "gfx_properties")
@NamedQuery(name = "GfxProperties.findAll", query = "SELECT g FROM GfxProperties g")
public class GfxProperties implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "id_gfx_properties")
    private Integer idGfxProperties;

    @Lob @Persistent @Column(name = "key_values")
    private String keyValues;

    public Map<String, String> getKeyValues() {
        return keyValues != null ? HStoreConverter.fromString(keyValues) : new HashMap<>();
    }

    public void setKeyValues(Map<String, String> keyValues) {
        try {
            if (keyValues == null) {
                this.keyValues = null;
            } else {
                this.keyValues = HStoreConverter.toString(keyValues);
            }
        } catch (Exception e) {
            // Ignore
        }
    }

    // other getters and setters
}

но это, похоже, не работает, у меня есть исключение, сообщающее, что столбец «key_values» имеет тип hstore, а выражение имеет тип oid.

Пожалуйста, помогите мне

1 Ответ

0 голосов
/ 26 июня 2019

Вы можете создать конвертер:

@Converter(autoApply = true)
public class MapToStringConveter 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);
    }
}

И затем использовать его в своей сущности:

@Convert(converter = MapToStringConveter.class)
@Column(name = "key_values")
private String keyValues;
...