出于某种原因,将Spring Batch与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 ....
我升级到最新的Spring批处理2.1.8.RELEASE和Spring 3.1.1.RELEASE,它应该与Hibernate 4一起工作。我查看了源代码,似乎帮助程序类正在使用新版本的会话在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();
}
}
}
所以问题是为什么它仍然试图使用旧版本,即使使用最新版本。有没有人知道为什么还会发生这种情况?
答案 0 :(得分:1)
我们终于通过针对Hibernate 4编译Spring批处理来实现它。看起来Spring批处理与Hibernate 4不兼容。
答案 1 :(得分:0)
这就是我的WAR:
activation-1.1.jar
amqp-client-2.7.1.jar
antlr-2.7.7.jar
aopalliance-1.0.jar
aspectjrt-1.5.0.jar
aspectjweaver-1.5.2.jar
aspectjweaver-1.6.9.jar
avalon-framework-4.1.3.jar
billing-commons-1.9.0-SNAPSHOT.jar
billing-core-1.9.0-SNAPSHOT.jar
cglib-nodep-2.2.2.jar
classpath.txt
commons-batch-2.0.0-SNAPSHOT.jar
commons-beanutils-1.7.0.jar
commons-cli-1.1.jar
commons-codec-1.2.jar
commons-customer-experience-1.3.0.jar
commons-domain-1.0.0.jar
commons-email-1.1.jar
commons-email-1.9.1-SNAPSHOT.jar
commons-hibernate-3.0.0-SNAPSHOT.jar
commons-http-1.2.1.jar
commons-httpclient-3.1.jar
commons-httpclient-contrib-3.1.jar
commons-io-1.3.1.jar
commons-lang-2.1.jar
commons-logging-1.1.jar
commons-logging-1.2.0.jar
commons-monitoring-1.0.0.jar
commons-property-3.3.0.jar
commons-rabbitmq-1.1.1.jar
commons-spring-agent-2.5.0-SNAPSHOT.jar
commons-util-1.6.1.jar
customer-inventory-commons-1.4.0.jar
customer-inventory-core-1.4.0.jar
cxf-api-2.3.2.jar
cxf-common-schemas-2.3.2.jar
cxf-common-utilities-2.3.2.jar
cxf-rt-bindings-xml-2.3.2.jar
cxf-rt-core-2.3.2.jar
cxf-rt-frontend-jaxrs-2.3.2.jar
cxf-rt-transports-common-2.3.2.jar
cxf-rt-transports-http-2.3.2.jar
dom4j-1.6.1.jar
generic-monitoring-console-api-1.1.0.jar
geronimo-javamail_1.4_spec-1.7.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.4.Final.jar
hibernate-entitymanager-4.1.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-validator-4.3.0.Final.jar
javassist-3.15.0-GA.jar
jaxb-impl-2.1.13.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
jettison-1.1.jar
jms-1.1.jar
jsr250-api-1.0.jar
jsr311-api-1.1.1.jar
log4j-1.2.15.jar
logkit-1.0.1.jar
mail-1.4.jar
neethi-2.0.4.jar
orchestration-api-1.7.0-20120820.120350-6.jar
quartz-1.5.2.jar
simplestuff-0.9.jar
singleview-api-commons-1.1.0.jar
singleview-api-core-1.1.0.jar
slf4j-api-1.5.6.jar
slf4j-simple-1.5.6.jar
spring-aop-3.1.1.RELEASE.jar
spring-asm-3.1.1.RELEASE.jar
spring-aspects-3.1.1.RELEASE.jar
spring-batch-core-2.1.8.RELEASE.jar
spring-batch-infrastructure-2.1.8.RELEASE.jar
spring-beans-3.1.1.RELEASE.jar
spring-context-3.1.1.RELEASE.jar
spring-context-support-3.1.1.RELEASE.jar
spring-core-3.1.1.RELEASE.jar
spring-expression-3.1.1.RELEASE.jar
spring-jdbc-3.1.1.RELEASE.jar
spring-orm-3.1.1.RELEASE.jar
spring-tx-3.1.1.RELEASE.jar
spring-web-3.1.1.RELEASE.jar
spring-webmvc-3.1.1.RELEASE.jar
stax2-api-3.0.2.jar
validation-api-1.0.0.GA.jar
woodstox-core-asl-4.0.8.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.7.jar
xpp3_min-1.1.4c.jar
xstream-1.3.1.jar