Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2008 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
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * 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 JPA.

It executes the JPQL setQueryString(java.lang.String) to retrieve requested data. The query is executed using paged requests of a size specified in AbstractPagingItemReader.setPageSize(int). Additional pages are requested when needed as org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read() method is called, returning an object corresponding to current position.

The performance of the paging depends on the JPA implementation and its use of database specific features to limit the number of returned rows.

Setting a fairly large page size and using a commit interval that matches the page size should provide better performance.

In order to reduce the memory usage for large results the persistence context is flushed and cleared after each page is read. This causes any entities read to be detached. If you make changes to the entities and want the changes persisted then you must explicitly merge the entities.

The reader must be configured with an javax.persistence.EntityManagerFactory. All entity access is performed within a new transaction, independent of any existing Spring managed transactions.

The implementation is thread-safe in between calls to org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext), but remember to use saveState=false if used in a multi-threaded client (no restart available).

Author(s):
Thomas Risberg
Dave Syer
Since:
2.0
 
 public class JpaPagingItemReader<T> extends AbstractPagingItemReader<T> {
 
 
 
 	private final Map<StringObjectjpaPropertyMap = new HashMap<StringObject>();
 
 	private String queryString;
 
 
 
 	public JpaPagingItemReader() {
	}

Create a query using an appropriate query provider (entityManager OR queryProvider).
	private Query createQuery() {
		if ( == null) {
		}
		else {
		}
	}
	public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
		this. = entityManagerFactory;
	}

The parameter values to be used for the query execution.

Parameters:
parameterValues the values keyed by the parameter named used in the query string.
	public void setParameterValues(Map<StringObjectparameterValues) {
		this. = parameterValues;
	}
	public void afterPropertiesSet() throws Exception {
		if ( == null) {
		}
		// making sure that the appropriate (JPA) query provider is set
		else {
			Assert.isTrue( != null"JPA query provider must be set");
		}
	}

Parameters:
queryString JPQL query string
	public void setQueryString(String queryString) {
		this. = queryString;
	}

Parameters:
queryProvider JPA query provider
	public void setQueryProvider(JpaQueryProvider queryProvider) {
		this. = queryProvider;
	}
	protected void doOpen() throws Exception {
		super.doOpen();
		if ( == null) {
			throw new DataAccessResourceFailureException("Unable to obtain an EntityManager");
		}
		// set entityManager to queryProvider, so it participates
		// in JpaPagingItemReader's managed transaction
		if ( != null) {
		}
	}
	@SuppressWarnings("unchecked")
	protected void doReadPage() {
		tx.begin();
		if ( != null) {
				query.setParameter(me.getKey(), me.getValue());
			}
		}
		if ( == null) {
		}
		else {
		}
		tx.commit();
	}
	protected void doJumpToPage(int itemIndex) {
	}
	protected void doClose() throws Exception {
		super.doClose();
	}
New to GrepCode? Check out our FAQ X