From f419209f64249c4441920503582e194ba01e7195 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 3 Jun 2012 12:17:24 +0200 Subject: initial import --- src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java | 40 ++++++++++++++++++++++ src/main/java/nu/xss/jpa/dao/Dao.java | 10 ++++++ src/main/java/nu/xss/jpa/dao/GenericJpaDao.java | 43 ++++++++++++++++++++++++ src/main/java/nu/xss/jpa/entity/Entity.java | 5 +++ 4 files changed, 98 insertions(+) create mode 100644 src/main/java/nu/xss/jpa/dao/AbstractJpaDao.java create mode 100644 src/main/java/nu/xss/jpa/dao/Dao.java create mode 100644 src/main/java/nu/xss/jpa/dao/GenericJpaDao.java create mode 100644 src/main/java/nu/xss/jpa/entity/Entity.java (limited to 'src') 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 implements Dao, Serializable { + + private static final long serialVersionUID = 4998055731089977476L; + + @PersistenceContext(unitName="mainPU") + protected EntityManager entityManager; + + protected Class entity; + + public AbstractJpaDao(final Class entity) { + this.entity = (Class) 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 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 { + void persist(E entity); + void remove(E entity); + E findById(K id); + List 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 implements Dao, Serializable { + + private static final long serialVersionUID = 4998055731089977476L; + + @PersistenceContext + protected EntityManager entityManager; + + protected Class entity; + + @SuppressWarnings("unchecked") + public GenericJpaDao() { + ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); + this.entity = (Class) 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 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 { + +} -- cgit v1.2.3-55-g7522