Napisałem metody do utrwalania danych w bazie danych.
Mógłby ktos powiedzieć czy jest to poprawne, ewntualnie poprawić??
Będę niesamowicie zobowiązany.
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class Factory {
private static EntityManagerFactory entityManagerFactory;
private static final Logger log = Logger.getLogger(Factory.class.getSimpleName());
public static EntityManagerFactory buildEntityManagerFactory() {
closeFactory();
return configureEntityManagerFactory();
}
public static EntityManagerFactory buildIfNeeded() {
if (entityManagerFactory != null) {
return entityManagerFactory;
}
return buildEntityManagerFactory();
}
public static void rollback(EntityTransaction tx) {
try {
if (tx != null) {
tx.rollback();
}
} catch (RuntimeException ex) {
log.log(Level.WARNING, "Can't rollback", ex);
}
}
public static void close(EntityManager entityManager) {
if (entityManager != null) {
try {
entityManager.close();
} catch (IllegalStateException ex) {
log.log(Level.WARNING, "Can't close", ex);
}
}
}
public static void closeFactory() {
if (entityManagerFactory != null) {
try {
entityManagerFactory.close();
} catch (Exception ex) {
log.log(Level.WARNING, "Cant close", ex);
}
}
}
public static EntityManager getEntityManager() {
buildIfNeeded();
return entityManagerFactory.createEntityManager();
}
private static EntityManagerFactory configureEntityManagerFactory() {
entityManagerFactory = Persistence.createEntityManagerFactory("JPAService");
return entityManagerFactory;
}
}
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
public class DAO {
private EntityManager entityManager;
private EntityTransaction tx;
public DAO() {
Factory.buildIfNeeded();
}
public void persist(Object obj) throws Exception {
try {
startOperation();
entityManager.persist(obj);
tx.commit();
} catch (Exception e) {
handleException(e);
} finally {
Factory.close(entityManager);
}
}
public void merge(Object obj) throws Exception {
try {
startOperation();
entityManager.merge(obj);
tx.commit();
} catch (Exception e) {
handleException(e);
} finally {
Factory.close(entityManager);
}
}
public void remove(Object obj) throws Exception {
try {
startOperation();
entityManager.remove(obj);
tx.commit();
} catch (Exception e) {
handleException(e);
} finally {
Factory.close(entityManager);
}
}
public void removeAll(Class clazz) throws Exception {
try {
startOperation();
Query query = entityManager.createQuery("DELETE From " + clazz.getName());
query.executeUpdate();
tx.commit();
} catch (Exception e) {
handleException(e);
} finally {
Factory.close(entityManager);
}
}
public Object find(String query, Class clazz) throws Exception {
Object object = null;
try {
startOperation();
Query q1 = entityManager.createNamedQuery(query, clazz);
object = q1.getSingleResult();
tx.commit();
} catch (Exception e) {
handleException(e);
} finally {
Factory.close(entityManager);
}
return object;
}
public <T>Object find(String query, T parameter, Class clazz) throws Exception {
Object object = null;
try {
startOperation();
Query q1 = entityManager.createNamedQuery(query, clazz).setParameter("parameter", parameter);
object = q1.getSingleResult();
tx.commit();
} catch (Exception e) {
handleException(e);
} finally {
Factory.close(entityManager);
}
return object;
}
public List findAll(String query, Class clazz) throws Exception {
List objects = null;
try {
startOperation();
Query q1 = entityManager.createNamedQuery(query, clazz);
objects = q1.getResultList();
tx.commit();
} catch (Exception e) {
handleException(e);
} finally {
Factory.close(entityManager);
}
return objects;
}
protected void handleException(Exception ex) throws Exception {
Factory.rollback(tx);
throw ex;
}
protected void startOperation() {
this.entityManager = Factory.getEntityManager();
tx = entityManager.getTransaction();
tx.begin();
}
}
Pozdrawiam
Paweł