我想将Jpa的自定义映射外部化为XML文件。我已经看到了使用orm.xml的例子,但是当我在resources / META-INF中创建orm.xml时,似乎没有加载它。我看到的所有示例都是通过persistence.xml加载的,我没有使用Spring Boot。我没有多少寻找答案。如何使Spring Boot加载我的orm.xml文件?
示例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>
答案 0 :(得分:9)
呀。如果结果jar中的$result=array()
目录中存在orm.xml
,则Spring Boot应默认加载它
否则,可以通过创建自定义META-INF
手动加载它,如下所示:
Application.properties
LocalContainerEntityManagerFactoryBean
DBConfig.java(如果您使用Hibernate作为持久性提供程序)
spring.jpa.orm=orm.xml
我发现在实体保持干净的同时将所有映射移动到外部xml文件非常有用。
答案 1 :(得分:2)
好的误报,默认情况下Spring Boot会加载它,但是我的Intellij / Gradle设置还有另一个问题,那就是它没有部署最新的build / WAR,因此它没有使用orm.xml进行部署。我不得不做一个gradle clean,并在Intellij中使缓存失效并重新启动。