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:
+88
@@ -0,0 +1,88 @@
|
||||
package com.ankurm.hibernatedemo.naturalid;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.ankurm.hibernatedemo.HibernateDemoApplication;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.stat.Statistics;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
/**
|
||||
* Proves the article's composite-natural-id code sample actually resolves, and captures the
|
||||
* real generated SQL: a two-column WHERE clause that includes the FK to Company, not a join
|
||||
* on the natural id of Company itself.
|
||||
*
|
||||
* <p>Docs: docs/06-natural-ids.md, Topic 3.
|
||||
*/
|
||||
@SpringBootTest(classes = HibernateDemoApplication.class)
|
||||
class CompositeNaturalIdTest {
|
||||
|
||||
@Autowired
|
||||
private EntityManagerFactory emf;
|
||||
|
||||
private Statistics stats() {
|
||||
return emf.unwrap(SessionFactory.class).getStatistics();
|
||||
}
|
||||
|
||||
@Test
|
||||
void byNaturalId_compositeKey_usingTwoFields_resolvesCorrectRow() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
Company acme = new Company("Acme");
|
||||
Company other = new Company("Other Co");
|
||||
em.persist(acme);
|
||||
em.persist(other);
|
||||
// Same deptCode ("ENG-01") at TWO different companies -- proves it's the (company, code)
|
||||
// PAIR that is unique, not deptCode alone.
|
||||
em.persist(new Department(acme, "ENG-01", "Acme Engineering"));
|
||||
em.persist(new Department(other, "ENG-01", "Other Co Engineering"));
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
EntityManager em2 = emf.createEntityManager();
|
||||
Session session = em2.unwrap(Session.class);
|
||||
stats().clear();
|
||||
|
||||
Department dept = session.byNaturalId(Department.class)
|
||||
.using("company", acme)
|
||||
.using("deptCode", "ENG-01")
|
||||
.load();
|
||||
|
||||
long queries = stats().getPrepareStatementCount();
|
||||
em2.close();
|
||||
|
||||
System.out.println("RESULT[composite-naturalid]: byNaturalId(company=Acme, deptCode=ENG-01) resolved to '"
|
||||
+ dept.getName() + "' in " + queries + " query/queries");
|
||||
|
||||
assertThat(dept).isNotNull();
|
||||
assertThat(dept.getName()).isEqualTo("Acme Engineering");
|
||||
assertThat(queries).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void byNaturalId_compositeKey_capturesTheGeneratedSql() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
Company acme = new Company("SQL-Capture Co");
|
||||
em.persist(acme);
|
||||
em.persist(new Department(acme, "OPS-1", "Operations"));
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
|
||||
EntityManager em2 = emf.createEntityManager();
|
||||
Session session = em2.unwrap(Session.class);
|
||||
Department dept = session.byNaturalId(Department.class)
|
||||
.using("company", acme)
|
||||
.using("deptCode", "OPS-1")
|
||||
.load();
|
||||
em2.close();
|
||||
assertThat(dept).isNotNull();
|
||||
// The actual SQL is captured in docs/output/naturalid-composite-sql.txt via show_sql
|
||||
// logging on this test run -- see that file for the verbatim SELECT.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user