001/* 002 * Copyright 2006-2008 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.batch.item.database.orm; 018 019import javax.persistence.Query; 020 021import org.springframework.util.Assert; 022import org.springframework.util.StringUtils; 023 024/** 025 * <p> 026 * This query provider creates JPA {@link Query}s from injected native SQL 027 * queries. This is useful if there is a need to utilize database-specific 028 * features such as query hints, the CONNECT keyword in Oracle, etc. 029 * </p> 030 * 031 * @author Anatoly Polinsky 032 * 033 * @param <E> entity returned by executing the query 034 */ 035public class JpaNativeQueryProvider<E> extends AbstractJpaQueryProvider { 036 037 private Class<E> entityClass; 038 039 private String sqlQuery; 040 041 @Override 042 public Query createQuery() { 043 return getEntityManager().createNativeQuery(sqlQuery, entityClass); 044 } 045 046 public void setSqlQuery(String sqlQuery) { 047 this.sqlQuery = sqlQuery; 048 } 049 050 public void setEntityClass(Class<E> entityClazz) { 051 this.entityClass = entityClazz; 052 } 053 054 @Override 055 public void afterPropertiesSet() throws Exception { 056 Assert.isTrue(StringUtils.hasText(sqlQuery), "Native SQL query cannot be empty"); 057 Assert.notNull(entityClass, "Entity class cannot be NULL"); 058 } 059}