package com.ankurm.hibernatedemo.mappingstyle; import static org.assertj.core.api.Assertions.assertThat; import com.ankurm.hibernatedemo.HibernateDemoApplication; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; /** * Proves that {@code spring.jpa.mapping-resources} is the Spring Boot 4.1.1 wiring that makes * an orm.xml-only mapping (no {@code @Entity} at all) a real managed JPA entity, backed by a * table ({@code xml_only_widgets}) that ONLY orm.xml knows about. * *

Docs: docs/04-annotations-vs-xml.md, Topic 1. */ @SpringBootTest(classes = HibernateDemoApplication.class) @TestPropertySource(properties = { "spring.jpa.mapping-resources=orm-xml-only-mapping.xml" }) class OrmXmlMappingResourcesTest { @Autowired private EntityManagerFactory emf; @Test void ormXmlOnlyEntity_isManaged_andRoundTripsThroughItsOwnTable() { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); OrmXmlOnlyEntity e = new OrmXmlOnlyEntity("mapped-by-orm-xml-only"); em.persist(e); em.getTransaction().commit(); Long id = e.getId(); em.close(); assertThat(id).isNotNull(); EntityManager em2 = emf.createEntityManager(); // This JPQL uses the entity name (defaults to simple class name) -- proves the entity is // registered in metadata purely via orm.xml, with zero annotations on the Java class. List found = em2.createQuery( "select o from OrmXmlOnlyEntity o where o.label = :label", OrmXmlOnlyEntity.class) .setParameter("label", "mapped-by-orm-xml-only") .getResultList(); em2.close(); assertThat(found).hasSize(1); assertThat(found.get(0).getId()).isEqualTo(id); System.out.println("RESULT[orm-xml-only]: persisted+queried id=" + id + " via JPQL against entity mapped ENTIRELY by orm.xml (table xml_only_widgets), zero annotations on the Java class."); } }