Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2006-2007 the original author or authors.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.springframework.batch.item.database;
 import java.util.Map;
org.springframework.batch.item.ItemReader for reading database records built on top of Hibernate. It executes the HQL query when initialized iterates over the result set as method is called, returning an object corresponding to current row. The query can be set directly using setQueryString(java.lang.String), a named query can be used by setQueryName(java.lang.String), or a query provider strategy can be supplied via setQueryProvider(org.springframework.batch.item.database.orm.HibernateQueryProvider).

The reader can be configured to use either org.hibernate.StatelessSession sufficient for simple mappings without the need to cascade to associated objects or standard hibernate org.hibernate.Session for more advanced mappings or when caching is desired. When stateful session is used it will be cleared in the update(org.springframework.batch.item.ExecutionContext) method without being flushed (no data modifications are expected).

The implementation is not thread-safe.

Robert Kasanicky
Dave Syer
 	private boolean initialized = false;
 	private int fetchSize;
 	public void afterPropertiesSet() throws Exception {
 		Assert.state( >= 0, "fetchSize must not be negative");

The parameter values to apply to a query (map of name:value).

parameterValues the parameter values to set
 	public void setParameterValues(Map<StringObjectparameterValues) {
 		this. = parameterValues;

A query name for an externalized query. Either this or the { query string or the { query provider should be set.

queryName name of a hibernate named query
 	public void setQueryName(String queryName) {

Fetch size used internally by Hibernate to limit amount of data fetched from database per round trip.

fetchSize the fetch size to pass down to Hibernate
	public void setFetchSize(int fetchSize) {
		this. = fetchSize;

A query provider. Either this or the {{@link setQueryString(java.lang.String) query string} or the {{@link setQueryName(java.lang.String) query name} should be set.

queryProvider Hibernate query provider
	public void setQueryProvider(HibernateQueryProvider queryProvider) {

A query string in HQL. Either this or the { query provider or the { query name should be set.

queryString HQL query string
	public void setQueryString(String queryString) {

The Hibernate SessionFactory to use the create a session.

sessionFactory the org.hibernate.SessionFactory to set
	public void setSessionFactory(SessionFactory sessionFactory) {

Can be set only in uninitialized state.

useStatelessSession true to use org.hibernate.StatelessSession false to use standard hibernate org.hibernate.Session
	public void setUseStatelessSession(boolean useStatelessSession) {
	protected T doRead() throws Exception {
		if (.next()) {
			Object[] data = .get();
			if (data.length > 1) {
				// If there are multiple items this must be a projection
				// and T is an array type.
item = (T) data;
				return item;
			else {
				// Assume if there is only one item that it is the data the user
				// wants.
				// If there is only one item this is going to be a nasty shock
				// if T is an array type but there's not much else we can do...
item = (T) data[0];
				return item;
		return null;

Open hibernate session and create a forward-only cursor for the query.
	protected void doOpen() throws Exception {
		Assert.state(!"Cannot open an already opened ItemReader, call close first");
		 = true;

Update the context and clear the session if stateful.

executionContext the current org.springframework.batch.item.ExecutionContext
org.springframework.batch.item.ItemStreamException if there is a problem
	public void update(ExecutionContext executionContextthrows ItemStreamException {

Wind forward through the result set to the item requested. Also clears the session every now and then (if stateful) to avoid memory problems. The frequency of session clearing is the larger of the fetch size (if set) and 100.

itemIndex the first item to read
java.lang.Exception if there is a problem
See also:
	protected void jumpToItem(int itemIndexthrows Exception {
		int flushSize = Math.max(, 100);

Close the cursor and hibernate session.
	protected void doClose() throws Exception {
		 = false;
		if ( != null) {
New to GrepCode? Check out our FAQ X