【发布时间】:2017-10-05 10:52:56
【问题描述】:
我有一个带有 2 个数据源的 Spring Boot 项目。首先使用该项目中的实体,我需要第二个数据源,该数据源使用从另一个项目作为 Maven 依赖项导入的 JPA 实体。我使用 this example 创建了 2 个数据源。但由于错误,我无法运行项目:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name '(inner bean)#69ebb490':
Unsatisfied dependency expressed through method 'createSharedEntityManager' parameter 0: Could not convert argument value of type [org.springframework.orm.jpa.JpaTransactionManager] to required type [javax.persistence.EntityManagerFactory]:
Failed to convert value of type 'org.springframework.orm.jpa.JpaTransactionManager' to required type 'javax.persistence.EntityManagerFactory'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type 'org.springframework.orm.jpa.JpaTransactionManager' to required type 'javax.persistence.EntityManagerFactory': no matching editors or conversion strategy found
我对导入的实体有以下配置(我创建了相同的包结构,以将我的服务放入我的应用程序中与导入实体所在的相同包中):
import com.company.second.db.entities.MyImportedEntity;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = {"com.company.second.db", "com.company.second.db.entities"},
entityManagerFactoryRef = "secondTransactionManager",
transactionManagerRef = "secondEntityManagerFactory"
)
public class SecondDataSourceConfig {
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "second.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean secondEntityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("secondDataSource") DataSource dataSource) {
final Map<String, String> properties = new HashMap<>();
properties.put("hibernate.archive.autodetection", "class, hbm");
return builder.dataSource(dataSource)
.packages("com.company.second.db", "com.company.second.db.entities")
.properties(properties).packages(MyImportedEntity.class)
.persistenceUnit("second")
.build();
}
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager SecondTransactionManager(
@Qualifier("secondEntityManagerFactory") EntityManagerFactory secondEntityManagerFactory) {
return new JpaTransactionManager(secondEntityManagerFactory);
}
}
导入实体示例:
package com.company.second.db.entities;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.util.List;
@Data
@Entity
@Table(name = "my_imported_entity")
public class MyImportedEntity {...}
另外,我尝试将 Service 配置为使用此导入的实体:
package com.company.second.db.service.impl;
import com.company.second.db.entities.MyImportedEntity;
import com.company.second.db.entities.repository.MyImportedEntityRepository;
import com.company.second.db.service.MyImportedEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Service
@Transactional(value = "secondTransactionManager", readOnly = true, propagation = Propagation.REQUIRED)
public class MyImportedEntityServiceImpl implements MyImportedEntityService { ... }
我的辅助配置与辅助配置相同,但带有 @Primary 注释。没有这个错误,它工作正常:
Cannot convert value of type 'org.springframework.orm.jpa.JpaTransactionManager' to required type 'javax.persistence.EntityManagerFactory': no matching editors or conversion strategy found
我该如何解决这个问题?
【问题讨论】:
标签: java spring jpa spring-data spring-data-jpa