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:
+81
@@ -0,0 +1,81 @@
|
||||
package com.ankurm.hibernatedemo.persistenceannotations;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.ankurm.hibernatedemo.HibernateDemoApplication;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
/**
|
||||
* Three things new in Jakarta Persistence 3.2 that post 4864 predates: {@code @EnumeratedValue}
|
||||
* (custom enum persisted representation), {@code getSingleResultOrNull()} on TypedQuery, and
|
||||
* record types as JPQL constructor-expression targets.
|
||||
*
|
||||
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
|
||||
*/
|
||||
@SpringBootTest(classes = HibernateDemoApplication.class)
|
||||
class Jpa32NewFeaturesTest {
|
||||
|
||||
@Autowired
|
||||
private EntityManagerFactory emf;
|
||||
|
||||
@Test
|
||||
void enumeratedValue_persistsTheCustomCode_notOrdinalOrName() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
EnumeratedValueEntity e = new EnumeratedValueEntity(EnumeratedValueEntity.Priority.HIGH);
|
||||
em.persist(e);
|
||||
em.getTransaction().commit();
|
||||
Long id = e.getId();
|
||||
em.close();
|
||||
|
||||
EntityManager em2 = emf.createEntityManager();
|
||||
Object raw = em2.createNativeQuery(
|
||||
"select priority from enumerated_value_entity where id = " + id).getSingleResult();
|
||||
EnumeratedValueEntity reread = em2.find(EnumeratedValueEntity.class, id);
|
||||
em2.close();
|
||||
|
||||
System.out.println("RESULT[jpa32-enumeratedvalue]: raw DB value for HIGH = '" + raw
|
||||
+ "' (neither ordinal '2' nor name 'HIGH' -- the @EnumeratedValue-annotated code 'H')");
|
||||
assertThat(raw).isEqualTo("H");
|
||||
assertThat(reread.getPriority()).isEqualTo(EnumeratedValueEntity.Priority.HIGH);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSingleResultOrNull_newIn32_returnsNullInsteadOfThrowing() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
TypedQuery<EnumeratedValueEntity> q = em.createQuery(
|
||||
"select e from EnumeratedValueEntity e where e.id = :id", EnumeratedValueEntity.class);
|
||||
q.setParameter("id", -999L);
|
||||
EnumeratedValueEntity result = q.getSingleResultOrNull();
|
||||
em.close();
|
||||
System.out.println("RESULT[jpa32-getsingleresultornull]: query matching zero rows via getSingleResultOrNull() = " + result
|
||||
+ " (getSingleResult() would have thrown NoResultException here)");
|
||||
assertThat(result).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void jpqlConstructorExpression_targetingARecord_worksIn725() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.persist(new EnumeratedValueEntity(EnumeratedValueEntity.Priority.HIGH));
|
||||
em.persist(new EnumeratedValueEntity(EnumeratedValueEntity.Priority.HIGH));
|
||||
em.persist(new EnumeratedValueEntity(EnumeratedValueEntity.Priority.LOW));
|
||||
em.getTransaction().commit();
|
||||
|
||||
List<PriorityCountView> counts = em.createQuery(
|
||||
"select new com.ankurm.hibernatedemo.persistenceannotations.PriorityCountView(e.priority, count(e)) "
|
||||
+ "from EnumeratedValueEntity e group by e.priority order by e.priority",
|
||||
PriorityCountView.class).getResultList();
|
||||
em.close();
|
||||
|
||||
System.out.println("RESULT[jpa32-record-constructor-expression]: " + counts);
|
||||
assertThat(counts).extracting(PriorityCountView::priority)
|
||||
.contains(EnumeratedValueEntity.Priority.HIGH, EnumeratedValueEntity.Priority.LOW);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user