1.0.0-SNAPSHOT

This commit is contained in:
insanity 2017-05-26 19:00:36 +09:00
parent 336e903435
commit d644072521
2 changed files with 35 additions and 15 deletions

View File

@ -13,7 +13,7 @@
<groupId>com.loafle</groupId>
<artifactId>overflow_jpa_base_dao</artifactId>
<packaging>jar</packaging>
<version>1.0.0-RELEASE</version>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@ -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<T> implements BaseDAO<T> {
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";
}
}
}