Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:43:06+00:00 2026-06-10T00:43:06+00:00

For some reason I get the following exception when using Spring Batch in combination

  • 0

For some reason I get the following exception when using Spring Batch in combination with Hibernate 4.

java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
at org.springframework.batch.item.database.HibernateItemReaderHelper.createQuery(HibernateItemReaderHelper.java:152)
at org.springframework.batch.item.database.HibernateItemReaderHelper.getForwardOnlyCursor(HibernateItemReaderHelper.java:122)
at ....

I upgraded to the latest Spring batch 2.1.8.RELEASE and Spring 3.1.1.RELEASE which supposed to work with Hibernate 4. I looked into the source and it seems that the helper class is using the new version of the session factory that is used in Hibernate 4:

package org.springframework.batch.item.database;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
import org.springframework.batch.item.database.orm.HibernateQueryProvider;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * Internal shared state helper for hibernate readers managing sessions and
 * queries.
 * 
 * @author Dave Syer
 * 
 */
public class HibernateItemReaderHelper<T> implements InitializingBean {

    private SessionFactory sessionFactory;

    private String queryString = "";

    private String queryName = "";

    private HibernateQueryProvider queryProvider;

    private boolean useStatelessSession = true;

    private StatelessSession statelessSession;

    private Session statefulSession;

    /**
     * @param queryName name of a hibernate named query
     */
    public void setQueryName(String queryName) {
        this.queryName = queryName;
    }

    /**
     * @param queryString HQL query string
     */
    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }

    /**
     * @param queryProvider Hibernate query provider
     */
    public void setQueryProvider(HibernateQueryProvider queryProvider) {
        this.queryProvider = queryProvider;
    }

    /**
     * Can be set only in uninitialized state.
     * 
     * @param useStatelessSession <code>true</code> to use
     * {@link StatelessSession} <code>false</code> to use standard hibernate
     * {@link Session}
     */
    public void setUseStatelessSession(boolean useStatelessSession) {
        Assert.state(statefulSession == null && statelessSession == null,
                "The useStatelessSession flag can only be set before a session is initialized.");
        this.useStatelessSession = useStatelessSession;
    }

    /**
     * @param sessionFactory hibernate session factory
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void afterPropertiesSet() throws Exception {

        Assert.state(sessionFactory != null, "A SessionFactory must be provided");

        if (queryProvider == null) {
            Assert.notNull(sessionFactory, "session factory must be set");
            Assert.state(StringUtils.hasText(queryString) ^ StringUtils.hasText(queryName),
                    "queryString or queryName must be set");
        }
        // making sure that the appropriate (Hibernate) query provider is set
        else {
            Assert.state(queryProvider != null, "Hibernate query provider must be set");
        }

    }

    /**
     * Get a cursor over all of the results, with the forward-only flag set.
     * 
     * @param fetchSize the fetch size to use retrieving the results
     * @param parameterValues the parameter values to use (or null if none).
     * 
     * @return a forward-only {@link ScrollableResults}
     */
    public ScrollableResults getForwardOnlyCursor(int fetchSize, Map<String, Object> parameterValues) {
        Query query = createQuery();
        if (parameterValues != null) {
            query.setProperties(parameterValues);
        }
        return query.setFetchSize(fetchSize).scroll(ScrollMode.FORWARD_ONLY);
    }

    /**
     * Open appropriate type of hibernate session and create the query.
     */
    public Query createQuery() {

        if (useStatelessSession) {
            if (statelessSession == null) {
                statelessSession = sessionFactory.openStatelessSession();
            }
            if (queryProvider != null) {
                queryProvider.setStatelessSession(statelessSession);
            }
            else {
                if (StringUtils.hasText(queryName)) {
                    return statelessSession.getNamedQuery(queryName);
                }
                else {
                    return statelessSession.createQuery(queryString);
                }
            }
        }
        else {
            if (statefulSession == null) {
                statefulSession = sessionFactory.openSession();
            }
            if (queryProvider != null) {
                queryProvider.setSession(statefulSession);
            }
            else {
                if (StringUtils.hasText(queryName)) {
                    return statefulSession.getNamedQuery(queryName);
                }
                else {
                    return statefulSession.createQuery(queryString);
                }
            }
        }

        // If queryProvider is set use it to create a query
        return queryProvider.createQuery();

    }

    /**
     * Scroll through the results up to the item specified.
     * 
     * @param cursor the results to scroll over
     */
    public void jumpToItem(ScrollableResults cursor, int itemIndex, int flushInterval) {
        for (int i = 0; i < itemIndex; i++) {
            cursor.next();
            if (i % flushInterval == 0 && !useStatelessSession) {
                statefulSession.clear(); // Clears in-memory cache
            }
        }
    }

    /**
     * Close the open session (stateful or otherwise).
     */
    public void close() {
        if (statelessSession != null) {
            statelessSession.close();
            statelessSession = null;
        }
        if (statefulSession != null) {
            statefulSession.close();
            statefulSession = null;
        }
    }

    /**
     * Read a page of data, clearing the existing session (if necessary) first,
     * and creating a new session before executing the query.
     * 
     * @param page the page to read (starting at 0)
     * @param pageSize the size of the page or maximum number of items to read
     * @param fetchSize the fetch size to use
     * @param parameterValues the parameter values to use (if any, otherwise
     * null)
     * @return a collection of items
     */
    public Collection<? extends T> readPage(int page, int pageSize, int fetchSize, Map<String, Object> parameterValues) {

        clear();

        Query query = createQuery();
        if (parameterValues != null) {
            query.setProperties(parameterValues);
        }
        @SuppressWarnings("unchecked")
        List<T> result = query.setFetchSize(fetchSize).setFirstResult(page * pageSize).setMaxResults(pageSize).list();
        return result;

    }

    /**
     * Clear the session if stateful.
     */
    public void clear() {
        if (statefulSession != null) {
            statefulSession.clear();
        }
    }

}

So the question is why is it still trying to use an older version even though the newest is used. Does anybody have an idea why this still could be happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:43:07+00:00Added an answer on June 10, 2026 at 12:43 am

    We finally got it to work by compiling Spring batch against Hibernate 4. It seems Spring batch is not compatible with Hibernate 4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Javascript and for some reason I can't get the following code
I can run the following query in PHPMyAdmin, but for some reason I get
For some reason, I can't get embedded firebird sql to work on Windows using
I'm querying a DB using MS SQL and for some reason I get the
This is kind of embarrassing but I cannot for some reason get his CSS
For some reason I get some warnings about non dll-interface class when building with
I have a search setup inside a div but for some reason cannot get
I use CSS transitions fairly regularly and for some reason I cannot get them
For some reason I can't get this to work. It pulls the name and
For some reason I can't get this script to run when I add the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.