当前位置: 首页 > 面试题库 >

Spring Boot Load orm.xml

南宫建白
2023-03-14
问题内容

我想将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>

问题答案:

好的错误警报是,Spring Boot默认情况下会加载它,但是我的Intellij / Gradle设置还有另一个问题,即它没有部署最新的build /
WAR,因此没有orm.xml进行了部署。我必须进行gradle清理,并使Intellij中的缓存无效并重新启动。



 类似资料:

相关阅读

相关文章

相关问答