summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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();
}