Bom dia,
Estou tentando implantar o JPA em um sistema desenvolvido utlizando Struts, MySQL, Tomcat 5.5, Hibernate
O MyEclipse gera os beans, as classes de DAO e EntityManagerHelper apartir de uma conexão com o banco de dados.
Na minha action Empresa Save coloquei o seguinte codigo:
EmpresaForm empresaForm = (EmpresaForm) form;
Empresa empresa = empresaForm.getEmpresa();
empresa.setDtMovi(dat);
EmpresaDAO dao = new EmpresaDAO();
dao.save(empresa);
Sendo que após preencher o formulário o mesmo estará no objeto a ser salvo. Ele me retorna uma mensagem de “save successful”, mas os dados não foram para o banco. Creio que seja a falta do commit…mas como o codigo foi gerado…estou perdido já que sou novato no JPA.
Classe EmpresaDAO:
package com.wr.business.jpa;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import javax.persistence.EntityManager;
/**
* Data access object (DAO) for domain model class Empresa.
*
* @see com.wr.business.jpa.Empresa
* @author MyEclipse Persistence Tools
*/
public class EmpresaDAO {
// property constants
public static final String STR_NOME_FANTASIA = "strNomeFantasia";
public static final String STR_ENDERECO = "strEndereco";
public static final String STR_NOME_CONTATO = "strNomeContato";
public static final String STR_SITE = "strSite";
public static final String STR_FONE = "strFone";
public static final String STR_EMAIL = "strEmail";
public static final String STR_LOGO = "strLogo";
private EntityManager getEntityManager() {
return EntityManagerHelper.getEntityManager();
}
public void save(Empresa transientInstance) {
EntityManagerHelper.log("saving Empresa instance", Level.INFO, null);
try {
getEntityManager().persist(transientInstance);
EntityManagerHelper.log("save successful", Level.INFO, null);
} catch (RuntimeException re) {
EntityManagerHelper.log("save failed", Level.SEVERE, re);
throw re;
}
}
public void delete(Empresa persistentInstance) {
EntityManagerHelper.log("deleting Empresa instance", Level.INFO, null);
try {
getEntityManager().remove(persistentInstance);
EntityManagerHelper.log("delete successful", Level.INFO, null);
} catch (RuntimeException re) {
EntityManagerHelper.log("delete failed", Level.SEVERE, re);
throw re;
}
}
public Empresa update(Empresa detachedInstance) {
EntityManagerHelper.log("updating Empresa instance", Level.INFO, null);
try {
Empresa result = getEntityManager().merge(detachedInstance);
EntityManagerHelper.log("update successful", Level.INFO, null);
return result;
} catch (RuntimeException re) {
EntityManagerHelper.log("update failed", Level.SEVERE, re);
throw re;
}
}
public Empresa findById(Integer id) {
EntityManagerHelper.log("finding Empresa instance with id: " + id,
Level.INFO, null);
try {
Empresa instance = getEntityManager().find(Empresa.class, id);
return instance;
} catch (RuntimeException re) {
EntityManagerHelper.log("find failed", Level.SEVERE, re);
throw re;
}
}
@SuppressWarnings("unchecked")
public List<Empresa> findByProperty(String propertyName, Object value) {
EntityManagerHelper.log("finding Empresa instance with property: "
+ propertyName + ", value: " + value, Level.INFO, null);
try {
String queryString = "select model from Empresa model where model."
+ propertyName + "= :propertyValue";
return getEntityManager().createQuery(queryString).setParameter(
"propertyValue", value).getResultList();
} catch (RuntimeException re) {
EntityManagerHelper.log("find by property name failed",
Level.SEVERE, re);
throw re;
}
}
public List<Empresa> findByStrNomeFantasia(Object strNomeFantasia) {
return findByProperty(STR_NOME_FANTASIA, strNomeFantasia);
}
public List<Empresa> findByStrEndereco(Object strEndereco) {
return findByProperty(STR_ENDERECO, strEndereco);
}
public List<Empresa> findByStrNomeContato(Object strNomeContato) {
return findByProperty(STR_NOME_CONTATO, strNomeContato);
}
public List<Empresa> findByStrSite(Object strSite) {
return findByProperty(STR_SITE, strSite);
}
public List<Empresa> findByStrFone(Object strFone) {
return findByProperty(STR_FONE, strFone);
}
public List<Empresa> findByStrEmail(Object strEmail) {
return findByProperty(STR_EMAIL, strEmail);
}
public List<Empresa> findByStrLogo(Object strLogo) {
return findByProperty(STR_LOGO, strLogo);
}
@SuppressWarnings("unchecked")
public List<Empresa> findAll() {
EntityManagerHelper.log("finding all Empresa instances", Level.INFO,
null);
try {
String queryString = "select model from Empresa model";
return getEntityManager().createQuery(queryString).getResultList();
} catch (RuntimeException re) {
EntityManagerHelper.log("find all failed", Level.SEVERE, re);
throw re;
}
}
}
Esta é a classe onde se encontra o Commit…mas não sei como utilizá-lo…
package com.wr.business.jpa;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
/**
* @author MyEclipse Persistence Tools
*/
public class EntityManagerHelper {
private static final EntityManagerFactory emf;
private static final ThreadLocal<EntityManager> threadLocal;
private static final Logger logger;
static {
emf = Persistence.createEntityManagerFactory("wrBoleto");
threadLocal = new ThreadLocal<EntityManager>();
logger = Logger.getLogger("wrBoleto");
logger.setLevel(Level.ALL);
}
public static EntityManager getEntityManager() {
EntityManager manager = threadLocal.get();
if (manager == null || !manager.isOpen()) {
manager = emf.createEntityManager();
threadLocal.set(manager);
}
return manager;
}
public static void closeEntityManager() {
EntityManager em = threadLocal.get();
threadLocal.set(null);
if (em != null) em.close();
}
public static void beginTransaction() {
getEntityManager().getTransaction().begin();
}
public static void commit() {
getEntityManager().getTransaction().commit();
}
public static Query createQuery(String query) {
return getEntityManager().createQuery(query);
}
public static void log(String info, Level level, Throwable ex) {
logger.log(level, info, ex);
}
}
alguem ja passou por isso???
grato