Spring Boot Load orm.xml

11,003

Solution 1

Okay false alarm, it appears Spring Boot loads it by default, however my Intellij / Gradle setup had another issue, where it was not deploying the latest build / WAR so it deployed without the orm.xml. I had to do a gradle clean, and invalidate the cache in Intellij and restart.

Solution 2

Yeah. Spring Boot should load it by default if orm.xml exists in META-INF directory in result jar.
Otherwise it's possible to load it manually by creating custom LocalContainerEntityManagerFactoryBean as follows below:

Application.properties

spring.jpa.orm=orm.xml

DBConfig.java (In case you use Hibernate as a persistence provider)

@Configuration
public class DBConfig extends HibernateJpaAutoConfiguration {

@Value("${spring.jpa.orm}")
private String orm;

@SuppressWarnings("SpringJavaAutowiringInspection")
public DBConfig(DataSource dataSource, JpaProperties jpaProperties, ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
    super(dataSource, jpaProperties, jtaTransactionManagerProvider);
}

@Override
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        EntityManagerFactoryBuilder factoryBuilder)
{
    final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = super.entityManagerFactory(factoryBuilder);
    entityManagerFactoryBean.setMappingResources(orm);
    return entityManagerFactoryBean;
}

}

I found it quite usefull for me to move all mappings to external xml-files while entities stay clean.

Share:
11,003
greyfox
Author by

greyfox

I'm a web developer from Columbus, Ohio. I studied Computer Science at Capital University in Columbus, where I received my bachelor's degree. In college I was heavy into C++ and Python. I dabbled my hands in Objective-C/Cocoa as well. After college I began doing web development using PHP/MySQL. I really fell in love with web development. Now I'm transitioning into Java/Spring MVC. At some point I would like to get more into ASP.NET MVC.

Updated on June 05, 2022

Comments

  • greyfox
    greyfox almost 2 years

    I want to externalize my custom mappings for Jpa to an XML file. I've seen examples of using orm.xml however when I create orm.xml in resources/META-INF it does not appear to be loaded. All the examples I've seen it gets loaded through persistence.xml, which I don't have with Spring Boot. I didn't turn up much searching for answers. How do I make Spring Boot load my orm.xml file?

    Example orm.xml

    <entity-mappings version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm
        http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">
    <sql-result-set-mapping name="AuthorMappingXml">
        <entity-result entity-class="org.thoughts.on.java.jpa.model.Author">
            <field-result name="id" column="authorId"/>
            <field-result name="firstName" column="firstName"/>
            <field-result name="lastName" column="lastName"/>
            <field-result name="version" column="version"/>
        </entity-result>
    </sql-result-set-mapping>
    <sql-result-set-mapping name="AuthorBookCountMappingXml">
        <entity-result entity-class="org.thoughts.on.java.jpa.model.Author">
            <field-result name="id" column="id"/>
            <field-result name="firstName" column="firstName"/>
            <field-result name="lastName" column="lastName"/>
            <field-result name="version" column="version"/>
        </entity-result>
        <column-result name="bookCount" class="java.lang.Long" />
    </sql-result-set-mapping>
    <sql-result-set-mapping name="BookAuthorMappingXml">
        <entity-result entity-class="org.thoughts.on.java.jpa.model.Author">
            <field-result name="id" column="authorId"/>
            <field-result name="firstName" column="firstName"/>
            <field-result name="lastName" column="lastName"/>
            <field-result name="version" column="authorVersion"/>
        </entity-result>
        <entity-result entity-class="org.thoughts.on.java.jpa.model.Book">
            <field-result name="id" column="id"/>
            <field-result name="title" column="title"/>
            <field-result name="author" column="author_id"/>
            <field-result name="version" column="version"/>
        </entity-result>
    </sql-result-set-mapping>
    <sql-result-set-mapping name="BookValueMappingXml">
        <constructor-result target-class="org.thoughts.on.java.jpa.value.BookValue">
            <column name="id" class="java.lang.Long"/>
            <column name="title"/>
            <column name="version" class="java.lang.Long"/>
            <column name="authorName"/>
        </constructor-result>
    </sql-result-set-mapping>
    </entity-mappings>