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:
@@ -0,0 +1,58 @@
|
||||
package com.ankurm.hibernatedemo.mappingstyle;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.MappingSettings;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Proves the hbm.xml-mapped entity actually round-trips through the database -- not just
|
||||
* that metadata parses. Docs: docs/04-annotations-vs-xml.md, Topic 1.
|
||||
*/
|
||||
class HbmXmlRuntimeTest {
|
||||
|
||||
@Test
|
||||
void hbmXmlEntity_defaultSettings_actuallyPersistsAndQueries() {
|
||||
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.applySetting("hibernate.connection.driver_class", "org.h2.Driver")
|
||||
.applySetting("hibernate.connection.url", "jdbc:h2:mem:hbmruntime1;DB_CLOSE_DELAY=-1")
|
||||
.applySetting("hibernate.connection.username", "sa")
|
||||
.applySetting("hibernate.connection.password", "")
|
||||
.applySetting("hibernate.hbm2ddl.auto", "create")
|
||||
// NOTE: hibernate.transform_hbm_xml.enabled is deliberately NOT set here.
|
||||
.build();
|
||||
try {
|
||||
Metadata metadata = new MetadataSources(registry)
|
||||
.addResource("com/ankurm/hibernatedemo/mappingstyle/HbmEmployee.hbm.xml")
|
||||
.buildMetadata();
|
||||
try (SessionFactory sf = metadata.buildSessionFactory()) {
|
||||
Long id;
|
||||
try (Session s = sf.openSession()) {
|
||||
Transaction tx = s.beginTransaction();
|
||||
HbmEmployee e = new HbmEmployee("Ada", "[email protected]");
|
||||
s.persist(e);
|
||||
tx.commit();
|
||||
id = e.getId();
|
||||
}
|
||||
try (Session s = sf.openSession()) {
|
||||
HbmEmployee loaded = s.get(HbmEmployee.class, id);
|
||||
assertThat(loaded).isNotNull();
|
||||
assertThat(loaded.getFirstName()).isEqualTo("Ada");
|
||||
assertThat(loaded.getEmail()).isEqualTo("[email protected]");
|
||||
System.out.println("RESULT[hbm-default-runtime]: persisted+loaded id=" + id
|
||||
+ " firstName=" + loaded.getFirstName() + " email=" + loaded.getEmail()
|
||||
+ " -- NO hibernate.transform_hbm_xml.enabled setting was applied.");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
StandardServiceRegistryBuilder.destroy(registry);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user