getting H2/Hibernate to recognize the Postgresql "text" type as Java enum

2,935 views
Skip to first unread message

limbic...@gmail.com

unread,
Oct 20, 2008, 4:06:49 PM10/20/08
to H2 Database
I'm having trouble getting H2/Hibernate to recognize the Postgresql
"text" type. I have defined a column in Java as an enumerated type,
mapping that to "text default 'NOT_NEEDED" in Postgres.

public static enum PostProc { NOT_NEEDED, NEEDED, COMPLETED };

Then in my model class I have:

@Column(name = "postproc", columnDefinition = "text default
'NOT_NEEDED'")
@Check(constraints = "(postproc in ('NOT_NEEDED', 'NEEDED',
'COMPLETED')")
@Enumerated(EnumType.STRING)
public PostProc getPostProcStatus() {
return postProcStatus;
}


testGetRunningJobs(com.example.UnitTest) Time elapsed: 0.061 sec <<<
FAILURE!
java.lang.ClassCastException: java.io.BufferedReader
at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:95)
at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:105)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at
org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:
2101)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:
236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at
org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:
338)
at
org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:
172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at com.example.MyClass$9.doInHibernate(MyClass.java:148)
at
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:
372)
at
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:
338)



For historical reasons, our codebase uses this Postgres-specific
"text" type rather than a varchar. Can this be made to work in H2?
I also tried adding a domain, but that did not help:

st.executeUpdate("CREATE domain IF NOT EXISTS TEXT AS
VARCHAR;");


Many thanks in advance for help with this.
L

Thomas Mueller

unread,
Oct 23, 2008, 7:44:29 PM10/23/08
to h2-da...@googlegroups.com
Hi,

TEXT is currently mapped to CLOB. Unfortunately I don't have a
workaround for you, I'm sorry. I will add a feature request to allow
to remap such data types, but it will take some time until this is
implemented.

Regards,
Thomas

Limbic System

unread,
Oct 24, 2008, 10:08:43 AM10/24/08
to h2-da...@googlegroups.com
Hi Thomas, thanks for your response.

I found that my code works fine with a small modification to
DataTypes.java- I moved the "TEXT" alias up so that it maps under the
"String" type, rather than CLOB:


add(Value.STRING, Types.VARCHAR, "String",
createString(true),
new String[]{"VARCHAR", "VARCHAR2", "TEXT",
"NVARCHAR", "NVARCHAR2", "VARCHAR_CASESENSITIVE",
"CHARACTER VARYING", "TID"},
4
);


Not being familiar with the codebase, I am not sure what other side
effects this may cause, but it does seem to solve my problem with
Postgres. Being able to re-map types at runtime would be very
helpful.

Thanks.

Thomas Mueller

unread,
Oct 27, 2008, 4:17:03 PM10/27/08
to h2-da...@googlegroups.com
Hi,

> Being able to re-map types at runtime would be very helpful.

Yes, anyway that's easy to implement and can solve other problems as
well. I have implemented it now, it will be available in the next
release. You will still need to define it however using:

CREATE DOMAIN TEXT AS VARCHAR;

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages