我有两个数据源和两个entityManagerFactory实例。我试图使用3.1的新功能(使用packagesToScan属性引导JPA entityManagerFactory而不使用persistence.xml。)
为了使用正确的实体管理器工厂实例,我必须区分使用持久性单元名称并在persistence.xml中定义PU名称正在停止弹簧包扫描功能。
如何在使用packagesToScan功能时提供PU名称?
的更多重复我找不到上述帖子的答案或评论。所以转发为新问题。
答案 0 :(得分:2)
如果我正确理解您的问题,您希望设置persistenceUnit
支持EntityManagerFactory
的名称,在没有persistence.xml
的情况下定义?
当您声明entityManagerFactory时,您可以设置persistenceUnitName属性。例如:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="yourPersistenceUnitName"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="packagesToScan">
<list>
<value>..</value>
...
</list>
</property>
</bean>
答案 1 :(得分:1)
是的,你可以。这是一个使用Spring注释配置的例子
我发现最好将每个数据源组织到一个不同的包中 我的包结构是:
datasource
|__ converters <-- holds any custom attribute converters for JPA
|__ default <-- for default datasource
| |__ model <-- contains entities for default datasource
| |__ repository <-- contains repositories for default datasource
|__ anotherdatasource <-- for second datasource
|__ model <-- contains entities for second datasource
|__ repository <-- contains repositories for second datasource
选择一个数据源作为默认值,并为其创建一个配置类...
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory", basePackages = { "com.example.datasource.default.repository" })
public class JpaDefaultDatasourceConfig {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.example.datasource.default.model", "com.example.datasource.converters").persistenceUnit("mydefault").build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
然后为每个后续数据源创建另一个配置类,沿着...... (注意:使用包来分隔实体/存储库扫描和整个使用的命名约定)< / em>的
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "anotherEntityManagerFactory", transactionManagerRef = "anotherTransactionManager", basePackages = { "com.example.datasource.anotherdatasource.repository" })
public class JpaAnotherDatasourceConfig {
@Bean(name = "anotherDataSource")
@ConfigurationProperties(prefix = "another.datasource")
public DataSource anotherDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "anotherEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean anotherEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("anotherDataSource") DataSource anotherDataSource) {
return builder.dataSource(anotherDataSource).packages("com.example.datasource.anotherdatasource.model", "com.example.datasource.converters").persistenceUnit("anotherName").build();
}
@Bean(name = "anotherTransactionManager")
public PlatformTransactionManager anotherTransactionManager(@Qualifier("anotherEntityManagerFactory") EntityManagerFactory anotherEntityManagerFactory) {
return new JpaTransactionManager(anotherEntityManagerFactory);
}
}
您可以使用配置前缀配置每个数据源。对于上面的两个示例,您可以使用
配置它们<强> application.yml 强>
## JPA configuration
# This is the configuration for default datasource created by spring
spring:
datasource:
url: jdbc:mysql://localhost/default
username: foo
password: bar
driverClassName: com.mysql.jdbc.Driver
test-on-borrow: true
test-while-idle: true
validation-query: select 1;
# maxActive: 1
# This is the configuration for an additional datasource which we will create ourselves
another:
datasource:
url: jdbc:mysql://localhost/another
username: foo
password: bar
driverClassName: com.mysql.jdbc.Driver
test-on-borrow: true
test-while-idle: true
validation-query: select 1;
你现在不必担心持久性单位的名称(虽然我们确实命名了它们),因为我们已经仔细分离了实体经理,只看他们的实体/存储库你可以简单地注入适用的存储库并使用它,而不必担心它获取错误的数据源。如果您确实需要持久性单元,您可以通过名称@PersistenceUnit(name = "anotherDatasource")