【问题标题】:Error in JpaTransactionManager while using imported entities使用导入的实体时 JpaTransactionManager 出错
【发布时间】: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


    【解决方案1】:

    看这里:

    entityManagerFactoryRef = "secondTransactionManager",
    transactionManagerRef = "secondEntityManagerFactory"
    

    看起来你把豆子放错了,应该是:

    entityManagerFactoryRef = "secondEntityManagerFactory",
    transactionManagerRef = "secondTransactionManager"
    

    这就是为什么Spring 尝试:convert value of type 'org.springframework.orm.jpa.JpaTransactionManager' to required type 'javax.persistence.EntityManagerFactory'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 2014-10-09
      • 2011-03-23
      • 2013-09-15
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      相关资源
      最近更新 更多