调用openSession的SessionFactory抛出noSuchMethoderror异常

时间:2015-05-29 11:37:06

标签: java spring hibernate session spring-batch

我正在使用 hibernate 3.2.5.GA ,在调用openSession的{​​{1}}时,它会返回SessionFactory类型的Session对象:

org.hibernate.classic.Session

我也在使用 Spring Batch 2.2.7.RELEASE ,在public org.hibernate.classic.Session openSession() throws HibernateException; 中设置SessionFacotry时,调用HibernateItemReaderHelper时会抛出异常,因为它期望openSession类型:Session

org.hibernate.Session

任何人都知道解决方案吗?

P.S。我无法升级 Hibernate

2 个答案:

答案 0 :(得分:1)

奇怪的是他们在SessionFactory中使用org.hibernate.classic.Session实现,即使在版本3到版本4之后,看看两者是否可用(org.hibernate.classic.Session和org.hibernate.Session)之后你可能知道spring classic用于与hibernate 2.1的兼容性,它从hibernate 3开始就被弃用了。

建议

不应该使用 HibernateItemReaderHelper ,因为它是一个Spring内部类。它是在Spring Batch 2.2.7中引入的。

解决方案

我查看了所有可用item readers的代码,但他们使用了新的org.hibernate.Session。 在这个版本中没有办法绕过它。

  • 如果您无法更改休眠版本,请将Spring Batch版本更改为较早版本,然后选择其中一个项目阅读器。
  • 如果您也不能这样做,请构建自定义项目阅读器。

这里'一个link来浏览spring批处理源代码并检查当你开始在org.springframework.batch包下的各种Spring Batch版本中查找项目阅读器时正在使用哪个Session版本.item。

答案 1 :(得分:1)

很简单。您可以通过简单地从Spring-Batch 2.2.7更改原始HibernateItemReaderHelper的会话类型来编写自己的HibernateItemReaderHelper。RELEASE.below是HibernateItemReaderHelper,它将与您的Hibernate版本一起使用。原始文件是从Spring-Batch 2.2.7 RELEASE中提取出来并自定义为与hibernate 3.2.5.GA一起使用

package com.baji.batch.readers;

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.SessionFactory;
import org.hibernate.StatelessSession;
import org.hibernate.classic.Session;
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;
    }

    @Override
    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();
        }
    }

}