创建Hibernate Type以使用PostgreSQL保存JSONObject

时间:2014-06-18 09:09:38

标签: java json postgresql hibernate mapping

我正在使用Postgres 9和Hibernate 4作为ORM。

在Postgres中,可以选择创建一个包含json类型列的表。

我希望我的Java代码能够在查询中发送JSONObject,以便将它们转换为\转换为Postgres类型。

我该怎么做?

我需要反对:

  1. 第一个对象来自UserType。
  2. 第二个对象扩展:extends AbstractSingleColumnStandardBasicType实现DiscriminatorType。
  3. 那有什么例子吗?

2 个答案:

答案 0 :(得分:1)

您不必手动创建所有这些类型,只需使用以下依赖项通过Maven Central获取它们:

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
    <version>1.0.0</version>
</dependency>

有关详细信息,请查看hibernate-types open-source project

现在,正如this article中所解释的那样,映射就像那样简单:

@Entity(name = "Book")
@Table(name = "book")
@TypeDef(
    name = "jsonb-node", 
    typeClass = JsonNodeBinaryType.class
)
public class Book {

    @Id
    @GeneratedValue
    private Long id;

    @NaturalId
    private String isbn;

    @Type( type = "jsonb-node" )
    @Column(columnDefinition = "jsonb")
    private JsonNode properties;

    //Getters and setters omitted for brevity
}

就是这样!

答案 1 :(得分:0)

只需花一些时间在这上面,这对我有用:

    public class JSONUserType implements UserType {

        private static final int[] SQL_TYPES = { Types.LONGVARCHAR };
    @Override
    public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
        return deepCopy(cached);
    }

    @Override
    public Object deepCopy(Object value) throws HibernateException {
        if (value == null)
            return value;
        try {
            return new JSONObject(((JSONObject) value).toString());
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return ((JSONObject) value).toString();
    }

    @Override
    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == null)
            return (y != null);
        return (x.equals(y));
    }

    @Override
    public int hashCode(Object x) throws HibernateException {
        return ((JSONObject) x).toString().hashCode();
    }

    @Override
    public boolean isMutable() {
        return true;
    }

    @Override
    public Object replace(Object original, Object target, Object owner)throws HibernateException {
        return deepCopy(original);
    }

    @Override
    @SuppressWarnings("unchecked")
    public Class returnedClass() {
        return JSONObject.class;
    }

    @Override
    public int[] sqlTypes() {
        return SQL_TYPES;
    }

    @Override
    public Object nullSafeGet(ResultSet rs, String[] names,SessionImplementor session, Object owner)throws HibernateException, SQLException {
        String jsonString = rs.getString(names[0]);
        if (jsonString == null)
            return null;

        try {
            return new JSONObject(jsonString);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index,SessionImplementor session) throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, Types.OTHER);
        } else {

            st.setObject(index, ((JSONObject) value).toString(),Types.OTHER);
        }

    }

}