From 6e1d6f9c198b70efa50da09ca3719a3d350de2cd Mon Sep 17 00:00:00 2001 From: Sebastian Schmelzer Date: Tue, 21 Aug 2012 16:56:20 +0200 Subject: add sort/order parameters to find(), findAll() --- src/main/java/nu/xss/jpa/dao/Dao.java | 8 +++ src/main/java/nu/xss/jpa/dao/GenericJpaDao.java | 73 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/main/java/nu/xss/jpa/dao/Dao.java b/src/main/java/nu/xss/jpa/dao/Dao.java index 345804c..317e291 100644 --- a/src/main/java/nu/xss/jpa/dao/Dao.java +++ b/src/main/java/nu/xss/jpa/dao/Dao.java @@ -10,4 +10,12 @@ public interface Dao { List findAll(); List findAll(int count); List findAll(int count, int offset); + + List findAll(String sortColumn); + List findAll(int count, String sortColumn); + List findAll(int count, int offset, String sortColumn); + + List findAll(String sortColumn, boolean isAscending); + List findAll(int count, String sortColumn, boolean isAscending); + List findAll(int count, int offset, String sortColumn, boolean isAscending); } diff --git a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java index 1eb1f59..1649f8f 100644 --- a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java +++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java @@ -75,6 +75,30 @@ public abstract class GenericJpaDao, K> implements public List findAll(int count, int offset) { return find(createSimpleQuery(), count, offset); } + + public List findAll(String sortColumn) { + return find(createSimpleQuery(), sortColumn); + } + + public List findAll(int count, String sortColumn) { + return find(createSimpleQuery(), count, sortColumn); + } + + public List findAll(int count, int offset, String sortColumn) { + return find(createSimpleQuery(), count, offset, sortColumn); + } + + public List findAll(String sortColumn, boolean isAscending) { + return find(createSimpleQuery(), sortColumn, isAscending); + } + + public List findAll(int count, String sortColumn, boolean isAscending) { + return find(createSimpleQuery(), count, sortColumn, isAscending); + } + + public List findAll(int count, int offset, String sortColumn, boolean isAscending) { + return find(createSimpleQuery(), count, offset, sortColumn, isAscending); + } protected CriteriaBuilder getCriteriaBuilder() { return entityManager.getCriteriaBuilder(); @@ -92,6 +116,7 @@ public abstract class GenericJpaDao, K> implements return c; } + protected List find(CriteriaQuery query) { return entityManager.createQuery(query).getResultList(); } @@ -109,6 +134,54 @@ public abstract class GenericJpaDao, K> implements return q.getResultList(); } + protected List find(CriteriaQuery query, String sortColumn) { + return find(query, sortColumn, false); + } + + protected List find(CriteriaQuery query, int count, String sortColumn) { + return find(query, count, sortColumn, false); + } + + protected List find(CriteriaQuery query, int count, int offset, String sortColumn) { + return find(query, count, offset, sortColumn, false); + } + + + protected List find(CriteriaQuery query, String sortColumn, boolean isAscending) { + Root root = query.from(this.entity); + if (isAscending) { + query.orderBy(getCriteriaBuilder().asc(root.get(sortColumn))); + } else { + query.orderBy(getCriteriaBuilder().desc(root.get(sortColumn))); + } + return entityManager.createQuery(query).getResultList(); + } + + protected List find(CriteriaQuery query, int count, String sortColumn, boolean isAscending) { + Root root = query.from(this.entity); + if (isAscending) { + query.orderBy(getCriteriaBuilder().asc(root.get(sortColumn))); + } else { + query.orderBy(getCriteriaBuilder().desc(root.get(sortColumn))); + } + TypedQuery q = entityManager.createQuery(query); + q.setMaxResults(count); + return q.getResultList(); + } + + protected List find(CriteriaQuery query, int count, int offset, String sortColumn, boolean isAscending) { + Root root = query.from(this.entity); + if (isAscending) { + query.orderBy(getCriteriaBuilder().asc(root.get(sortColumn))); + } else { + query.orderBy(getCriteriaBuilder().desc(root.get(sortColumn))); + } + TypedQuery q = entityManager.createQuery(query); + q.setFirstResult(offset); + q.setMaxResults(count); + return q.getResultList(); + } + protected E findSingle(CriteriaQuery query) { return entityManager.createQuery(query).getSingleResult(); } -- cgit v1.2.3-55-g7522