Correct JPA Annotation for PostgreSQL's text type without Hibernate Annotations

前端 未结 4 2062
抹茶落季
抹茶落季 2020-12-15 16:28

I\'m developing an application using:

  • Java 1.7
  • JPA (included in javaee-api 7.0)
  • Hibernate 4.3.8.Final
  • PostgreSQL-JDBC 9.4-1200-jdbc4
4条回答
  •  醉话见心
    2020-12-15 17:33

    If you want to use plain JPA you could just remap the used CLOB type on the Dialect like this:

    public class PGSQLMapDialect extends PostgreSQL9Dialect {
    
    
      @Override
      public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) {
        if (Types.CLOB == sqlTypeDescriptor.getSqlType()) {
          return LongVarcharTypeDescriptor.INSTANCE;
        }
        return super.remapSqlTypeDescriptor(sqlTypeDescriptor);
      }
    
    
    }
    

    So it won't use the CLOB mapping from the JDBC driver which uses a OID for the column and stores/loads the text via large object handling. This would just result in setString and getString calls on the createt text column on the Postgres JDBC Driver via the VarcharTypeDescriptor class.

提交回复
热议问题