gpt4 book ai didi

java - JPA:从属性创建 EntityManagerFactory

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:33 25 4
gpt4 key购买 nike

我在 JAR 项目中使用 JPA,并使用 persistence.xml 来设置我的 EntityManager。

但由于 persistence.xml 在构建后位于 JAR 中,因此用户之后更改设置非常复杂。所以我正在寻找一种解决方案,我可以通过在运行时加载的属性文件配置我的连接。

我在网上遇到了这个解决方案:

Map properties = new HashMap();

// Configure the internal EclipseLink connection pool
properties.put(JDBC_DRIVER, "oracle.jdbc.OracleDriver");
properties.put(JDBC_URL, "jdbc:oracle:thin:@localhost:1521:ORCL");
properties.put(JDBC_USER, "user-name");
properties.put(JDBC_PASSWORD, "password");

Persistence.createEntityManagerFactory("unit-name", properties);

这是我一直在寻找的解决方案,但我在这里遗漏了一件事:在我的 persistence.xml 中,我还在映射文件上声明了一个架构名称:

持久性.xml:

<persistence version="2.0" ...>
<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="..."/>
<property name="javax.persistence.jdbc.password" value="..."/>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="javax.persistence.jdbc.user" value="..."/>
</properties>
<mapping-file>META-INF/orm.xml</mapping-file>
</persistence-unit>
</persistence>

orm.xml:

<entity-mappings ...>
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>SCHEMA_NAME</schema>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>

所以我的问题基本上是:是否有一个属性可用于在运行时设置架构,就像我对其他属性所做的那样?

或者有更好的解决方案吗?

提前致谢!

最佳答案

切换到java配置。然后你可以通过 Autowiring 环境轻松地注入(inject)属性值

这个例子非常基础。但一般来说,如果您知道如何进行 xml 配置,则可以将其直接映射到 Java 配置上

contextConfig.java

/**
* Spring Context configuration.
*/
@ComponentScan(basePackages = { "com.example" })
@PropertySource({ "classpath:common.properties" })
@Configuration
@Import(JpaConfig.class)
public class ContextConfig extends WebMvcConfigurerAdapter {
/**
* This bean is needed because Spring when you use xml config to load property files the bean is automatically
* created... when you use @PropertySource then not so much
* @return new bean
*/
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}

jpaConfig.java

@Configuration
@EnableJpaRepositories("com.example.repository")
public class JpaConfig {

@Autowired
private Environment env;

/**
* Create the fooDataSource Bean.
* @return fooDataSource Bean
*/
@Bean
public BasicDataSource fooDataSource() {

BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName(env.getProperty("cfg_foo.driver.name"));
basicDataSource.setUrl(env.getProperty("cfg_foo.jdbc.url"));
basicDataSource.setUsername(env.getProperty("cfg_foo.username"));
basicDataSource.setPassword(env.getProperty("cfg_foo.password"));
basicDataSource.setPoolPreparedStatements(Boolean.valueOf(env.getProperty("cfg_foo.poolPreparedStatements")));
basicDataSource.setInitialSize(Integer.valueOf(env.getProperty("cfg_foo.poolInitialSize")));
basicDataSource.setMaxActive(Integer.valueOf(env.getProperty("cfg_foo.poolMaxActive")));
basicDataSource.setMaxIdle(Integer.valueOf(env.getProperty("cfg_foo.poolMaxIdle")));
basicDataSource.setValidationQuery("SELECT '1'");

return basicDataSource;
}

/**
* Create the hibernateJpaVendorAdapter Bean.
* @return hibernateJpaVendorAdapter Bean
*/
@Bean
public HibernateJpaVendorAdapter hibernateJpaVendorAdapter() {

HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
adapter.setShowSql(Boolean.valueOf(env.getProperty("show.sql")));
adapter.setGenerateDdl(Boolean.valueOf(env.getProperty("format.sql")));

return adapter;
}

/**
* Create the entityManagerFactory Bean.
* @return entityManagerFactory Bean
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setPersistenceUnitName("foo");
entityManagerFactory.setDataSource(fooDataSource());
entityManagerFactory.setJpaVendorAdapter(hibernateJpaVendorAdapter());
entityManagerFactory.setPackagesToScan("com.example.repository");

return entityManagerFactory;
}

}

关于java - JPA:从属性创建 EntityManagerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17988247/

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