使用 JPA 2.1 从来自两个不同模式的表中生成实体
Generating entities from tables from two different schemas with JPA 2.1
我目前正在尝试弄清楚如何在我的项目中使用两种不同的模式:目前我可以从我在 Eclipse 上创建的两个不同的数据源成功生成我的实体
基本上它是同一台服务器,但我不得不使用两个不同的连接字符串来访问这两个模式。
问题在于,通过切换连接以从其他模式生成实体,无法识别先前的实体:
有没有办法解决这个问题?有没有办法让我的实体无论如何都能被识别?
编辑:
我最终创建了 2 个额外的 JPA 项目,在其中生成我的实体,然后我将这 2 个项目添加到主项目的 POM 中,但它仍然只读取一个持久性单元,并且无法识别来自另一个模式的实体。
忘了回答这个问题,我终于找到了解决这个问题的方法。
在 application.properties
中,我们为模式 modules
和 supplychain
添加了属性,我为这两个模式创建了 2 个 JPA 项目,并在那里生成了实体。
之后,我们在SpringServletInitializer中创建bean:
private Map<String, String> jpaProperties() {
Map<String, String> p = new HashMap<String, String>();
p.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
p.put("hibernate.id.new_generator_mappings",
env.getProperty("spring.jpa.properties.hibernate.id.new_generator_mappings"));
p.put("hibernate.format_sql", env.getProperty("spring.jpa.properties.hibernate.format_sql"));
p.put("hibernate.naming.physical-strategy", env.getProperty("spring.jpa.hibernate.naming.physical-strategy"));
p.put("hibernate.ddl-auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
return p;
}
@Bean
@ConfigurationProperties(prefix = "modules.datasource")
public DataSource modulesDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
@ConfigurationProperties(prefix = "supplychain.datasource")
public DataSource supplychainDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name="modules")
public LocalContainerEntityManagerFactoryBean modulesEntityManagerFactory(
org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder builder) throws IOException {
return builder.dataSource(modulesDataSource()).packages(Moduli.class).properties(jpaProperties()).persistenceUnit("JPAModules").build();
}
@Bean(name="supplychain")
@Primary
public LocalContainerEntityManagerFactoryBean supplychainEntityManagerFactory(EntityManagerFactoryBuilder builder)
throws IOException {
return builder.dataSource(supplychainDataSource()).packages(Rent.class).properties(jpaProperties()).persistenceUnit("JPASupplychain").build();
}
@Bean(name = "modulesTransactionManager")
public PlatformTransactionManager modulesTransactionManager(EntityManagerFactoryBuilder builder)
throws IOException {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(modulesEntityManagerFactory(builder).getObject());
tm.setDataSource(modulesDataSource());
return tm;
}
@Bean(name = "supplychainTransactionManager")
@Primary
public PlatformTransactionManager supplychainTransactionManager(EntityManagerFactoryBuilder builder)
throws IOException {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(supplychainEntityManagerFactory(builder).getObject());
tm.setDataSource(supplychainDataSource());
return tm;
}
添加两个数据源,我终于可以使用两个持久化了!
我目前正在尝试弄清楚如何在我的项目中使用两种不同的模式:目前我可以从我在 Eclipse 上创建的两个不同的数据源成功生成我的实体
基本上它是同一台服务器,但我不得不使用两个不同的连接字符串来访问这两个模式。
问题在于,通过切换连接以从其他模式生成实体,无法识别先前的实体:
有没有办法解决这个问题?有没有办法让我的实体无论如何都能被识别?
编辑: 我最终创建了 2 个额外的 JPA 项目,在其中生成我的实体,然后我将这 2 个项目添加到主项目的 POM 中,但它仍然只读取一个持久性单元,并且无法识别来自另一个模式的实体。
忘了回答这个问题,我终于找到了解决这个问题的方法。
在 application.properties
中,我们为模式 modules
和 supplychain
添加了属性,我为这两个模式创建了 2 个 JPA 项目,并在那里生成了实体。
之后,我们在SpringServletInitializer中创建bean:
private Map<String, String> jpaProperties() {
Map<String, String> p = new HashMap<String, String>();
p.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
p.put("hibernate.id.new_generator_mappings",
env.getProperty("spring.jpa.properties.hibernate.id.new_generator_mappings"));
p.put("hibernate.format_sql", env.getProperty("spring.jpa.properties.hibernate.format_sql"));
p.put("hibernate.naming.physical-strategy", env.getProperty("spring.jpa.hibernate.naming.physical-strategy"));
p.put("hibernate.ddl-auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
return p;
}
@Bean
@ConfigurationProperties(prefix = "modules.datasource")
public DataSource modulesDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
@ConfigurationProperties(prefix = "supplychain.datasource")
public DataSource supplychainDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name="modules")
public LocalContainerEntityManagerFactoryBean modulesEntityManagerFactory(
org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder builder) throws IOException {
return builder.dataSource(modulesDataSource()).packages(Moduli.class).properties(jpaProperties()).persistenceUnit("JPAModules").build();
}
@Bean(name="supplychain")
@Primary
public LocalContainerEntityManagerFactoryBean supplychainEntityManagerFactory(EntityManagerFactoryBuilder builder)
throws IOException {
return builder.dataSource(supplychainDataSource()).packages(Rent.class).properties(jpaProperties()).persistenceUnit("JPASupplychain").build();
}
@Bean(name = "modulesTransactionManager")
public PlatformTransactionManager modulesTransactionManager(EntityManagerFactoryBuilder builder)
throws IOException {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(modulesEntityManagerFactory(builder).getObject());
tm.setDataSource(modulesDataSource());
return tm;
}
@Bean(name = "supplychainTransactionManager")
@Primary
public PlatformTransactionManager supplychainTransactionManager(EntityManagerFactoryBuilder builder)
throws IOException {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(supplychainEntityManagerFactory(builder).getObject());
tm.setDataSource(supplychainDataSource());
return tm;
}
添加两个数据源,我终于可以使用两个持久化了!