package com.ankurm.hibernatedemo.mappingstyle; import org.hibernate.SessionFactory; 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; /** * Empirical probe: what does Hibernate 7.4.5.Final actually do with a legacy * {@code .hbm.xml} mapping file, bypassing Spring entirely so every setting is explicit. * *

Docs: docs/04-annotations-vs-xml.md, Topic 1. */ class HbmXmlBootTest { private StandardServiceRegistry baseRegistry() { return new StandardServiceRegistryBuilder() .applySetting("hibernate.connection.driver_class", "org.h2.Driver") .applySetting("hibernate.connection.url", "jdbc:h2:mem:hbmtest;DB_CLOSE_DELAY=-1") .applySetting("hibernate.connection.username", "sa") .applySetting("hibernate.connection.password", "") .applySetting("hibernate.hbm2ddl.auto", "create") .build(); } @Test void defaultSettings_bootingWithHbmXml_printsWhatHappens() { StandardServiceRegistry registry = baseRegistry(); try { Metadata metadata = new MetadataSources(registry) .addResource("com/ankurm/hibernatedemo/mappingstyle/HbmEmployee.hbm.xml") .buildMetadata(); SessionFactory sf = metadata.buildSessionFactory(); System.out.println("RESULT[default]: BOOT SUCCEEDED, SessionFactory built without hibernate.transform_hbm_xml.enabled"); sf.close(); } catch (Throwable t) { System.out.println("RESULT[default]: BOOT FAILED: " + t.getClass().getName() + ": " + t.getMessage()); Throwable cause = t.getCause(); while (cause != null) { System.out.println("RESULT[default]: caused by: " + cause.getClass().getName() + ": " + cause.getMessage()); cause = cause.getCause(); } } finally { StandardServiceRegistryBuilder.destroy(registry); } } @Test void transformHbmXmlEnabled_bootingWithHbmXml_printsWhatHappens() { StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .applySetting("hibernate.connection.driver_class", "org.h2.Driver") .applySetting("hibernate.connection.url", "jdbc:h2:mem:hbmtest2;DB_CLOSE_DELAY=-1") .applySetting("hibernate.connection.username", "sa") .applySetting("hibernate.connection.password", "") .applySetting("hibernate.hbm2ddl.auto", "create") .applySetting(MappingSettings.TRANSFORM_HBM_XML, "true") .build(); try { Metadata metadata = new MetadataSources(registry) .addResource("com/ankurm/hibernatedemo/mappingstyle/HbmEmployee.hbm.xml") .buildMetadata(); SessionFactory sf = metadata.buildSessionFactory(); System.out.println("RESULT[transform=true]: BOOT SUCCEEDED, SessionFactory built WITH " + MappingSettings.TRANSFORM_HBM_XML + "=true"); sf.close(); } catch (Throwable t) { System.out.println("RESULT[transform=true]: BOOT FAILED: " + t.getClass().getName() + ": " + t.getMessage()); Throwable cause = t.getCause(); while (cause != null) { System.out.println("RESULT[transform=true]: caused by: " + cause.getClass().getName() + ": " + cause.getMessage()); cause = cause.getCause(); } } finally { StandardServiceRegistryBuilder.destroy(registry); } } }