如何将多个 JPA 持久性上下文与 Spring Data JPA 一起使用?

How to use multiple JPA persistence contexts with Spring Data JPA?

我需要两个持久性上下文来管理不同数据库中的实体。

我正在使用基于 java 的配置以及 spring 4.1.4、休眠 4.3.7 和 spring 数据 1.2。

基本上,CompanyApplicationContext 导入两个持久性上下文:PersistenceContext 和 SharedDbPersistenceContext。每个持久性上下文都有自己的数据源、事务管理器和实体管理器工厂。每个持久性上下文的实体和 jpa 存储库存在于单独的包中,并在持久性上下文 class 文件中提及。

我收到这个错误:

Not an managed type: class com.company.shared.model.ClientSetting

完全异常:

Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.company.shared.model.ClientSetting
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219)

我需要什么额外的配置才能让它工作?为什么每个上下文都没有正确扫描和注册实体和存储库?

主要应用上下文:

package com.company.config;
@Configuration
@ComponentScan(basePackages
        = "com.company"
)
@Import({
    PersistenceContext.class,
    SharedDbPersistenceContext.class})
public class CompanyApplicationContext extends WebMvcConfigurerAdapter {

}

持久性上下文:

 package com.company.config;
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            basePackages = {"com.company.todo.repository"})
    public class PersistenceContext {

        @Resource
        private Environment environment;

        @Bean
        public DataSource dataSource() {
            BoneCPDataSource dataSource = new BoneCPDataSource();
            //init data source
            return dataSource;
        }


        @Bean
        public JpaTransactionManager transactionManager() {
            JpaTransactionManager transactionManager = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
            return transactionManager;
        }

        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

            entityManagerFactoryBean.setDataSource(dataSource());
            entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
            entityManagerFactoryBean.setPackagesToScan("com.company.todo.model");
            Properties jpaProperties = new Properties();
            //init jpa properties
            entityManagerFactoryBean.setJpaProperties(jpaProperties);
            return entityManagerFactoryBean;
        }

    }

共享数据库持久性上下文:

    package com.company.config;
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            basePackages = {"com.company.shared.repository"})
    public class SharedDbPersistenceContext {

        @Resource
        private Environment environment;

        @Bean
        public DataSource sharedDataSource() {
            //init and return data source
        }

        @Bean
        public JpaTransactionManager sharedDbTransactionManager() {
//init transaction manager
        }

        @Bean
        public LocalContainerEntityManagerFactoryBean sharedEntityManagerFactory() {
            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

            entityManagerFactoryBean.setDataSource(sharedDataSource());
            entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
            entityManagerFactoryBean.setPackagesToScan("com.company.shared.model");
            Properties jpaProperties = new Properties();
            //init jpaProperties
            entityManagerFactoryBean.setJpaProperties(jpaProperties);
            return entityManagerFactoryBean;
        }
    }

存储库:

package com.company.shared.repository;
@Repository
public interface ClientSettingsRepository extends JpaRepository<ClientSetting, Integer> {}

实体:

package com.company.shared.model;
@Entity
public class ClientSetting implements Serializable {
//props
}

服务:

package com.company.shared.service;
@Service
public class DataSourceServiceImpl implements DataSourceService{

    @Autowired
    ClientSettingsRepository clientSettingsRepository;

    @Override
    public List<ClientSetting> findClientDataSourceSettings() {

        return clientSettingsRepository.findClientDataSourceSettings();
    }

}

原来我不见了 entityManagerFactoryReftransactionManagerRef @EnableJpaRepositories 注释

 @Configuration
 @EnableTransactionManagement
 @EnableJpaRepositories(
       basePackages = {"com.company.todo.repository"},
       entityManagerFactoryRef = "mainEntityManagerFactory",
       transactionManagerRef = "entityManagerFactory")
       public class PersistenceContext {}

SharedDbPersistenceContext :

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
       basePackages = {"com.company.shared.repository"},
       entityManagerFactoryRef = "sharedEntityManagerFactory",
       transactionManagerRef = "sharedDbTransactionManager")
       public class SharedDbPersistenceContext {}

工作正常。