Permalink
Browse files

APPNG-2202 added List<T> search(String queryString, Object... params)

  • Loading branch information...
madness-inc committed Jul 24, 2018
1 parent 8ec55d6 commit 98646248822bd24d2762e15d5cb5e3b0f93e3319
@@ -17,6 +17,7 @@
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
@@ -43,7 +44,8 @@
* @see JpaSpecificationExecutor
*/
@NoRepositoryBean
public interface SearchRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
public interface SearchRepository<T, ID extends Serializable>
extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
/**
* Checks whether the given property is unique for this domain type.
@@ -86,8 +88,8 @@
Page<T> search(Pageable pageable);
/**
* Performs a paginated search based upon the given query-String.<b>Note that the query string must start with
* "from &lt;Entity> &lt;entityName>"!</b>
* Performs a paginated search based upon the given query-String.<b>Note that the query string must start with "from
* &lt;Entity> &lt;entityName>"!</b>
*
* @param queryString
* the JPQL-query, starting with "from &lt;Entity> &lt;entityName>"
@@ -103,14 +105,27 @@
*/
Page<T> search(String queryString, String entityName, Pageable pageable, Object... params);
/**
* Performs a search with the given query-String.
*
* @param queryString
* the JPQL-query
* @param params
* the parameters to be applied to the {@link Query}, using {@link Query#setParameter(int, Object)}.
* @return a {@link Page} containing the result
*
*/
List<T> search(String queryString, Object... params);
/**
* Performs a paginated search with the given {@link SearchQuery}.
*
* @param searchQuery
* the {@link SearchQuery}
* @param pageable
* a {@link Pageable}
* @return a {@link Page} containing the result
* a {@link Pageable} (optional)
* @return a {@link Page} containing the result. When {@code pageable} is null, a single page containing all results
* will be returned
*/
Page<T> search(SearchQuery<T> searchQuery, Pageable pageable);
@@ -129,6 +129,16 @@ private void appendOrder(Pageable pageable, StringBuilder queryBuilder, String e
List<T> content = query.getResultList();
return new PageImpl<T>(content, pageable, total);
}
public List<T> search(String queryString, Object... args) {
TypedQuery<T> query = entityManager.createQuery(queryString, domainClass);
if (null != args) {
for (int i = 1; i <= args.length; i++) {
query.setParameter(i, args[i - 1]);
}
}
return query.getResultList();
}
public Page<T> search(SearchQuery<T> searchQuery, Pageable pageable) {
return searchQuery.execute(pageable, entityManager);
@@ -18,6 +18,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
@@ -99,6 +100,13 @@ public Predicate toPredicate(Root<TestEntity> root, CriteriaQuery<?> query, Crit
}), pageable);
validate(page);
}
@Test
public void testQueryStringUnpaged() {
List<TestEntity> items = repo.search("from TestEntity e where e.name like ?1", "%name%");
Assert.assertEquals(3, items.size());
}
@Test
public void testQueryStringPageableParams() {

0 comments on commit 9864624

Please sign in to comment.