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. * *
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