[Fwd: Re: Enums - from java to jpa]

From: "Vernon" <vwu(at)vicina(dot)info>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: [Fwd: Re: Enums - from java to jpa]
Date: 2009-05-13 15:06:43
Message-ID: 63784.174.6.129.141.1242227203.squirrel@www.vicina.info
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

---------------------------- Original Message ----------------------------
Subject: Re: [JDBC] Enums - from java to jpa
From: "Vernon" <vwu(at)vicina(dot)info>
Date: Wed, May 13, 2009 7:54 am
To: "Guy Rouillier" <guyr-ml1(at)burntmail(dot)com>
--------------------------------------------------------------------------

I read an article on Java enum type with Hibernate by a SE at Oracle. The
author says that the Java enum type mapping can be done with two
approaches: integer for one and String for the other. Here is how the
first approach works:

import javax.persistence.EnumType;

public enum STATUS {
PENDING, RUNNING, PAUSED, DELETED
};

protected STATUS status;

@Enumerated(EnumType.ORDINAL)
@Column(name = "status", nullable = false)
public STATUS getStatus() {
return status;
}

where the mapping field in the DB table is the int type.

> The Jasper wrote:
>> L.S.,
>>
>> We are updating out application to make use of JPA. We have also
introduced postgres enums into our database. Naturally we also have
java enums which mirror the postgres enums. Now...how do I get JPA to
understand that it has different types at either end which are
>> actually the same. I mostly get ClassCastExceptions where PGObject is
being cast to String. I can't get rid of an enum definition at either
end, I'm stuck with that. So I need to make JPA understand how to map
from one to the other. We use Hibernate as our JPA implementation.
>>
>
> Our project is also using JPA via Hibernate, but with Oracle on the back
end. Here is how we annotate our columns that access Java enums:
>
> @Column(name = "PREGION_ID", precision = 20, scale = 0, nullable =
> true)
> @Type(type = "com.masergy.sales360.persistence.GenericEnumUserType",
> parameters = {
> @Parameter(name = "enumClass", value =
> "com.masergy.sales360.client.model.OmsPricingRegionEnum"),
> @Parameter(name = "identifierMethod", value = "toInt"),
> @Parameter(name = "valueOfMethod", value = "fromInt") })
>
> GenericEnumUserType implements UserType, ParameterizedType (Hibernate
interfaces.)
>
> --
> Guy Rouillier
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>
> --------------------------------
> Spam/Virus scanning by CanIt Pro
>
> For more information see
> http://www.kgbinternet.com/SpamFilter.htm
>
> To control your spam filter, log in at
> http://filter.kgbinternet.com
>

--
www.vicina.info - your local community information service

News portal - quick, easy browses more than 4,000 news entries from all
major news agencies around the world.
Community news - share your story with others in your community.
Service directory - find or review local places to eat, shop, relax, and
play.
Classified ad - post and browse free classified ads in your neighbourhood.

--
www.vicina.info - your local community information service

News portal - quick, easy browses more than 4,000 news entries from all
major news agencies around the world.
Community news - share your story with others in your community.
Service directory - find or review local places to eat, shop, relax, and
play.
Classified ad - post and browse free classified ads in your neighbourhood.

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2009-05-13 15:21:42 Re: pooled prepared statements
Previous Message John Lister 2009-05-13 14:37:17 Re: pooled prepared statements