我有两个实体管理器配置用于两个单独的数据库,但当我尝试自动连接实体管理器来配置GraphQLExecutor bean时,我得到一个异常,指出有两个bean符合条件,尽管我在PersistenceContext中指定了一个单元名。
例外情况
package com.ogl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories(basePackages = "com.ogl.system", entityManagerFactoryRef = "companyEntityManagerFactory", transactionManagerRef = "companyTransactionManager")
public class SystemJpaConfig {
private final Environment environment;
@Autowired
public SystemJpaConfig(Environment environment) {
this.environment = environment;
}
@Bean("systemEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean systemEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setPackagesToScan("com.ogl.system");
entityManagerFactoryBean.setPersistenceUnitName("system");
entityManagerFactoryBean.setDataSource(systemDataSource());
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.POSTGRESQL);
adapter.setShowSql(true);
adapter.setGenerateDdl(false);
entityManagerFactoryBean.setJpaVendorAdapter(adapter);
return entityManagerFactoryBean;
}
@Bean
DataSource systemDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("p4.datasource.driver"));
dataSource.setUrl(environment.getProperty("p4.system.url"));
dataSource.setUsername(environment.getProperty("p4.system.user"));
dataSource.setPassword(environment.getProperty("p4.system.password"));
return dataSource;
}
@Bean
public PlatformTransactionManager systemTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory((systemEntityManagerFactory().getObject()));
return transactionManager;
}
}
package com.ogl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories(basePackages = "com.ogl.company", entityManagerFactoryRef = "companyEntityManagerFactory", transactionManagerRef = "companyTransactionManager")
public class CompanyJpaConfig {
private final Environment environment;
@Autowired
public CompanyJpaConfig(Environment environment) {
this.environment = environment;
}
@Primary
@Bean("companyEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean companyEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setPackagesToScan("com.ogl.company");
entityManagerFactoryBean.setPersistenceUnitName("company");
entityManagerFactoryBean.setDataSource(companyDataSource());
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.POSTGRESQL);
adapter.setShowSql(true);
adapter.setGenerateDdl(false);
entityManagerFactoryBean.setJpaVendorAdapter(adapter);
return entityManagerFactoryBean;
}
@Bean
DataSource companyDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("p4.datasource.driver"));
dataSource.setUrl(environment.getProperty("p4.company.url"));
dataSource.setUsername(environment.getProperty("p4.company.user"));
dataSource.setPassword(environment.getProperty("p4.company.password"));
return dataSource;
}
@Primary
@Bean
public PlatformTransactionManager companyTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory((companyEntityManagerFactory().getObject()));
return transactionManager;
}
}
注射
package com.ogl;
import org.crygier.graphql.GraphQLExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Configuration
@ComponentScan
public class GraphQLJpaConfig {
@PersistenceContext(unitName = "company")
private EntityManager entityManager;
@Bean
public GraphQLExecutor graphQLExecutor() {
return new GraphQLExecutor(entityManager);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ogl</groupId>
<artifactId>jpa-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jpa-demo</name>
<description>Demo project for JPA</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.crygier</groupId>
<artifactId>graphql-jpa</artifactId>
<version>0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
您定义了两个EntityManager。现在你得告诉spring应该注射哪一种。为此,您可以使用@qualifier
注释:
@PersistenceContext(unitName = "company")
@Qualifier(<Name of the entitimanager you want to use>)
private EntityManager entityManager;
我有这个超级班: 我想确定我完全理解了解决方案。通过,我给类DAOBase指定了特定的名称“daoBaseBeanname”,应用程序可以用它来标识类DAOBase,这样就不会把它与扩展DAOBase的其他类混淆了。对吗? 谢谢你。
我有这个超级班: 谢谢你。
为什么我会犯这个错误。我该怎么解决呢? 谢谢
问题内容: 我正在尝试构建一个全新的Spring Framework 4.0项目,而没有所有神奇的东西,而只是简单地将它踢过去。 我在这里关注该教程:http : //spring.io/guides/tutorials/data/并取得了一些成功。我只是停留在这一点上。 当我运行此单元测试时,得到以下堆栈跟踪: 根据观察和研究,似乎是在告诉我有两个EntityManager类。第一个来自hibe
由于Zuul不支持与SC的brixton.m3打包的版本中的补丁(https://github.com/spring-cloud/spring-cloud-netflix/issues/412),我正在尝试将一个基于Spring Boot+Spring Cloud的项目升级到brixton.m4。我启用了Spring-boot-starter-actul和spring-cloud-starter-