Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Load orm.xml

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>
like image 670
greyfox Avatar asked Aug 18 '15 03:08

greyfox


2 Answers

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.

like image 176
rvit34 Avatar answered Sep 21 '22 01:09

rvit34


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.

like image 29
greyfox Avatar answered Sep 20 '22 01:09

greyfox