首页 > 解决方案 > 具有多个数据源的 Spring Boot + Camel JPA

问题描述

我需要创建一个轮询数据库的 Camel 路由,转换检索到的数据,然后将新实体插入另一个数据库。我需要有关配置的帮助。

这些是 jpa 端点:

from("jpa://" + Entity1.class.getName()
            + "?"
            + "persistenceUnit=entity1PU&"
            + "consumer.namedQuery=query1&"
            + "consumeDelete=false"
            )
        //various operations... 
        .to("direct:route2");

from("direct:route2")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                //processing...
            }
        })
        .to("jpa://" + Entity2.class.getName()
                + "?"
                + "persistenceUnit=entity2PU&"
                + "entityType=java.util.ArrayList&"
                + "usePersist=true&"
                + "flushOnSend=true");

我想通过代码和注释来配置持久性单元,而不是使用 persistence.xml;这些是相对类。这是第一个:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    basePackages = "com.foo.entity1.repo", 
    entityManagerFactoryRef = "entity1EntityManagerFactory",
    transactionManagerRef = "entity1TransactionManager"
)
public class Entity1PersistenceConfig {

    @Autowired
    @Qualifier("datasource1")
    private DataSource dataSource;

    @Primary
    public DataSource dataSource() {
        return this.dataSource;
    }

    @Primary
    @Bean(name="entity1EntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();      
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.foo.entity1.domain");
        factory.setDataSource(this.dataSource());

        factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        factory.setPersistenceUnitName("entity1PU");

        Properties hibernateProps = setJpaHibernateCommonProperties();
        hibernateProps.setProperty("hibernate.dialect", environment.getProperty("spring.jpa.properties.hibernate.oracle.dialect"));
        factory.setJpaProperties(hibernateProps);
        return factory;
    }

    @Primary
    @Bean(name="entity1TransactionManager")
    public PlatformTransactionManager transactionManager() {

        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(entityManagerFactory().getObject());

        return jpaTransactionManager;       
    }
}

第二个:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    basePackages = "com.foo.entity2.repo", 
    entityManagerFactoryRef = "entity2EntityManagerFactory",
    transactionManagerRef = "entity2TransactionManager"
)
public class Entity2PersistenceConfig {

    @Autowired
    @Qualifier("datasource2")
    private DataSource dataSource;

    public DataSource dataSource() {
        return this.dataSource;
    }

    @Bean(name="entity2EntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();      
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.foo.entity2.domain");
        factory.setDataSource(this.dataSource());

        factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        factory.setPersistenceUnitName("entity2PU");

        Properties hibernateProps = setJpaHibernateCommonProperties();
        hibernateProps.setProperty("hibernate.dialect", environment.getProperty("spring.jpa.properties.hibernate.mysql.dialect"));
        factory.setJpaProperties(hibernateProps);
        return factory;
    }

    @Bean(name="entity2TransactionManager")
    public PlatformTransactionManager transactionManager() {

        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(entityManagerFactory().getObject());

        return jpaTransactionManager;       
    }
}

实体和存储库位于正确的包中;此外,数据库的配置在特定类中正确完成,并且被正确注入。

当我尝试运行该项目时,我得到以下信息:

2018-05-30 11:38:36.481  INFO 1056 --- [main] o.h.j.b.internal.PersistenceXmlParser: HHH000318: Could not find any META-INF/persistence.xml file in the classpath

 javax.persistence.PersistenceException: No Persistence provider for EntityManager named entity1PU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61) ~[hibernate-jpa-2.1-api-1.0.0.Final.jar:1.0.0.Final]
at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:96) ~[spring-orm-4.3.17.RELEASE.jar:4.3.17.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:384) ~[spring-orm-4.3.17.RELEASE.jar:4.3.17.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371) ~[spring-orm-4.3.17.RELEASE.jar:4.3.17.RELEASE]
at org.apache.camel.component.jpa.JpaEndpoint.createEntityManagerFactory(JpaEndpoint.java:552) ~[camel-jpa-2.21.1.jar:2.21.1]
at org.apache.camel.component.jpa.JpaEndpoint.getEntityManagerFactory(JpaEndpoint.java:250) ~[camel-jpa-2.21.1.jar:2.21.1]
at org.apache.camel.component.jpa.JpaEndpoint.validate(JpaEndpoint.java:545) ~[camel-jpa-2.21.1.jar:2.21.1]
at org.apache.camel.component.jpa.JpaEndpoint.createConsumer(JpaEndpoint.java:165) ~[camel-jpa-2.21.1.jar:2.21.1]
at org.apache.camel.impl.EventDrivenConsumerRoute.addServices(EventDrivenConsumerRoute.java:69) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.impl.DefaultRoute.onStartingServices(DefaultRoute.java:103) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.impl.RouteService.doWarmUp(RouteService.java:172) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.impl.RouteService.warmUp(RouteService.java:145) ~[camel-core-2.21.1.jar:2.21.1]

为什么要寻找persistence.xml 文件而不是使用注解?我正在使用 Spring Boot 1.5.13.RELEASE 和 Camel 2.21.1。

标签: spring-bootjpaapache-camel

解决方案


基本上为了让它工作,我不得不放弃使用注释并创建一个等效的 persistence.xml 文件,因为 Apache Camel 只在其中寻找配置。

一旦我可以升级到 Spring Boot 2(因为 Apache Camel 得到更新并开始支持它),我设法通过注释进行配置并删除了 persistence.xml 文件。


推荐阅读