Spring Batch AbstractPagingItemReader issue if no order by is defined

Spring Batch allows to define a reader based on a JPA/Hibernate/SQL request via JpaPagingItemReader/HibernatePagingItemReader/JdbcPagingItemReader.

The default pageSize in AbstractPagingItemReader is 10, so if there is more than 10 result lines, a new request will be executed by the reader.

If the request does not contains an order by, the result can contains twice a record because the request with non order by is non determinist on some database.

I think a rule checking that non order by are defined would be valuable in sonarqube.

Hello @Max_well, welcome to the Sonar Community!

Thanks for proposing such an interesting rule. Could you provide us with some code examples that should trigger the rule? This will speed up the specification of the rule.


Hello !

I think it boils down to:

String jpqlQuery = "select f from Foo f";

JpaPagingItemReader<Foo> reader = new JpaPagingItemReader<>();

or, if using xml spring configuration

<bean id="myBean" class="org.springframework.batch.item.database.JpaPagingItemReader" scope="step">
	<property name="queryString">
			<![CDATA[select f from Foo f]]>

with the query string missing an “order by” in both case.

HibernatePagingItemReader is deprecated in Hibernate 5.0 so probably out of scope of a rule.
And looking into JdbcPagingItemReader, it’s seems more complex, and as I’m not familiar with it, I’m not confident enough to give examples.

1 Like

Thank you @Max_well, for providing more details.

The [SONARJAVA-4737] - Jira to implement the rule has been added to the backlog.