I'm trying to store a long text file into the DBMS. Here is my table definition :
@Entity
@Table(name = "foos")
public class Foo {
private static final long serialVersionUID = 7111245814238004034L;
@Id
@GeneratedValue
@Column(nullable = false, name = "id")
private long id;
@Lob
@Column(nullable = false, name = "content", length = 100_000)
private String content;
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
And here is my liquibase changeset :
<createTable tableName="foos">
<column name="id" type="bigint" />
<column name="content" type="clob" />
</createTable>
This work fine with H2 but with Postgres, when I try load a Foo, I get :
org.postgresql.util.PSQLException: Bad value for type long : <content>
org.postgresql.jdbc.PgResultSet.toLong(PgResultSet.java:2876)
org.postgresql.jdbc.PgResultSet.getLong(PgResultSet.java:2083)
org.postgresql.jdbc.PgResultSet.getClob(PgResultSet.java:452)
org.postgresql.jdbc.PgResultSet.getClob(PgResultSet.java:439)
org.apache.commons.dbcp2.DelegatingResultSet.getClob(DelegatingResultSet.java:675)
org.apache.commons.dbcp2.DelegatingResultSet.getClob(DelegatingResultSet.java:675)
org.hibernate.type.descriptor.sql.ClobTypeDescriptor$1.doExtract(ClobTypeDescriptor.java:44)
org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243)
(saving a Foo to the database works though).
If I try to add another annotation to content
:
@Type(type = "org.hibernate.type.TextType")
This works fine with Postgres (prod environnement) but not with H2 (test env) :
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [content] in table [public.war_contexts]; found [clob (Types#CLOB)], but expecting [varchar(2147483647) (Types#LONGVARCHAR)]
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateColumnType(AbstractSchemaValidator.java:159)
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateTable(AbstractSchemaValidator.java:143)
at org.hibernate.tool.schema.internal.GroupedSchemaValidatorImpl.validateTables(GroupedSchemaValidatorImpl.java:42)
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.performValidation(AbstractSchemaValidator.java:89)
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:68)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:192)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:314)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1237)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391)
... 81 more
Any idea on how to solve the issue on both DBMS (and bonus points if it works with MariaDB or Oracle)
EDIT:
tried those conneciton settings
jdbc:h2:mem:myDb;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1
But it's not helping
CLOB
data type in PostgreSQL. H2 supports this data type, butTEXT
in H2 is also mapped toCLOB
. Hibernate schema validation complains too often about different, but still compatible data types; you need to disable it if you really want to use different databases, usually it's a bad idea to use different database systems for tests and production. – Vermis@Column(columnDefinition = "clob")
from this answer solved it. – Trachytic