summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastian Schmelzer2012-07-30 19:20:05 +0200
committerSebastian Schmelzer2012-07-30 19:20:05 +0200
commit2a620bf30ada3e5b09bb370d1a9fed02a78c53ea (patch)
tree4d93e2dbaa3b655c8f90cc1bc0c5e85151d6e1cc /src
parentMerge branch 'master' of xss.nu:xss-jpa (diff)
downloadxss-jpa-2a620bf30ada3e5b09bb370d1a9fed02a78c53ea.tar.gz
xss-jpa-2a620bf30ada3e5b09bb370d1a9fed02a78c53ea.tar.xz
xss-jpa-2a620bf30ada3e5b09bb370d1a9fed02a78c53ea.zip
Added abstract entity classes
Diffstat (limited to 'src')
-rw-r--r--src/main/java/nu/xss/jpa/dao/GenericJpaDao.java54
-rw-r--r--src/main/java/nu/xss/jpa/entity/AbstractAutoIdEntity.java28
-rw-r--r--src/main/java/nu/xss/jpa/entity/AbstractEntity.java54
-rw-r--r--src/main/java/nu/xss/jpa/entity/AbstractIdEntity.java25
-rw-r--r--src/main/java/nu/xss/jpa/entity/Entity.java5
-rw-r--r--src/main/java/nu/xss/jpa/entity/TypedEntity.java11
6 files changed, 168 insertions, 9 deletions
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<E, K> implements Dao<E, K>, Serializable {
+public abstract class GenericJpaDao<E extends TypedEntity<K>, K> implements
+ Dao<E, K>, Serializable {
private static final long serialVersionUID = 4998055731089977476L;
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -54,11 +59,52 @@ public abstract class GenericJpaDao<E, K> implements Dao<E, K>, Serializable {
}
public List<E> findAll() {
- CriteriaBuilder cb = entityManager.getCriteriaBuilder();
- CriteriaQuery<E> c = cb.createQuery(this.entity);
+ return find(createSimpleQuery());
+ }
+
+ protected CriteriaBuilder getCriteriaBuilder() {
+ return entityManager.getCriteriaBuilder();
+ }
+
+ protected CriteriaQuery<E> createQuery() {
+ CriteriaQuery<E> c = getCriteriaBuilder().createQuery(this.entity);
+ return c;
+ }
+
+ protected CriteriaQuery<E> createSimpleQuery() {
+ CriteriaQuery<E> c = getCriteriaBuilder().createQuery(this.entity);
Root<E> root = c.from(this.entity);
c.select(root);
- return entityManager.createQuery(c).getResultList();
+ return c;
}
+ protected List<E> find(CriteriaQuery<E> query) {
+ return entityManager.createQuery(query).getResultList();
+ }
+
+ protected E findSingle(CriteriaQuery<E> query) {
+ return entityManager.createQuery(query).getSingleResult();
+ }
+
+ protected E findSingleOrNull(CriteriaQuery<E> 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<E> 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<K> extends AbstractEntity<K> {
+
+ 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<K> implements TypedEntity<K> {
+
+ 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<K> extends AbstractEntity<K> {
+
+ 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<K> extends Serializable {
+
+ K getId();
+
+ void setId(K id);
+
+}