【问题标题】:spring batch and JPA with two databasesspring batch 和 JPA 有两个数据库
【发布时间】:2020-12-06 23:12:02
【问题描述】:

我有两个使用 MySQL 的数据库读取器和使用 spring h2 的写入器的 spring 批处理。所以两者都配置了模型和各自的存储库。 即使模型是相同的,也可以作为模型列表读取,同时写入另一个具有相同模型的 repo 会抛出类型转换错误。 所以我尝试了 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;
    }

Item writer:: - 这里是值的来源,但由于类型转换而在保存时弹出错误!

 @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;
    }

模型2:

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",
          sequenceName = "USER_SEQ",
          initialValue = 1, 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;
    }

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa spring-batch


    【解决方案1】:

    即使模型是相同的,读取为模型列表,同时写入具有相同类型的模型的另一个 repo 抛出类型转换错误

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

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

    因此,在您的情况下,您需要在步骤中添加 ItemProcessor&lt;Employee1, Employee2&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2021-02-25
      相关资源
      最近更新 更多