summaryrefslogtreecommitdiffstats
path: root/src/main/java/nu/xss
diff options
context:
space:
mode:
authorSebastian Schmelzer2012-08-21 16:56:20 +0200
committerSebastian Schmelzer2012-08-21 16:56:20 +0200
commit6e1d6f9c198b70efa50da09ca3719a3d350de2cd (patch)
tree94b340e0ab4b1bd72dc1fa1977e7bbaf2043867e /src/main/java/nu/xss
parentminor jpa tweaks (diff)
downloadxss-jpa-6e1d6f9c198b70efa50da09ca3719a3d350de2cd.tar.gz
xss-jpa-6e1d6f9c198b70efa50da09ca3719a3d350de2cd.tar.xz
xss-jpa-6e1d6f9c198b70efa50da09ca3719a3d350de2cd.zip
add sort/order parameters to find(), findAll()
Diffstat (limited to 'src/main/java/nu/xss')
-rw-r--r--src/main/java/nu/xss/jpa/dao/Dao.java8
-rw-r--r--src/main/java/nu/xss/jpa/dao/GenericJpaDao.java73
2 files changed, 81 insertions, 0 deletions
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<E, K> {
List<E> findAll();
List<E> findAll(int count);
List<E> findAll(int count, int offset);
+
+ List<E> findAll(String sortColumn);
+ List<E> findAll(int count, String sortColumn);
+ List<E> findAll(int count, int offset, String sortColumn);
+
+ List<E> findAll(String sortColumn, boolean isAscending);
+ List<E> findAll(int count, String sortColumn, boolean isAscending);
+ List<E> 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<E extends TypedEntity<K>, K> implements
public List<E> findAll(int count, int offset) {
return find(createSimpleQuery(), count, offset);
}
+
+ public List<E> findAll(String sortColumn) {
+ return find(createSimpleQuery(), sortColumn);
+ }
+
+ public List<E> findAll(int count, String sortColumn) {
+ return find(createSimpleQuery(), count, sortColumn);
+ }
+
+ public List<E> findAll(int count, int offset, String sortColumn) {
+ return find(createSimpleQuery(), count, offset, sortColumn);
+ }
+
+ public List<E> findAll(String sortColumn, boolean isAscending) {
+ return find(createSimpleQuery(), sortColumn, isAscending);
+ }
+
+ public List<E> findAll(int count, String sortColumn, boolean isAscending) {
+ return find(createSimpleQuery(), count, sortColumn, isAscending);
+ }
+
+ public List<E> 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<E extends TypedEntity<K>, K> implements
return c;
}
+
protected List<E> find(CriteriaQuery<E> query) {
return entityManager.createQuery(query).getResultList();
}
@@ -109,6 +134,54 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
return q.getResultList();
}
+ protected List<E> find(CriteriaQuery<E> query, String sortColumn) {
+ return find(query, sortColumn, false);
+ }
+
+ protected List<E> find(CriteriaQuery<E> query, int count, String sortColumn) {
+ return find(query, count, sortColumn, false);
+ }
+
+ protected List<E> find(CriteriaQuery<E> query, int count, int offset, String sortColumn) {
+ return find(query, count, offset, sortColumn, false);
+ }
+
+
+ protected List<E> find(CriteriaQuery<E> query, String sortColumn, boolean isAscending) {
+ Root<E> 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<E> find(CriteriaQuery<E> query, int count, String sortColumn, boolean isAscending) {
+ Root<E> root = query.from(this.entity);
+ if (isAscending) {
+ query.orderBy(getCriteriaBuilder().asc(root.get(sortColumn)));
+ } else {
+ query.orderBy(getCriteriaBuilder().desc(root.get(sortColumn)));
+ }
+ TypedQuery<E> q = entityManager.createQuery(query);
+ q.setMaxResults(count);
+ return q.getResultList();
+ }
+
+ protected List<E> find(CriteriaQuery<E> query, int count, int offset, String sortColumn, boolean isAscending) {
+ Root<E> root = query.from(this.entity);
+ if (isAscending) {
+ query.orderBy(getCriteriaBuilder().asc(root.get(sortColumn)));
+ } else {
+ query.orderBy(getCriteriaBuilder().desc(root.get(sortColumn)));
+ }
+ TypedQuery<E> q = entityManager.createQuery(query);
+ q.setFirstResult(offset);
+ q.setMaxResults(count);
+ return q.getResultList();
+ }
+
protected E findSingle(CriteriaQuery<E> query) {
return entityManager.createQuery(query).getSingleResult();
}