gpt4 book ai didi

java - Spring Boot 加载 orm.xml

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:42 25 4
gpt4 key购买 nike

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

最佳答案

是的。如果 orm.xml 存在于结果 jar 的 META-INF 目录中,Spring Boot 应该默认加载它。
否则,可以通过创建自定义 LocalContainerEntityManagerFactoryBean 手动加载它,如下所示:

应用程序属性

spring.jpa.orm=orm.xml

DBConfig.java(如果您使用 Hibernate 作为持久性提供程序)

@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;
}

}

我发现在实体保持干净的同时将所有映射移动到外部 xml 文件对我来说非常有用。

关于java - Spring Boot 加载 orm.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32062828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com