59 lines
2.7 KiB
Java
Executable File
59 lines
2.7 KiB
Java
Executable File
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);
|
|
}
|
|
}
|
|
}
|