Enumerated types and Hibernate

From: Emerson Farrugia <emerson(dot)farrugia(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Enumerated types and Hibernate
Date: 2009-02-12 08:58:03
Message-ID: b0867d070902120058ye737625vb870baec351daca8@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,
I'm using Hibernate and would like to map a Java enum to a table column
defined as an enumerated type. Is this possible?

The following is my SQL:

create type day as enum ('Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday');

create table date (
...
day day not null
...);

And the following is my Java code:

public enum Day {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}

@Enumerated(EnumType.STRING)
public Day getDay() {
return day;
}

When I try to persist an object containing a Day, I get the following
exception:

Feb 12, 2009 9:32:12 AM org.hibernate.util.JDBCExceptionReporter
logExceptions
WARNING: SQL Error: 0, SQLState: 42804
Feb 12, 2009 9:32:12 AM org.hibernate.util.JDBCExceptionReporter
logExceptions
SEVERE: ERROR: column "day" is of type day but expression is of type
character varying

Using the ordinal enum type in the @Enumerated annotation gives me a similar
error, telling me that the expression is of type integer.

Is my only option to abandon the PostgreSQL enumerated types and save
ordinals or strings directly instead?

Any help would be appreciated,
Regards,
Emerson

Browse pgsql-general by date

  From Date Subject
Next Message Grzegorz Jaśkiewicz 2009-02-12 09:04:02 Re: Multiple postgres.exe On Processes
Previous Message Serge Fonville 2009-02-12 08:28:28 Re: Multiple postgres.exe On Processes