summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pom.xml47
-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
7 files changed, 207 insertions, 17 deletions
diff --git a/pom.xml b/pom.xml
index 4589b5b..96fe7af 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,9 +6,9 @@
<version>0.0.2-SNAPSHOT</version>
<properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>4.1.4.Final</hibernate.version>
- <slf4j.version>1.6.1</slf4j.version>
+ <slf4j.version>1.6.1</slf4j.version>
</properties>
<dependencies>
@@ -23,6 +23,11 @@
<version>${hibernate.version}</version>
</dependency>
<dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-jpamodelgen</artifactId>
+ <version>1.2.0.Final</version>
+ </dependency>
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
@@ -39,18 +44,44 @@
<configuration>
<source>1.7</source>
<target>1.7</target>
+ <optimize>true</optimize>
+ <debug>true</debug>
+ <compilerArguments>
+ <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
+ </compilerArguments>
</configuration>
</plugin>
- <!-- No dependencies in jar for helpers..
<plugin>
- <artifactId>maven-assembly-plugin</artifactId>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <version>1.7</version>
+ <executions>
+ <execution>
+ <id>add-source</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>add-source</goal>
+ </goals>
+ <configuration>
+ <sources>
+ <source>${project.build.directory}/generated-sources/annotations/</source>
+ </sources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.5</version>
<configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
+ <source>1.7</source>
+ <target>1.7</target>
</configuration>
</plugin>
- -->
+ <!-- No dependencies in jar for helpers.. <plugin> <artifactId>maven-assembly-plugin</artifactId>
+ <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs> </configuration> </plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
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);
+
+}