Camel + Sping Boot +具有多个数据源的JPA未找到正确的持久性单元

axzmvihb  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(134)

我有一个Sping Boot 服务,使用JPA和Apache camel。我在https://www.baeldung.com/spring-data-jpa-multiple-databases之后配置了两个不同的数据源。到目前为止,数据源本身似乎已经就位。但是camel没有根据persistenceUnit名称选择它们。当我删除与其中一个数据源相关的所有代码时,它突然工作了。对我来说,似乎persistenceUnit名称设置不正确。我总是得到以下错误:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named domain
我还比较了我使用Spring Boot + Camel JPA with multiple Datasources的方法
数据源1:

import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.PersistenceContext;
import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
@PropertySource({"classpath:database.properties"})
@EnableJpaRepositories(
        basePackages = {"com.myapp.persistence.domain"},
        entityManagerFactoryRef = "domainEntityManager",
        transactionManagerRef = "domainTransactionManager")
public class PersistenceDomainAutoConfiguration {

    @Autowired
    private Environment env;

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource domainDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    @PersistenceContext(unitName = "domain")
    public LocalContainerEntityManagerFactoryBean domainEntityManager() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(domainDataSource());
        em.setPackagesToScan("com.myapp.model.domain","com.myapp.legacy");
        em.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        em.setPersistenceUnitName("domain");
        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        final HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.dialect",
                "org.hibernate.dialect.PostgreSQLDialect");
        em.setJpaPropertyMap(properties);

        return em;
    }

    @Bean
    public PlatformTransactionManager domainTransactionManager() {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(domainEntityManager().getObject());
        return transactionManager;
    }

}

数据源2:

package com.myapp.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.PersistenceContext;
import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
@PropertySource({"classpath:database.properties"})
@EnableJpaRepositories(
        basePackages = "com.myapp.persistence.lookup",
        entityManagerFactoryRef = "lookupEntityManager",
        transactionManagerRef = "lookupTransactionManager")
public class PersistenceLookupAutoConfiguration {

    @Autowired
    private Environment env;

    @Bean
    @ConfigurationProperties(prefix="spring.currency-datasource")
    public DataSource lookupDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @PersistenceContext(unitName = "lookup")
    public LocalContainerEntityManagerFactoryBean lookupEntityManager() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(lookupDataSource());
        em.setPackagesToScan("com.myapp.model.lookup");
        em.setPersistenceUnitName("lookup");

        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        final HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
        em.setJpaPropertyMap(properties);

        return em;
    }

    @Bean
    public PlatformTransactionManager lookupTransactionManager() {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(lookupEntityManager().getObject());
        return transactionManager;
    }
}

Camelroute摘录:

...
.to("jpa:com.myapp.model.domain.Event?entityType=java.util.ArrayList&persistenceUnit=domain")
gupuwyp2

gupuwyp21#

使用实体管理器工厂选项persistenceUnit示例

相关问题