如何在Spring Data JPA中使用多个JPA持久化上下文?

时间:2015-01-06 05:38:39

标签: hibernate jpa spring-data spring-data-jpa

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

我使用的是基于java的配置,包含spring 4.1.4,hibernate 4.3.7和spring data 1.2。

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

我收到此错误:

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;
        }

    }

共享Db持久性上下文:

    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();
    }

}

1 个答案:

答案 0 :(得分:1)

原来我失踪了 @EnableJpaRepositories注释中的 entityManagerFactoryRef transactionManagerRef

 @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 {}

工作正常。