java - 带有多个数据源的 Camel + Spring Boot + JPA 找不到正确的持久性单元
问题描述
我有一个使用 JPA 和 Apache camel 的 spring boot 服务。我在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 与多个数据源进行了比较
数据源 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;
}
}
骆驼路线摘录:
...
.to("jpa:com.myapp.model.domain.Event?entityType=java.util.ArrayList&persistenceUnit=domain")
解决方案
使用 entityManagerFactory 选项的 persistenceUnit 实例
推荐阅读
- node.js - nodejs 编写一个程序,在指定时间执行对 HTTP 端点的调用
- python - 什么是仅读取文本文件中的完整单词的python代码(词法分析仅检测整个单词)?
- html - 当高度增加时,如何使文本垂直居中?
- python - 如果任何“单元格”匹配 3 个字符串之一,则寻找 Pythonic 方法来删除 Pandas 数据框中的行
- ios - 如何在自己的完成处理程序中使用变量
- python - 无法运行 MongoDB 聚合命令 (OperationFailure)
- microservices - Kubernetes-在 CICD 中的多个集群之间切换上下文的最佳方法是什么?
- c# - 如何生成 guid 并将其分配给变量?
- r - 使用 purrr::map 对列进行分组并获取频率
- c++ - 尽管包含 -g 标志,gdb 在中断命令后抛出“没有名为 handleChars.cpp 的源文件”