diff options
author | Sebastian Schmelzer | 2012-08-14 16:42:52 +0200 |
---|---|---|
committer | Sebastian Schmelzer | 2012-08-14 16:42:52 +0200 |
commit | 9794dd2ef3d43632014caf1b3817316e7d3fc6cb (patch) | |
tree | f5c3618190c93fe221fdae6a711a3523cdbe59c1 /src | |
parent | Added abstract entity classes (diff) | |
download | xss-jpa-9794dd2ef3d43632014caf1b3817316e7d3fc6cb.tar.gz xss-jpa-9794dd2ef3d43632014caf1b3817316e7d3fc6cb.tar.xz xss-jpa-9794dd2ef3d43632014caf1b3817316e7d3fc6cb.zip |
minor jpa tweaks
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java | 40 | ||||
-rw-r--r-- | src/main/java/nu/xss/jpa/dao/Dao.java | 2 | ||||
-rw-r--r-- | src/main/java/nu/xss/jpa/dao/GenericJpaDao.java | 35 |
3 files changed, 33 insertions, 44 deletions
diff --git a/src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java b/src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java deleted file mode 100644 index b1638ac..0000000 --- a/src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java +++ /dev/null @@ -1,40 +0,0 @@ -package nu.xss.jpa.dao; - -import java.io.Serializable; -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -public abstract class AbstractJpaDao<E, K> implements Dao<E, K>, Serializable { - - private static final long serialVersionUID = 4998055731089977476L; - - @PersistenceContext - protected EntityManager entityManager; - - protected Class<E> entity; - - public AbstractJpaDao(final Class<E> entity) { - this.entity = (Class<E>) entity; - } - - public void persist(E entity) { - entityManager.persist(entity); - - } - - public void remove(E entity) { - entityManager.remove(entity); - } - - public E findById(K id) { - return entityManager.find(entity, id); - } - - public List<E> findAll() { - // TODO Auto-generated method stub - return null; - } - -} diff --git a/src/main/java/nu/xss/jpa/dao/Dao.java b/src/main/java/nu/xss/jpa/dao/Dao.java index 2c541c9..345804c 100644 --- a/src/main/java/nu/xss/jpa/dao/Dao.java +++ b/src/main/java/nu/xss/jpa/dao/Dao.java @@ -8,4 +8,6 @@ public interface Dao<E, K> { void delete(E entity); E findById(K id); List<E> findAll(); + List<E> findAll(int count); + List<E> findAll(int count, int offset); } diff --git a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java index 21ee630..1eb1f59 100644 --- a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java +++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java @@ -2,12 +2,14 @@ package nu.xss.jpa.dao; import java.io.Serializable; 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; import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; @@ -32,25 +34,29 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements public GenericJpaDao() { ParameterizedType genericSuperclass = (ParameterizedType) getClass() .getGenericSuperclass(); - this.entity = (Class<E>) genericSuperclass.getActualTypeArguments()[0]; + Type type = genericSuperclass.getActualTypeArguments()[0]; + if (type instanceof Class) { + this.entity = (Class<E>) type; + } else if (type instanceof ParameterizedType) { + this.entity = (Class<E>) ((ParameterizedType)type).getRawType(); + } } public void save(E entity) { logger.info("Persist Entity: {}.", entity.toString()); entityManager.persist(entity); - entityManager.flush(); logger.info("Saved entity: {}.", entity.toString()); } public void merge(E entity) { logger.info("Merge Entity: {}.", entity.toString()); entityManager.merge(entity); - entityManager.flush(); logger.info("Saved entity: {}.", entity.toString()); } public void delete(E entity) { - entityManager.remove(entity); + E persistentEntity = findById(entity.getId()); + entityManager.remove(persistentEntity); logger.info("Deleted entity: {}.", entity.toString()); } @@ -61,6 +67,14 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements public List<E> findAll() { return find(createSimpleQuery()); } + + public List<E> findAll(int count) { + return find(createSimpleQuery(), count); + } + + public List<E> findAll(int count, int offset) { + return find(createSimpleQuery(), count, offset); + } protected CriteriaBuilder getCriteriaBuilder() { return entityManager.getCriteriaBuilder(); @@ -82,6 +96,19 @@ public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements return entityManager.createQuery(query).getResultList(); } + protected List<E> find(CriteriaQuery<E> query, int count) { + TypedQuery<E> q = entityManager.createQuery(query); + q.setMaxResults(count); + return q.getResultList(); + } + + 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(); + } + protected E findSingle(CriteriaQuery<E> query) { return entityManager.createQuery(query).getSingleResult(); } |