From 2907d0cb9fd535155a46144dbcff447ab495d77c Mon Sep 17 00:00:00 2001 From: Johann Latocha Date: Thu, 5 Jul 2012 13:39:31 +0200 Subject: add merge --- src/main/java/nu/xss/jpa/dao/Dao.java | 1 + src/main/java/nu/xss/jpa/dao/GenericJpaDao.java | 28 +++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/nu/xss/jpa/dao/Dao.java b/src/main/java/nu/xss/jpa/dao/Dao.java index 18d886f..2c541c9 100644 --- a/src/main/java/nu/xss/jpa/dao/Dao.java +++ b/src/main/java/nu/xss/jpa/dao/Dao.java @@ -4,6 +4,7 @@ import java.util.List; public interface Dao { void save(E entity); + void merge(E entity); void delete(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 index e282911..cac96e6 100644 --- a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java +++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java @@ -10,30 +10,40 @@ import javax.persistence.PersistenceContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class GenericJpaDao implements Dao, Serializable { - +public abstract class GenericJpaDao implements Dao, Serializable { + private static final long serialVersionUID = 4998055731089977476L; protected final Logger logger = LoggerFactory.getLogger(this.getClass()); - + @PersistenceContext protected EntityManager entityManager; - + protected Class entity; - + @SuppressWarnings("unchecked") public GenericJpaDao() { - ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); + ParameterizedType genericSuperclass = (ParameterizedType) getClass() + .getGenericSuperclass(); this.entity = (Class) genericSuperclass.getActualTypeArguments()[0]; } - + public void save(E entity) { + logger.info("Persist Entity: {}.", entity.toString()); entityManager.persist(entity); - logger.info("Saved entity: {}.", entity.toString()); + entityManager.flush(); + logger.info("Saved entity: {}.", entity.toString()); + } + + public void merge(E entity) { + logger.info("Merge Entity: {}.", entity.toString()); + entityManager.merge(entity); + entityManager.flush(); + logger.info("Saved entity: {}.", entity.toString()); } public void delete(E entity) { entityManager.remove(entity); - logger.info("Deleted entity: {}.", entity.toString()); + logger.info("Deleted entity: {}.", entity.toString()); } public E findById(K id) { -- cgit v1.2.3-55-g7522