-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Comments
Mahmoud Ben Hassine commented It is possible to use a namedQuery by providing a custom 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 @David Zeigler Do you agree? |
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 /*
* 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). |
Hi @benas, would you like to assign this to me? Thank you. |
Resolved with #3726 . |
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
The text was updated successfully, but these errors were encountered: