@Enumerated

From NovaOrdis Knowledge Base
Revision as of 04:16, 19 October 2018 by Ovidiu (talk | contribs) (Created page with "=Internal= * JPA Concepts =Overview= Optional annotation to use to annotate member variables of that have an enumeration type. If the enumerati...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Internal

Overview

Optional annotation to use to annotate member variables of that have an enumeration type. If the enumeration type values are represented as an integer in the database, no annotation is necessary. This is the default. If the enumeration type values are represented as string, the corresponding member variable should be annotated with:

    @Enumerated(EnumType.STRING)
    private final Color color;

    public enum Color {
        WHITE, BLUE, RED, GREEN, BLACK
    }

If the values are maintained in the database as VARCHARs ("WHITE", etc.) and the member variable is not annotated to indicate that, we get a conversion error:

rg.postgresql.util.PSQLException: Bad value for type int : WRAP
	at org.postgresql.jdbc.PgResultSet.toInt(PgResultSet.java:2844) ~[postgresql-42.2.5.jar!/:42.2.5]
	at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2073) ~[postgresql-42.2.5.jar!/:42.2.5]
	at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2485) ~[postgresql-42.2.5.jar!/:42.2.5]
	...
	at org.hibernate.type.EnumType$OrdinalEnumValueMapper.getValue(EnumType.java:337) ~[hibernate-core-5.2.17.Final.jar!/:5.2.17.Final]