Add Hibernate 7 batches 2-6, batch 7, and batch 8: mapping styles, JPA annotations, natural IDs, @Immutable, stored procedures, in-memory test databases, JNDI mocking, proxies, associations, temporal mapping, named queries, HQL, Criteria API, EntityManager bootstrapping, Ehcache 3 L2 cache configuration, HikariCP connection pooling, Hibernate Validator CDI integration, aggregate functions, sorting, pagination, interceptors, and Hibernate Search 8 (Hibernate 7.4.5.Final + Spring Boot 4.1.1 + JDK 25)

This commit is contained in:
2026-09-20 06:06:42 +00:00
committed by Claude
commit 8568c0ce6c
330 changed files with 23668 additions and 0 deletions
@@ -0,0 +1,49 @@
package com.ankurm.hibernatedemo.persistenceannotations;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
/**
* {@code status} has NO {@code @Enumerated} at all -- JPA's default is ORDINAL. This entity
* exists purely to demonstrate what breaks: reordering (or inserting into the middle of) the
* enum silently repoints every stored ordinal at the wrong constant.
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
@Entity
@Table(name = "enum_default_ordinal_entity")
public class EnumDefaultOrdinalEntity {
@Id
@GeneratedValue
private Long id;
// No @Enumerated -- JPA default is ORDINAL.
private OrderStatus status;
public EnumDefaultOrdinalEntity() {
}
public EnumDefaultOrdinalEntity(OrderStatus status) {
this.status = status;
}
public Long getId() {
return id;
}
public OrderStatus getStatus() {
return status;
}
public void setStatus(OrderStatus status) {
this.status = status;
}
/** V1 order of constants: NEW=0, SHIPPED=1, DELIVERED=2. */
public enum OrderStatus {
NEW, SHIPPED, DELIVERED
}
}
@@ -0,0 +1,40 @@
package com.ankurm.hibernatedemo.persistenceannotations;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
/**
* Maps the SAME table ({@code enum_default_ordinal_entity}) as {@link EnumDefaultOrdinalEntity},
* but with a status enum that has an EXTRA constant inserted before {@code SHIPPED}. This
* simulates "someone added a constant to the middle of the enum" without a migration --
* the classic ORDINAL trap. No @Enumerated here either (default ORDINAL), matching the
* original mapping exactly.
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
@Entity
@Table(name = "enum_default_ordinal_entity")
public class EnumReorderedV2Entity {
@Id
private Long id;
@Column(name = "status")
private ReorderedStatus status;
public Long getId() {
return id;
}
public ReorderedStatus getStatus() {
return status;
}
/** V2: a constant (PENDING_REVIEW) was inserted BEFORE SHIPPED -- ordinal 1 now means something else. */
public enum ReorderedStatus {
NEW, PENDING_REVIEW, SHIPPED, DELIVERED
}
}
@@ -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;
}
}
}
@@ -0,0 +1,47 @@
package com.ankurm.hibernatedemo.persistenceannotations;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import java.util.Objects;
/**
* The anti-pattern the article warns about in Q5: {@code equals()}/{@code hashCode()} based on
* the SURROGATE {@code @GeneratedValue} id. Before the first flush, {@code id} is null, so the
* object's hash code is fixed at "hash of null" -- then flush() mutates {@code id}, changing the
* hash code out from under any HashSet/HashMap the object is already sitting in.
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
@Entity
public class IdBasedEqualsEntity {
@Id
@GeneratedValue
private Long id;
private String label;
public IdBasedEqualsEntity() {
}
public IdBasedEqualsEntity(String label) {
this.label = label;
}
public Long getId() {
return id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof IdBasedEqualsEntity that)) return false;
return Objects.equals(id, that.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
@@ -0,0 +1,41 @@
package com.ankurm.hibernatedemo.persistenceannotations;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
/**
* Deliberately relies on the JPA/Hibernate DEFAULT {@code equals()}/{@code hashCode()}
* (identity-based, inherited from Object) with a surrogate {@code @GeneratedValue} id that is
* null until the first flush. This is the entity used to reproduce the classic
* "HashSet.contains() returns false after persist()" symptom.
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
@Entity
public class IdentityHashSetEntity {
@Id
@GeneratedValue
private Long id;
private String label;
public IdentityHashSetEntity() {
}
public IdentityHashSetEntity(String label) {
this.label = label;
}
public Long getId() {
return id;
}
public String getLabel() {
return label;
}
// Deliberately NOT overriding equals()/hashCode() -- uses Object identity.
// A second class below overrides them using the surrogate id, to show that trap too.
}
@@ -0,0 +1,40 @@
package com.ankurm.hibernatedemo.persistenceannotations;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import java.util.Map;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
/**
* {@code @JdbcTypeCode(SqlTypes.JSON)} on a {@code Map<String,Object>} field, tested against
* H2 2.4.240 -- the article recommends it without saying whether it actually works on H2.
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
@Entity
public class JsonColumnEntity {
@Id
@GeneratedValue
private Long id;
@JdbcTypeCode(SqlTypes.JSON)
private Map<String, Object> details;
public JsonColumnEntity() {
}
public JsonColumnEntity(Map<String, Object> details) {
this.details = details;
}
public Long getId() {
return id;
}
public Map<String, Object> getDetails() {
return details;
}
}
@@ -0,0 +1,76 @@
package com.ankurm.hibernatedemo.persistenceannotations;
import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
/**
* Default access is FIELD (because {@code @Id} is annotated on the field). One property,
* {@code computedLabel}, is explicitly switched to PROPERTY access via {@code @Access} on its
* getter, with a derivation the FIELD side does not know about. This reproduces the classic
* "mixed access silently reads the wrong value" symptom: a raw field mutation is invisible to
* Hibernate once a property is PROPERTY-access, and vice versa.
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
@Entity
public class MixedAccessEntity {
@Id
@GeneratedValue
private Long id;
private String rawValue;
// FIELD access is the entity default (because @Id is on a field). This backing field is
// deliberately never read directly by Hibernate for the "label" property -- only the
// PROPERTY-access getter below is.
@Transient
private int getterCallCount = 0;
public MixedAccessEntity() {
}
public MixedAccessEntity(String rawValue) {
this.rawValue = rawValue;
}
public Long getId() {
return id;
}
public String getRawValue() {
return rawValue;
}
public void setRawValue(String rawValue) {
this.rawValue = rawValue;
}
/**
* Explicitly PROPERTY access: Hibernate calls this getter (not a field read) to determine
* the persisted value for the "computedLabel" mapped property.
*/
@Access(AccessType.PROPERTY)
@Column(name = "computed_label")
public String getComputedLabel() {
getterCallCount++;
return rawValue == null ? null : rawValue.toUpperCase();
}
// Hibernate's default PROPERTY-access strategy REQUIRES a setter even for a logically
// read-only derived column -- omitting it throws PropertyNotFoundException at boot
// ("Could not locate setter method for property 'computedLabel'"). This setter is a
// deliberate no-op: it exists purely to satisfy that requirement.
public void setComputedLabel(String ignored) {
// no-op: computedLabel is derived from rawValue, never written directly
}
public int getGetterCallCount() {
return getterCallCount;
}
}
@@ -0,0 +1,11 @@
package com.ankurm.hibernatedemo.persistenceannotations;
/**
* A Java record used as a JPQL constructor-expression result type -- new in Jakarta
* Persistence 3.2 (JPQL constructor expressions may target a record, matching a canonical
* constructor by position/type).
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
public record PriorityCountView(EnumeratedValueEntity.Priority priority, long total) {
}
@@ -0,0 +1,42 @@
package com.ankurm.hibernatedemo.persistenceannotations;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import java.time.LocalDate;
/**
* {@code @Temporal} is {@code @Deprecated(since = "3.2")} in jakarta.persistence-api 3.2.0
* (confirmed via javap on the annotation class file). This entity puts it on a
* {@code java.time.LocalDate} field anyway, to see whether Hibernate 7.4.5 ignores it,
* warns, or throws at boot.
*
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
*/
@Entity
public class TemporalOnLocalDateEntity {
@Id
@GeneratedValue
private Long id;
@Temporal(TemporalType.DATE)
private LocalDate eventDate;
public TemporalOnLocalDateEntity() {
}
public TemporalOnLocalDateEntity(LocalDate eventDate) {
this.eventDate = eventDate;
}
public Long getId() {
return id;
}
public LocalDate getEventDate() {
return eventDate;
}
}