From 2a620bf30ada3e5b09bb370d1a9fed02a78c53ea Mon Sep 17 00:00:00 2001 From: Sebastian Schmelzer Date: Mon, 30 Jul 2012 19:20:05 +0200 Subject: Added abstract entity classes --- src/main/java/nu/xss/jpa/dao/GenericJpaDao.java | 54 ++++++++++++++++++++-- .../nu/xss/jpa/entity/AbstractAutoIdEntity.java | 28 +++++++++++ .../java/nu/xss/jpa/entity/AbstractEntity.java | 54 ++++++++++++++++++++++ .../java/nu/xss/jpa/entity/AbstractIdEntity.java | 25 ++++++++++ src/main/java/nu/xss/jpa/entity/Entity.java | 5 -- src/main/java/nu/xss/jpa/entity/TypedEntity.java | 11 +++++ 6 files changed, 168 insertions(+), 9 deletions(-) create mode 100644 src/main/java/nu/xss/jpa/entity/AbstractAutoIdEntity.java create mode 100644 src/main/java/nu/xss/jpa/entity/AbstractEntity.java create mode 100644 src/main/java/nu/xss/jpa/entity/AbstractIdEntity.java delete mode 100644 src/main/java/nu/xss/jpa/entity/Entity.java create mode 100644 src/main/java/nu/xss/jpa/entity/TypedEntity.java (limited to 'src') diff --git a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java index 9e2babb..21ee630 100644 --- a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java +++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java @@ -5,15 +5,20 @@ import java.lang.reflect.ParameterizedType; import java.util.List; import javax.persistence.EntityManager; +import javax.persistence.NoResultException; +import javax.persistence.NonUniqueResultException; import javax.persistence.PersistenceContext; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; +import nu.xss.jpa.entity.TypedEntity; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class GenericJpaDao implements Dao, Serializable { +public abstract class GenericJpaDao, K> implements + Dao, Serializable { private static final long serialVersionUID = 4998055731089977476L; protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -54,11 +59,52 @@ public abstract class GenericJpaDao implements Dao, Serializable { } public List findAll() { - CriteriaBuilder cb = entityManager.getCriteriaBuilder(); - CriteriaQuery c = cb.createQuery(this.entity); + return find(createSimpleQuery()); + } + + protected CriteriaBuilder getCriteriaBuilder() { + return entityManager.getCriteriaBuilder(); + } + + protected CriteriaQuery createQuery() { + CriteriaQuery c = getCriteriaBuilder().createQuery(this.entity); + return c; + } + + protected CriteriaQuery createSimpleQuery() { + CriteriaQuery c = getCriteriaBuilder().createQuery(this.entity); Root root = c.from(this.entity); c.select(root); - return entityManager.createQuery(c).getResultList(); + return c; } + protected List find(CriteriaQuery query) { + return entityManager.createQuery(query).getResultList(); + } + + protected E findSingle(CriteriaQuery query) { + return entityManager.createQuery(query).getSingleResult(); + } + + protected E findSingleOrNull(CriteriaQuery query) { + try { + return findSingle(query); + } catch (final NoResultException e) { + return null; + } catch (final NonUniqueResultException e) { + logger.error("Found more than one result.. return first one"); + return null; + } + } + + protected E findSingleFirstOrNull(CriteriaQuery query) { + try { + return findSingle(query); + } catch (final NoResultException e) { + return null; + } catch (final NonUniqueResultException e) { + logger.warn("Found more than one result.. return first one"); + return find(query).get(0); + } + } } diff --git a/src/main/java/nu/xss/jpa/entity/AbstractAutoIdEntity.java b/src/main/java/nu/xss/jpa/entity/AbstractAutoIdEntity.java new file mode 100644 index 0000000..ee5fd40 --- /dev/null +++ b/src/main/java/nu/xss/jpa/entity/AbstractAutoIdEntity.java @@ -0,0 +1,28 @@ +package nu.xss.jpa.entity; + +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; + +@MappedSuperclass +public abstract class AbstractAutoIdEntity extends AbstractEntity { + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + private K id; + + @Override + public K getId() { + return id; + } + + @Override + public void setId(K id) { + this.id = id; + } + + + +} diff --git a/src/main/java/nu/xss/jpa/entity/AbstractEntity.java b/src/main/java/nu/xss/jpa/entity/AbstractEntity.java new file mode 100644 index 0000000..5a4f468 --- /dev/null +++ b/src/main/java/nu/xss/jpa/entity/AbstractEntity.java @@ -0,0 +1,54 @@ +package nu.xss.jpa.entity; + +import javax.persistence.MappedSuperclass; + +@MappedSuperclass +public abstract class AbstractEntity implements TypedEntity { + + private static final long serialVersionUID = 1L; + + public AbstractEntity() { + } + + public AbstractEntity(K id) { + this.setId(id); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + (this.getId() == null ? 0 : this.getId().hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + + if (this == obj) { + return true; + } + + if (obj == null) { + return false; + } + + if (this.getClass() != obj.getClass()) { + return false; + } + + final AbstractEntity other = (AbstractEntity) obj; + + if (this.getId() == null) { + if (other.getId() != null) { + return false; + } + } else if (!this.getId().equals(other.getId())) { + return false; + } + + return true; + } + +} diff --git a/src/main/java/nu/xss/jpa/entity/AbstractIdEntity.java b/src/main/java/nu/xss/jpa/entity/AbstractIdEntity.java new file mode 100644 index 0000000..7e1e203 --- /dev/null +++ b/src/main/java/nu/xss/jpa/entity/AbstractIdEntity.java @@ -0,0 +1,25 @@ +package nu.xss.jpa.entity; + +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; + +@MappedSuperclass +public abstract class AbstractIdEntity extends AbstractEntity { + + private static final long serialVersionUID = 1L; + + @Id + private K id; + + @Override + public K getId() { + return id; + } + + @Override + public void setId(K id) { + this.id = id; + } + + +} diff --git a/src/main/java/nu/xss/jpa/entity/Entity.java b/src/main/java/nu/xss/jpa/entity/Entity.java deleted file mode 100644 index 1943e80..0000000 --- a/src/main/java/nu/xss/jpa/entity/Entity.java +++ /dev/null @@ -1,5 +0,0 @@ -package nu.xss.jpa.entity; - -public class Entity { - -} diff --git a/src/main/java/nu/xss/jpa/entity/TypedEntity.java b/src/main/java/nu/xss/jpa/entity/TypedEntity.java new file mode 100644 index 0000000..d08ff0f --- /dev/null +++ b/src/main/java/nu/xss/jpa/entity/TypedEntity.java @@ -0,0 +1,11 @@ +package nu.xss.jpa.entity; + +import java.io.Serializable; + +public interface TypedEntity extends Serializable { + + K getId(); + + void setId(K id); + +} -- cgit v1.2.3-55-g7522