我有一个包含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 {...}
此外,我尝试配置服务以使用此导入的实体:
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
我该如何解决这个问题?
答案 0 :(得分:0)
看看这里:
entityManagerFactoryRef = "secondTransactionManager",
transactionManagerRef = "secondEntityManagerFactory"
看起来你把豆放错了,应该是:
entityManagerFactoryRef = "secondEntityManagerFactory",
transactionManagerRef = "secondTransactionManager"
这就是Spring
尝试:convert value of type 'org.springframework.orm.jpa.JpaTransactionManager' to required type 'javax.persistence.EntityManagerFactory'