Add Hibernate 7 batches 2-5: mapping styles, JPA annotations, natural IDs, @Immutable, stored procedures, in-memory test databases, JNDI mocking, proxies, associations, temporal mapping and named queries (Hibernate 7.4.5.Final + Spring Boot 4.1.1 + JDK 25)
This commit is contained in:
+61
@@ -0,0 +1,61 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user