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

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


Since the text type is not a part of the SQL standard there is no official JPA way I guess.

However, the text type is quite similar to varchar, but without the length limit. You can hint the JPA implementation with the length property of @Column:

@Column(length=10485760)private String description;

Update: 10 MiB seems to be the maximum length for varchar in postgresql. The text is almost unlimited, according the documentation:

In any case, the longest possible character string that can be stored is about 1 GB.


I just had to add this annotation:

@Column(columnDefinition="TEXT")

It did not work on its own. I had to recreate the table in the database.

DROP TABLE yourtable or just alter column type to text with ALTER TABLE statement


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.