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

asked Aug 27, 2012 at 16:00
1
  • 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/… Commented Aug 28, 2012 at 3:27

1 Answer 1

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.

answered Aug 27, 2012 at 17:46
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.