AbstractProducedQuery trying to call setCacheMode on StatelessSessionImpl, causing UnsupportedOperationException

Description

When using Named Queries via Spring Batch's HibernateCursorItemReader, it seems AbstractProducedQuery tries to call setCacheMode on the StatelessSessionImpl, which throws an UnsupportedOperationException.

Caused by: java.lang.UnsupportedOperationException
at org.hibernate.internal.StatelessSessionImpl.setCacheMode(StatelessSessionImpl.java:367) ~[hibernate-core-5.2.11.Final.jar:5.2.11.Final]
at org.hibernate.query.internal.AbstractProducedQuery.beforeQuery(AbstractProducedQuery.java:1325) ~[hibernate-core-5.2.11.Final.jar:5.2.11.Final]
at org.hibernate.query.internal.AbstractProducedQuery.scroll(AbstractProducedQuery.java:1369) ~[hibernate-core-5.2.11.Final.jar:5.2.11.Final]
at org.hibernate.query.internal.AbstractProducedQuery.scroll(AbstractProducedQuery.java:104) ~[hibernate-core-5.2.11.Final.jar:5.2.11.Final]
at org.springframework.batch.item.database.HibernateItemReaderHelper.getForwardOnlyCursor(HibernateItemReaderHelper.java:126) ~[spring-batch-infrastructure-4.0.0.M3.jar:4.0.0.M3]
at org.springframework.batch.item.database.HibernateCursorItemReader.doOpen(HibernateCursorItemReader.java:185) ~[spring-batch-infrastructure-4.0.0.M3.jar:4.0.0.M3]
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:146) ~[spring-batch-infrastructure-4.0.0.M3.jar:4.0.0.M3]

There is a comment at the start of SharedSessionContractImplementor which suggests this may have been foreseen:

// todo : this is the shared contract between Session and StatelessSession, but it defines methods that StatelessSession does not implement
// (it just throws UnsupportedOperationException). To me it seems like it is better to properly isolate those methods
// into just the Session hierarchy. They include (at least):
// 1) get/set CacheMode

I've tried setting the following on my named queries, but it doesn't help (because of the null checking and defaulting that occurs in Hibernate)

cacheMode = CacheModeType.IGNORE,
cacheable = false

Pretty confident the issue isn't in the Spring Batch calling code, but can't be sure.

Environment

None

Status

Assignee

Unassigned

Reporter

James Leece

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.2.11

Priority

Critical
Configure