dmitrievanthony
dmitrievanthony

Reputation: 1561

JPA enum (java.lang.ClassCastException: org.postgresql.util.PGobject)

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

Upvotes: 1

Views: 2106

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42074

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.

Upvotes: 3

Related Questions