I try to use @Enumerated annotation fot mapping my enum type, but getting follow error:
Exception in thread "main" java.lang.ClassCastException: org.postgresql.util.PGobject cannot be cast to java.lang.String
What i do: in postgres
create type "test_type" as enum ('test_1', 'test_2);
in java
public enum TestType{ test_1, test_2 }
@Entity @Table(name="test_table") public class TestTable { ...
@Enumerated(EnumType.STRING) @Column(name="col") private TestType col; ... }
-
This is one of the costs of using a database access layer that tries to be database-agnostic. It often can't use the full capabilities of the underlying database engine. In the case of enums you can write persistence-provider-specific datatype-converters or use provider-specific extension annotations like EclipseLink's enum mappings, but of course it'll only work on that persistence provider. See eg wiki.eclipse.org/EclipseLink/Examples/JPA/… and stackoverflow.com/questions/10898369/…Craig Ringer– Craig Ringer2012年08月28日 03:27:47 +00:00Commented Aug 28, 2012 at 3:27
1 Answer 1
In JPA enums can be persisted as a text (name of the enum) or as a numerical value (ordinal of enum). @Enumerated(EnumType.STRING) tells that you prefer to persist name. Consequently database type should be varchar. Your JPA provider is not aware of PostgreSQL enums.