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