【问题标题】:Create Hibernate Type to save JSONObject with PostgreSQL创建 Hibernate 类型以使用 PostgreSQL 保存 JSONObject
【发布时间】:2014-08-08 12:13:47
【问题描述】:

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

在 Postgres 中,可以选择使用json 类型列创建表。

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

我该怎么做?

我要反对:

  1. 第一个对象从 UserType 扩展而来。
  2. 第二个对象扩展:扩展 AbstractSingleColumnStandardBasicType 实现 DiscriminatorType。

有什么例子吗?

【问题讨论】:

    标签: java json postgresql hibernate mapping


    【解决方案1】:

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

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

    欲了解更多信息,请查看Hibernate Types open-source project

    现在,您的映射可以使用JsonType,如下所示:

    @Entity(name = "Book")
    @Table(name = "book")
    @TypeDef(
        name = "json", 
        typeClass = JsonType.class
    )
    public class Book {
     
        @Id
        @GeneratedValue
        private Long id;
     
        @NaturalId
        private String isbn;
     
        @Type( type = "json" )
        @Column(columnDefinition = "jsonb")
        private JsonNode properties;
     
        //Getters and setters omitted for brevity
    }
    

    就是这样!

    【讨论】:

      【解决方案2】:

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

          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);
              }
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-24
        • 2017-11-15
        • 2017-10-07
        • 1970-01-01
        • 2014-04-06
        相关资源
        最近更新 更多