summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastian Schmelzer2012-08-22 15:50:13 +0200
committerSebastian Schmelzer2012-08-22 15:50:13 +0200
commitd331e5172797a9ce2f9992e3f3ad92970eaa28ba (patch)
treefbd4f0eecab9dc453f6fefa939277e134a9fb48a /src
parentadd sort/order parameters to find(), findAll() (diff)
downloadxss-jpa-d331e5172797a9ce2f9992e3f3ad92970eaa28ba.tar.gz
xss-jpa-d331e5172797a9ce2f9992e3f3ad92970eaa28ba.tar.xz
xss-jpa-d331e5172797a9ce2f9992e3f3ad92970eaa28ba.zip
fix sorting bug
Diffstat (limited to 'src')
-rw-r--r--src/main/java/nu/xss/jpa/dao/GenericJpaDao.java94
1 files changed, 44 insertions, 50 deletions
diff --git a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
index 1649f8f..399763a 100644
--- a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
+++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
@@ -5,6 +5,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
+
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
@@ -35,6 +36,8 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
ParameterizedType genericSuperclass = (ParameterizedType) getClass()
.getGenericSuperclass();
Type type = genericSuperclass.getActualTypeArguments()[0];
+
+
if (type instanceof Class) {
this.entity = (Class<E>) type;
} else if (type instanceof ParameterizedType) {
@@ -65,39 +68,39 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
}
public List<E> findAll() {
- return find(createSimpleQuery());
+ return find(null);
}
public List<E> findAll(int count) {
- return find(createSimpleQuery(), count);
+ return find(null, count);
}
public List<E> findAll(int count, int offset) {
- return find(createSimpleQuery(), count, offset);
+ return find(null, count, offset);
}
public List<E> findAll(String sortColumn) {
- return find(createSimpleQuery(), sortColumn);
+ return find(null, sortColumn);
}
public List<E> findAll(int count, String sortColumn) {
- return find(createSimpleQuery(), count, sortColumn);
+ return find(null, count, sortColumn);
}
public List<E> findAll(int count, int offset, String sortColumn) {
- return find(createSimpleQuery(), count, offset, sortColumn);
+ return find(null, count, offset, sortColumn);
}
public List<E> findAll(String sortColumn, boolean isAscending) {
- return find(createSimpleQuery(), sortColumn, isAscending);
+ return find(null, sortColumn, isAscending);
}
public List<E> findAll(int count, String sortColumn, boolean isAscending) {
- return find(createSimpleQuery(), count, sortColumn, isAscending);
+ return find(null, count, sortColumn, isAscending);
}
public List<E> findAll(int count, int offset, String sortColumn, boolean isAscending) {
- return find(createSimpleQuery(), count, offset, sortColumn, isAscending);
+ return find(null, count, offset, sortColumn, isAscending);
}
protected CriteriaBuilder getCriteriaBuilder() {
@@ -109,76 +112,67 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
return c;
}
- protected CriteriaQuery<E> createSimpleQuery() {
- CriteriaQuery<E> c = getCriteriaBuilder().createQuery(this.entity);
- Root<E> root = c.from(this.entity);
- c.select(root);
- return c;
- }
-
protected List<E> find(CriteriaQuery<E> query) {
- return entityManager.createQuery(query).getResultList();
+ return find(query, 0, 0, null, true);
}
protected List<E> find(CriteriaQuery<E> query, int count) {
- TypedQuery<E> q = entityManager.createQuery(query);
- q.setMaxResults(count);
- return q.getResultList();
+ return find(query, count, 0, null, true);
}
protected List<E> find(CriteriaQuery<E> query, int count, int offset) {
- TypedQuery<E> q = entityManager.createQuery(query);
- q.setFirstResult(offset);
- q.setMaxResults(count);
- return q.getResultList();
+ return find(query, count, offset, null, true);
}
protected List<E> find(CriteriaQuery<E> query, String sortColumn) {
- return find(query, sortColumn, false);
+ return find(query, sortColumn, true);
}
protected List<E> find(CriteriaQuery<E> query, int count, String sortColumn) {
- return find(query, count, sortColumn, false);
+ return find(query, count, sortColumn, true);
}
protected List<E> find(CriteriaQuery<E> query, int count, int offset, String sortColumn) {
- return find(query, count, offset, sortColumn, false);
+ return find(query, count, offset, sortColumn, true);
}
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();
+ return find(query, 0, 0, sortColumn, isAscending);
}
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();
+ return find(query, count, 0, sortColumn, isAscending);
}
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)));
+ Root<E> root;
+
+ // check if we have a 'simple' query
+ if (query == null) {
+ query = createQuery();
}
+
+ root = query.from(this.entity);
+ query.select(root);
+
+ if (sortColumn != null) {
+ 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);
+ if ( count > 0 ) {
+ q.setFirstResult(offset);
+ }
+ if (offset > 0) {
+ q.setMaxResults(count);
+ }
+
return q.getResultList();
}