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

Activity

Show:
Alexander Shulgin
February 20, 2018, 2:23 PM
Edited

Enjoy!

Nathan Xu
December 21, 2019, 7:19 PM

I ran the above testing case locally with current Hibernate (v5.4.10) and found it is green. FYI.

Zunaira Shafique
September 11, 2020, 2:01 PM

Hi , all I have run these test cases and the issue is still there. this issue is been open since 2019 and we are still facing it in our project. can someone please take a look into it?

Nathan Xu
September 15, 2020, 3:39 PM
Edited

I reran the testing case and I think I figured out why the failure cannot be reproduced (with the help of HHH-14215). According to the following code snippet (from org.hibernate.query.internal.AbstractProducedQuery#beforeQuery():

As you can see, the precondition that UnsupportedOperationException could be thrown is either cacheStoreMode or cacheRetrieveMode is not null.

I changed the statement:

to

and then the issue could be reproduced successfully!

So it seems the issue is not that serious if you ensure the query has no cache mode specified. It is definitively an imperfection for CacheMode.IGNORE should not trigger exception for stateless session. I am gonna fix this soon provided that is the only issue exposed.

Nathan Xu
September 15, 2020, 4:07 PM

PR created at . Currently only testing case included. Will work on it later when I got more feedback here.

Assignee

Nathan Xu

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

Priority

Critical
Configure