春季批处理和具有两个数据库的JPA

问题描述

我有两个使用MysqL数据库读取器和一个使用spring h2的writer的春季批处理。因此,两者都配置有模型和各自的存储库。 即使模型完全相同,也可以将其读为模型列表,同时以相同的模型抛出类型转换错误将其写入另一个回购中。 因此,我尝试用n数字类型将对象保存起来,但仍然会引发错误。感谢您的帮助。 错误

java.lang.IllegalArgumentException: UnkNown entity: net.guides.springboot2.springboot2jpacrudexample.model.Employee
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:787) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] 

ItemReader:

@Autowired
    private EmployeeRepository employeeRepositoryMysqL;
        @Override
    public Employee1 read() throws Exception,UnexpectedInputException,ParseException,NonTransientResourceException
    {
    
        return employeeRepositoryMysqL.findAll;
    }

项目编写者:: -此处是值的来源,但由于类型转换而在保存时弹出错误

 @Autowired
    private EmployeeRepository2 employeeRepositoryH2;

    @Override
    public void write(List<? extends Employee2> Employees) throws Exception {
      employeeRepositoryH2.saveAll(Employees);
    }

模型1:

@Entity
@Table(name = "employees")
public class Employee1 {

    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "USER_SEQ_GENERATOR")
    private Long id;
    private String first_name;
    private String last_name;
    private String email_address;
    public Employee1() {
        
    }
    

    @Id
    /* @GeneratedValue(strategy=GenerationType.SEQUENCE) */
    
    @SequenceGenerator(
          name = "USER_SEQ_GENERATOR",sequenceName = "USER_SEQ",initialValue = 1,allocationSize = 1)
    
    public Long getId() {
        return id;
    }
    public void setId(int i) {
        this.id = id;
    }

    public String getFirst_name()
    {
        return first_name;
    }

    public void setFirst_name(String first_name)
    {
        this.first_name = first_name;
    }

    public String getLast_name()
    {
        return last_name;
    }

    public void setLast_name(String last_name)
    {
        this.last_name = last_name;
    }

    public String getEmail_address()
    {
        return email_address;
    }

    public void setEmail_address(String email_address)
    {
        this.email_address = email_address;
    }

Model2:

Entity
@Table(name = "employees")
public class Employee2 {

    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "USER_SEQ_GENERATOR")
    private BigInteger id;
    private String first_name;
    private String last_name;
    private String email_address;
    public Employee2() {
        
    }
    

    @Id
    /* @GeneratedValue(strategy=GenerationType.SEQUENCE) */
    
    @SequenceGenerator(
          name = "USER_SEQ_GENERATOR",allocationSize = 1)
    
    public BigInteger getId() {
        return id;
    }
    public void setId(BigInteger id) {
        this.id = id;
    }


    public String getFirst_name()
    {
        return first_name;
    }


    public void setFirst_name(String first_name)
    {
        this.first_name = first_name;
    }


    public String getLast_name()
    {
        return last_name;
    }


    public void setLast_name(String last_name)
    {
        this.last_name = last_name;
    }


    public String getEmail_address()
    {
        return email_address;
    }


    public void setEmail_address(String email_address)
    {
        this.email_address = email_address;
    }

解决方法

即使模型是相同的,也要作为模型列表阅读,同时以相同的模型抛出类型转换错误写入另一个仓库中

模型相同(概念上相同的字段),但类别不同:Employee1Employee2

在没有处理器的面向块的步骤 中,将读取器返回的项目以块的形式“交给”编写器(用于批量更新)。这意味着它们必须是同一类型。但是,这不是必需的。如果阅读项目与书面项目的类型不同,则可以使用ItemProcessor进行转换。数据转换是项目处理器的typical use case

因此,在您的情况下,您需要在步骤中添加ItemProcessor<Employee1,Employee2>