简体   繁体   中英

How to make hql selects in spring-batch?

I want to use spring-batch for retrieving and processing data from a postgres db.

I have a working SQL statement that would give me the full result set (about 400k entries):

private static final String QUERY = "SELECT * FROM MyDataTable ";

Now I want to use the JpaPagingItemReader so that the data is fetched (and written elsewhere) in chunks:

JpaPagingItemReader<MyEntity> reader = new JpaPagingItemReader<>();
reader.setEntityManagerFactory(emf);
reader.setQueryString(QUERY);

But it does not work:

[] 2014-09-17 16:31:58,234 ERROR : QuerySyntaxException: unexpected token: * near line 1, column 8 [SELECT * FROM my_data_table]

I also tried SELECT FROM MyDataTable and SELECT m FROM MyDataTable m without the star. Same result. So, how can I execute that hql query with spring-batch?

By the way: the query works fine in a sql editor like pgAdmin.

SELECT m FROM MyDataTable m is almost correct (it is valid JPQL query as long as you have entity calles MyDataTable ). So, it seems that you don't have entity class named MyDataTable .

As JpaPagingItemReader#setQueryString(String) accepts JPQL queries you should make sure that you have entity class for this table and then you should use its name instead MyDataTable .

By the way - for HQL queries there's HibernatePagingItemReader .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM