使用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。
请帮助我
答案 0 :(得分:0)
您可以创建一个转换器:
@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;