Skip to content

Add support for named queries in JpaPagingItemReader #1667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Dec 12, 2012 · 5 comments
Closed

Add support for named queries in JpaPagingItemReader #1667

spring-projects-issues opened this issue Dec 12, 2012 · 5 comments
Assignees
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

David Zeigler opened BATCH-1926 and commented

The Hibernate ItemReaders support queryName as an alternative to queryString allowing the reader to use a named query.

JpaPagingItemReader needs to support this option as well. When queryName is set, the javax.persistence.Query should be created using entityManager.createNamedQuery(queryName);


Affects: 2.1.9

@spring-projects-issues
Copy link
Collaborator Author

Mahmoud Ben Hassine commented

It is possible to use a namedQuery by providing a custom JpaQueryProvider. Similar to the JpaNativeQueryProvider for native queries, you can create a JpaNamedQueryProvider for named queries. Or using an inner class for example:

JpaPagingItemReader<Foo> reader = new JpaPagingItemReaderBuilder<Foo>()
		.name("fooReader")
		.queryProvider(new AbstractJpaQueryProvider() {
			@Override
			public Query createQuery() {
				return getEntityManager().createNamedQuery("your named query");
			}

			@Override
			public void afterPropertiesSet() throws Exception {

			}
		})
		// set other properties on the reader
		.build();

So support for named queries on JpaPagingItemReader is already provided through the JpaQueryProvider API.

@David Zeigler Do you agree?

@spring-projects-issues
Copy link
Collaborator Author

Mahmoud Ben Hassine commented

After re-reading this issue, I believe Spring Batch can provide the class to free the user from doing it as in my previous comment. The new JpaNamedQueryProvider could look like this (inspired by JpaNativeQueryProvider):

/*
 * Copyright 2019 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
 *
 *      https://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.orm;

import javax.persistence.Query;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * <p>
 * This query provider creates JPA named {@link Query}s.
 * </p>
 * 
 * @author Mahmoud Ben Hassine
 * @since 4.3
 * 
 * @param <E> entity returned by executing the query
 */
public class JpaNamedQueryProvider<E> extends AbstractJpaQueryProvider {

	private Class<E> entityClass;

	private String namedQuery;

    @Override
	public Query createQuery() {
		return getEntityManager().createNamedQuery(namedQuery, entityClass);
	}

	public void setNamedQuery(String namedQuery) {
		this.namedQuery = namedQuery;
	}

	public void setEntityClass(Class<E> entityClazz) {
		this.entityClass = entityClazz;
	}

    @Override
	public void afterPropertiesSet() throws Exception {
		Assert.isTrue(StringUtils.hasText(namedQuery), "Named query cannot be empty");
		Assert.notNull(entityClass, "Entity class cannot be NULL");
	}
}

I will open a PR with corresponding test (contributions are welcome).

@parikshitdutta
Copy link
Contributor

parikshitdutta commented Jun 4, 2020

Hi @benas, would you like to assign this to me?

Thank you.

@parikshitdutta
Copy link
Contributor

Hi @benas, as you find time, please review the PR #3726. I will look forward to your feedback.

Thank you.

fmbenhassine pushed a commit to fmbenhassine/spring-batch that referenced this issue Jun 26, 2020
@fmbenhassine fmbenhassine linked a pull request Jun 26, 2020 that will close this issue
@fmbenhassine
Copy link
Contributor

Resolved with #3726 .

@fmbenhassine fmbenhassine changed the title Add support for named queries on JpaPagingItemReader [BATCH-1926] Add support for named queries in JpaPagingItemReader Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants