summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastian2012-06-03 12:17:24 +0200
committerSebastian2012-06-03 12:17:24 +0200
commitf419209f64249c4441920503582e194ba01e7195 (patch)
treef6d3ce32d6ef3897b9d4f877342e9bf0308614db /src
downloadxss-jpa-f419209f64249c4441920503582e194ba01e7195.tar.gz
xss-jpa-f419209f64249c4441920503582e194ba01e7195.tar.xz
xss-jpa-f419209f64249c4441920503582e194ba01e7195.zip
initial import
Diffstat (limited to 'src')
-rw-r--r--src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java40
-rw-r--r--src/main/java/nu/xss/jpa/dao/Dao.java10
-rw-r--r--src/main/java/nu/xss/jpa/dao/GenericJpaDao.java43
-rw-r--r--src/main/java/nu/xss/jpa/entity/Entity.java5
4 files changed, 98 insertions, 0 deletions
diff --git a/src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java b/src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java
new file mode 100644
index 0000000..aef1aaf
--- /dev/null
+++ b/src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java
@@ -0,0 +1,40 @@
+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(unitName="mainPU")
+ 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
new file mode 100644
index 0000000..b91c834
--- /dev/null
+++ b/src/main/java/nu/xss/jpa/dao/Dao.java
@@ -0,0 +1,10 @@
+package nu.xss.jpa.dao;
+
+import java.util.List;
+
+public interface Dao<E, K> {
+ void persist(E entity);
+ void remove(E entity);
+ E findById(K id);
+ List<E> findAll();
+}
diff --git a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
new file mode 100644
index 0000000..5acfffc
--- /dev/null
+++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java
@@ -0,0 +1,43 @@
+package nu.xss.jpa.dao;
+
+import java.io.Serializable;
+import java.lang.reflect.ParameterizedType;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+public abstract class GenericJpaDao<E,K> implements Dao<E,K>, Serializable {
+
+ private static final long serialVersionUID = 4998055731089977476L;
+
+ @PersistenceContext
+ protected EntityManager entityManager;
+
+ protected Class<E> entity;
+
+ @SuppressWarnings("unchecked")
+ public GenericJpaDao() {
+ ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
+ this.entity = (Class<E>) genericSuperclass.getActualTypeArguments()[1];
+ }
+
+ 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/entity/Entity.java b/src/main/java/nu/xss/jpa/entity/Entity.java
new file mode 100644
index 0000000..1943e80
--- /dev/null
+++ b/src/main/java/nu/xss/jpa/entity/Entity.java
@@ -0,0 +1,5 @@
+package nu.xss.jpa.entity;
+
+public class Entity {
+
+}