没有 persistence.xml 的多个 entityManagerFactory

Multiple entityManagerFactory without persistence.xml

我一直在使用 spring mvc 4 和 JPA 配置 bean 的项目中创建 java class 而没有 persistence.xml 就像下面的代码。

package test.conf;

import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class JPAConfiguration {

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.setDataSource(dataSource());    

        Properties props = new Properties();
        props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        props.setProperty("hibernate.show_sql", "false");
        props.setProperty("hibernate.hbm2ddl.auto", "update");

        factoryBean.setJpaProperties(props);
        String [] packages = {"test.model"};
        factoryBean.setPackagesToScan(packages);

        return factoryBean;
    }

    @Bean
    @Primary
    public DriverManagerDataSource dataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        dataSource.setUrl("jdbc:mysql://localhost:3306/database");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return dataSource;
    }

    @Bean
    @Primary
    public JpaTransactionManager transactionManager(EntityManagerFactory emf){
        return new JpaTransactionManager(emf);
    }

}

这工作正常,但现在,我们必须连接到第二个数据库 (ORACLE),并且需要第二个实体管理器工厂。我怎样才能做到这一点 ?我找到了在 persistence.xml 上创建第二个持久性单元的示例,但我在那个项目中没有 persistence.xml。其他人教授如何使用 spring 引导创建到多个数据源的连接,但这不是 spring 引导项目。

我试图为另一个数据库创建第二个 JPAConfiguration class,然后为每个 entitymanagerfactorybean 指定一个 factoryBean.setPersistenceUnitName("");,但是似乎 Spring 搞砸了,因为它总是将一个持久性单元初始化两次。

您需要在属性中包含第二个数据源详细信息

#second db ...
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver

然后创建您的配置 class,所有配置都将使用第二个数据源并使用第二个数据源启用 jpa 存储库。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "secondEntityManagerFactory",
        transactionManagerRef = "secondTransactionManager",
        basePackages = {"com.server.second.repo"}
)
public class SecondDBConfiguration  

@Bean("secondDatasource")
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
secondEntityManagerFactory(
        EntityManagerFactoryBuilder builder,
        @Qualifier("secondDatasource") DataSource dataSource
) {
    return
            builder
                    .dataSource(dataSource)
                    .packages("com.server.shadow.domain")
                    .persistenceUnit("shadow")
                    .build();
}
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager secondTransactionManager(
        @Qualifier("secondEntityManagerFactory") EntityManagerFactory
                secondEntityManagerFactory
) {
    return new JpaTransactionManager(secondEntityManagerFactory);
}