diff options
-rw-r--r-- | src/main/java/nu/xss/jpa/dao/Dao.java | 1 | ||||
-rw-r--r-- | 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<E, K> { void save(E entity); + void merge(E entity); void delete(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 index f752aef..9e2babb 100644 --- a/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java +++ b/src/main/java/nu/xss/jpa/dao/GenericJpaDao.java @@ -13,30 +13,40 @@ import javax.persistence.criteria.Root; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class GenericJpaDao<E,K> implements Dao<E,K>, Serializable { - +public abstract class GenericJpaDao<E, K> implements Dao<E, K>, Serializable { + private static final long serialVersionUID = 4998055731089977476L; protected final Logger logger = LoggerFactory.getLogger(this.getClass()); - + @PersistenceContext protected EntityManager entityManager; - + protected Class<E> entity; - + @SuppressWarnings("unchecked") public GenericJpaDao() { - ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); + ParameterizedType genericSuperclass = (ParameterizedType) getClass() + .getGenericSuperclass(); this.entity = (Class<E>) 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) { |