diff --git a/pom.xml b/pom.xml
index f4c48bd..f8df332 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,7 +13,7 @@
com.loafle
overflow_jpa_base_dao
jar
- 1.0.0-RELEASE
+ 1.0.0-SNAPSHOT
diff --git a/src/main/java/com/loafle/overflow/commons/dao/JPABaseDAO.java b/src/main/java/com/loafle/overflow/commons/dao/JPABaseDAO.java
index 53524a8..30181bb 100644
--- a/src/main/java/com/loafle/overflow/commons/dao/JPABaseDAO.java
+++ b/src/main/java/com/loafle/overflow/commons/dao/JPABaseDAO.java
@@ -1,6 +1,7 @@
package com.loafle.overflow.commons.dao;
import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import java.lang.reflect.ParameterizedType;
@@ -23,28 +24,47 @@ public class JPABaseDAO implements BaseDAO {
return this.entityManager;
}
- public T find(T entity)
- {
- return this.entityManager.find(entityType, entity);
+ public T find(String id) {
+ return this.entityManager.find(this.entityType, Long.parseLong(id));
}
public T create(T entity) {
- this.entityManager.getTransaction().begin();
- this.entityManager.persist(entity);
- this.entityManager.getTransaction().commit();
+ EntityTransaction tx = this.entityManager.getTransaction();
+ if (!tx.isActive()) tx.begin();
+ try {
+ this.entityManager.persist(entity);
+ tx.commit();
+ }catch(Exception e) {
+ tx.rollback();
+ return null;
+ }
return entity;
}
public T update(T entity) {
- this.entityManager.getTransaction().begin();
- T result = this.entityManager.merge(entity);
- this.entityManager.getTransaction().commit();
- return result;
+ EntityTransaction tx = this.entityManager.getTransaction();
+ if (!tx.isActive()) tx.begin();
+ try {
+ T result = this.entityManager.merge(entity);
+ tx.commit();
+ return result;
+ }catch(Exception e) {
+ tx.rollback();
+ return null;
+ }
}
- public void delete(T entity) {
- this.entityManager.getTransaction().begin();
- this.entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
- this.entityManager.getTransaction().commit();
+ public String delete(T entity) {
+ EntityTransaction tx = this.entityManager.getTransaction();
+ if (!tx.isActive()) tx.begin();
+ try {
+ this.entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
+ tx.commit();
+ return "Success";
+ }catch(Exception e) {
+ tx.rollback();
+ return "Fail";
+ }
+
}
}