springspring-batchspring-batch-job-monitoring

JpaCursorItemReader :- Spring Batch


I am using Spring Batch to Read Data from PostgreSQL DB

using JpaCursorItemReader to Read Data from PostgreSQL Table Product .

public JpaCursorItemReader<Product> jpaCursorItemReader() {
        JpaCursorItemReader<Product> jpaCursorItemReader = 
                new JpaCursorItemReader<Product>();
        
        jpaCursorItemReader.setEntityManagerFactory(postgresqlEntityManagerFactory);
        
        jpaCursorItemReader.setQueryString("From Product");

        jpaCursorItemReader.setSaveState(true);
        
        return jpaCursorItemReader;
    }

I am getting below error

at org.springframework.batch.item.database.JpaCursorItemReader.doOpen(JpaCursorItemReader.java:124) ~[spring-batch-infrastructure-4.3.4.jar:4.3.4]
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:150) ~[spring-batch-infrastructure-4.3.4.jar:4.3.4]
    ... 34 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table "product0_"
  Position: 8

Please could anyone help to resolve this issue

I tried modifying

jpaCursorItemReader.setQueryString() , but no successes


Solution

  • You need to correct you JPQL query like this :

    public JpaCursorItemReader<Product> jpaCursorItemReader() {
            return new JpaCursorItemReader<Product>()
            .setEntityManagerFactory(postgresqlEntityManagerFactory);
            .setQueryString("SELECT p FROM Product p");
            .setSaveState(true)
            .build();
    }