daPhantom
daPhantom

Reputation: 119

ENUMs ordinal within JPA entities (Hibernate)

I've got an entity that is managed within Hibernate's EntityManager. This entity does have a state that is represented by an ENUM. Hibernate does save the ENUM value within the database as integer.

How does Hibernate perform the lookup when the ENUM itself does not specify any numeric values? Does it use the ordinal value (oh dear!)?

Upvotes: 1

Views: 1876

Answers (2)

jklee
jklee

Reputation: 2268

The magic annotation is @Enumerated.

For persist enumerated type as a string.

@Enumerated(EnumType.STRING)

For persist enumerated type as a integer.

@Enumerated(EnumType.ORDINAL)

Upvotes: 1

davidxxx
davidxxx

Reputation: 131356

Declare the mapping by specifying you want to use the name of the enum.

@Enumerated(EnumType.STRING)
private YourEnum yourEnum;

The default is EnumType.ORDINAL. It persists the enum in the DB as a number corresponding to the order of it in the declaration of the enum class.

Whereas the actual behavior.

Upvotes: 3

Related Questions