Files
hibernate-demo/src/main/java/com/ankurm/hibernatedemo/persistenceannotations/EnumeratedValueEntity.java
T

62 lines
1.7 KiB
Java
Executable File

package com.ankurm.hibernatedemo.persistenceannotations;
import jakarta.persistence.Entity;
import jakarta.persistence.Enumerated;
import jakarta.persistence.EnumType;
import jakarta.persistence.EnumeratedValue;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
/**
* {@code @EnumeratedValue} is NEW in Jakarta Persistence 3.2 (confirmed present via javap on
* jakarta.persistence-api 3.2.0 -- it does not exist in 3.1). It lets an enum control its own
* persisted representation (a custom code), instead of ORDINAL or STRING.
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
@Entity
public class EnumeratedValueEntity {
@Id
@GeneratedValue
private Long id;
// @EnumeratedValue's own field type (String) determines the persisted column type, but
// Hibernate still needs to be told this is a STRING-shaped enum, not the ORDINAL default --
// otherwise boot fails with "@EnumeratedValue for EnumType.ORDINAL must be placed on a
// field whose type is byte, short, or int".
@Enumerated(EnumType.STRING)
private Priority priority;
public EnumeratedValueEntity() {
}
public EnumeratedValueEntity(Priority priority) {
this.priority = priority;
}
public Long getId() {
return id;
}
public Priority getPriority() {
return priority;
}
/** Persisted representation is the "code" string, NOT the enum's ordinal or name(). */
public enum Priority {
LOW("L"), MEDIUM("M"), HIGH("H");
@EnumeratedValue
private final String code;
Priority(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
}