Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate postgresql/hsqldb TEXT column incompatibility problem

I have a problem using Hibernate and PostgreSQL for production and HSQLDB for testing.
I am using top-down approach letting Hibernate create database schema.
I am also using annotations; mapping part of hibernate.cfg.xml only contains lines like
<mapping class="package.subpackage.ClassName" />
Hibernate defaults String variables to character varying(255) on PostgreSQL which is not sufficient for me in some cases, so I have to redefine some columns manually using
@Column(columnDefinition = "TEXT").
But, TEXT type is invalid for HSQLDB, so those tables can not be created.

Can anyone help to solve this?

like image 525
Nemanja Avatar asked Nov 18 '10 10:11

Nemanja


4 Answers

The easiest way to deal with this specific issue is probably to not use the columnDefinition at all and instead to explicitly specify the column length with (for example)

@Column(length=10000)

It might also be that you could instead map it with @Lob(type = LobType.CLOB)

but I'm not sure that is supported properly in HSQLDB. In Postgres it should give you your TEXT type.

like image 50
Don Roby Avatar answered Oct 13 '22 22:10

Don Roby


Agree with @fredt. TEXT data type isn't standard SQL type, but extension that some engine supports.

To enable PostgreSQL compatibility mode use sql.syntax_pgs=true in your connection parameters.

like image 43
G. Demecki Avatar answered Oct 13 '22 22:10

G. Demecki


HSQLDB 2.1 and later has a PostgreSQL compatibility mode and supports the TEXT data type in this mode.

like image 6
fredt Avatar answered Oct 13 '22 23:10

fredt


To get H2 to work in compatability mode with PostgreSQL (useful for junit testing).

# JDBC Driver
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:play;MODE=PostgreSQL;TRACE_LEVEL_SYSTEM_OUT=2;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;INIT=CREATE TABLE IF NOT EXISTS PG_CLASS (RELNAME text, RELKIND text);
jdbc.username=sa
jdbc.password=

# general hibernate options
hibernate.database=h2
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

The create table PG_CLASS is required to allow Hibernate/JPA to correctly function. But other than that - pretty seamless.

like image 4
terrance.a.snyder Avatar answered Oct 13 '22 22:10

terrance.a.snyder