Olá,
uma duvida de como tratar em um DAO Generico
no caso abaixo qdo der um persist já commita mas se tiver que fazer o seguinte
EnderecoDAO enderecoDAO ....
enderecoDAO.persist(ende);
PaisDAO paisDAO = ...
paisDAO.persist(pais)
Cidade cidDAO =...
cidDAO.persist(cid);
-- se der erro aqui quero dar roolback mais do jeito
que está sempre comita ?????
qual outra forma de fazer ??????se alguém puder me ajudar ...abs
public class BaseDAO implements DAO {
private static Logger logger = Logger.getLogger(BaseDAO.class);
private Session session;
private Transaction tx;
public BaseDAO(Session session){
this.session = session;
}
@SuppressWarnings("unchecked")
public <T> T find(Class<T> classe, Serializable id) throws DaoException {
logger.info("Listando Id="+id);
tx = session.beginTransaction();
try{
return (T) session.get(classe, id);
}catch(Exception e) {
throw new DaoException("Erro =" +e);
}
}
public <T> void merge(T entity) throws DaoException {
logger.info("Salvando ou atualizando " + entity);
tx = session.beginTransaction();
try{
session.saveOrUpdate(entity);
tx.commit();
}catch(Exception e) {
tx.rollback();
throw new DaoException("Erro =" +e);
}
}
public <T> void persist(T entity) throws DaoException {
logger.info("salvando " + entity);
tx = session.beginTransaction();
try{
session.save(entity);
tx.commit();
}catch(Exception e) {
tx.rollback();
throw new DaoException("Erro =" +e);
}
}
public <T> void remove(T entity) throws DaoException {
logger.info("Deletando " + entity);
tx = session.beginTransaction();
try{
session.delete(entity);
tx.commit();
}catch(Exception e) {
tx.rollback();
throw new DaoException("Erro =" +e);
}
}
}