1

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; ... }

dmitrievanthony
  • 1,501
  • 1
  • 15
  • 41
  • 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 http://wiki.eclipse.org/EclipseLink/Examples/JPA/EnumToCode#How_to_Map_Enum_to_Coded_Values and http://stackoverflow.com/questions/10898369/mapping-java-enum-on-postgres-enum-with-eclipselink – Craig Ringer Aug 28 '12 at 03:27

1 Answers1

3

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.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135