Olá pessoal, blz?
Depois de um tempo parado fui pegar pra fazer uns exercicios com JAVA e estou tendo umas dificuldades…
Esse pequeno projetinho que to fazendo, já tinha feito a um tempinho atrás, basicamente fui dando ctrl+c, ctrl+v em vários trechso do código e modificando e completando o que faltava…
Até onde eu vejo, está tudo certo, mas estou tendo ums erros.
Vou postar o codigo das minhas classes Usuario.java, UsuarioDecorator.java, JPADAO.java, TesteUsuRepository.java e meu applicationContext.xml Se precisar de mais alguma coisa me avisem. No final do posto segue o trace do erro.
Eu percebi que no trace diz alguma coisa sobre “No Persistence provider for EntityManager named informit” mas não consegui entender o significado disso, aparentemente tá tudo certo.
Desculpa o excesso de código, mas espero que pelo menos isso ajude alguém a me dar uma pista de onde está meu problema.
PS: Se no arquivo TesteUsuRepository.java que é um JUnit Test Case eu removo a dependencia do Spring e crio “na mão” o UsuarioDecorator e o JPADAO tudo funciona certinho, já fiz esse teste. Inclusive grava no banco de dados, tudo correto. Mas com o Spring num vai…
Agradeço dese já a atenção. Valew.
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="usuTeste" class="mitologics.model.beans.Usuario">
<property name="login" value="usuario" />
<property name="nome" value="Usuario Teste" />
<property name="senha" value="teste" />
<property name="direitos" value="ADMIN" />
<property name="ativo" value="true" />
</bean>
<bean id="conTeste" class="mitologics.model.beans.Contato">
<property name="nome" value="Teste" />
<property name="apelido" value="Contato Teste" />
<property name="contato" value="Alguem" />
<property name="endereco" value="Rua Teste" />
<property name="numero" value="1000" />
<property name="bairro" value="Bairro Teste" />
<property name="cidade" value="Testopolis" />
<property name="uf" value="SP" />
<property name="complemento" value="Casa" />
<property name="referencia" value="Proximo ao outro Teste" />
<property name="cep" value="13000000" />
<property name="tel1" value="([telefone removido]" />
<property name="tel2" value="([telefone removido]" />
<property name="email1" value="[email removido]" />
<property name="site" value="http://www.teste.com" />
<property name="cpfcnpj" value="[telefone removido]" />
<property name="rgie" value="22222222" />
<property name="indicado" value="Teste2" />
</bean>
<bean id="usuDAO" class="mitologics.dao.JPADAO">
<constructor-arg value="mitologics.model.beans.Usuario" />
</bean>
<bean id="usuRepo" class="mitologics.model.infrastructure.UsuarioDecorator">
<property name="dao" ref="usuDAO"/>
</bean>
<bean id="conDAO" class="mitologics.dao.JPADAO">
<constructor-arg value="mitologics.model.beans.Contato" />
</bean>
<bean id="conRepo" class="mitologics.model.infrastructure.ContatoDecorator">
<property name="dao" ref="conDAO"/>
</bean>
</beans>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="informit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>mitologics.model.beans.Usuario</class>
<class>mitologics.model.beans.Contato</class>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connecion.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/informit"/>
<property name="hibernate.connection.username" value="mitologics"/>
<property name="hibernate.connection.password" value="mitologics"/>
</properties>
</persistence-unit>
</persistence>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>InforMitJPA</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<!-- Listeners -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Welcome file lists -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
JPADAO.java
package mitologics.dao;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
@SuppressWarnings("unchecked")
public class JPADAO<PersistentObject, IDType> implements DAO<PersistentObject, IDType> {
private static final String PERSISTENCE_UNIT = "informit";
private Class<PersistentObject> persistentClass;
private EntityManager em;
private static EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
public JPADAO(Class<PersistentObject> clazz) {
persistentClass = clazz;
}
protected EntityManager getEntityManager() {
if (emf == null || !emf.isOpen()) {
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
}
if (em != null && em.isOpen()) {
return em;
}
return emf.createEntityManager();
}
public void close() {
if (em != null && em.isOpen())
em.close();
}
public PersistentObject find(IDType id) {
em = getEntityManager();
return em.find(persistentClass, id);
}
public PersistentObject getByNamedQuery(String queryName, Map<String, Object> params) {
em = getEntityManager();
Query query = em.createNamedQuery(queryName);
if(params != null && !params.isEmpty()){
for(String key : params.keySet()){
query.setParameter(key, params.get(key));
}
}
PersistentObject result;
try{
result = (PersistentObject) query.getSingleResult();
}catch(NoResultException nre){
try {
return persistentClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Error in named query: "+queryName + " - InstatiationException");
} catch (IllegalAccessException e) {
throw new RuntimeException("Error in named query: "+queryName + " - IllegalAccessException");
}
}
return result;
}
public List<PersistentObject> list() {
em = getEntityManager();
Query query = em.createQuery("SELECT obj FROM " + persistentClass.getSimpleName() + " obj");
List<PersistentObject> list = query.getResultList();
return list;
}
public List<PersistentObject> listByNamedQuery(String queryName, Map<String, Object> params) {
em = getEntityManager();
Query query = em.createNamedQuery(queryName);
if(params != null && !params.isEmpty()){
for(String key : params.keySet()){
query.setParameter(key, params.get(key));
}
}
List<PersistentObject> list = query.getResultList();
return list;
}
public PersistentObject merge(PersistentObject obj) {
em = getEntityManager();
EntityTransaction tx = em.getTransaction();
try{
tx.begin();
obj = em.merge(obj);
tx.commit();
}catch(PersistenceException pe){
tx.rollback();
pe.printStackTrace();
}
return obj;
}
@SuppressWarnings("finally")
public PersistentObject persist(PersistentObject obj) {
em = getEntityManager();
EntityTransaction tx = em.getTransaction();
try{
tx.begin();
em.persist(obj);
tx.commit();
try{
em.refresh(obj);
}finally{
return obj;
}
}catch(PersistenceException pe){
tx.rollback();
pe.printStackTrace();
}
return obj;
}
public void remove(PersistentObject obj) {
em = getEntityManager();
EntityTransaction tx = em.getTransaction();
try{
tx.begin();
obj = em.merge(obj);
em.remove(obj);
tx.commit();
}catch(RuntimeException re){
tx.rollback();
re.printStackTrace();
throw re;
}
}
}
Usuario.java
package mitologics.model.beans;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
@NamedQueries( { @NamedQuery(name = "Usuario.getByLogin", query = "SELECT u FROM Usuario u WHERE u.login = :login and u.senha = :senha") })
public class Usuario implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(nullable = false, unique = true)
private String login;
private String nome;
@Column(nullable = false)
private String senha;
private String direitos;
private boolean ativo;
//Construtor padrão
public Usuario() {
}
//Construtor minimo
public Usuario(String login, String senha) {
super();
this.nome = login;
this.login = login;
this.senha = senha;
this.ativo = false;
}
//Construtor completo
public Usuario(String login, String senha,
String nome, String direitos, boolean ativo) {
super();
this.direitos = direitos;
this.login = login;
this.nome = nome;
this.senha = senha;
this.ativo = ativo;
}
public final String getLogin() {
return login;
}
public final void setLogin(String login) {
this.login = login;
}
public final String getNome() {
return nome;
}
public final void setNome(String nome) {
this.nome = nome;
}
public final String getSenha() {
return senha;
}
public final void setSenha(String senha) {
this.senha = senha;
}
public final String getDireitos() {
return direitos;
}
public final void setDireitos(String direitos) {
this.direitos = direitos;
}
public final boolean getAtivo() {
return ativo;
}
public final boolean isAtivo() {
return ativo;
}
public final void setAtivo(boolean ativo) {
this.ativo = ativo;
}
@Override
public Usuario clone(){
return new Usuario(this.getLogin(), this.getSenha(), this.getNome(), this.getDireitos(), this.getAtivo());
}
@Override
public boolean equals(Object obj) {
Usuario tmpUsu = (Usuario)obj;
if (this.getLogin() == tmpUsu.getLogin())
return true;
else
return false;
}
@Override
public String toString() {
return this.login + "," + this.senha + "," + this.nome + "," + this.direitos + "," + this.ativo;
}
}
UsuarioDecorator.java
package mitologics.model.infrastructure;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.NoResultException;
import mitologics.dao.DAO;
import mitologics.model.beans.Usuario;
public class UsuarioDecorator implements UsuarioRepository {
private DAO<Usuario, String> dao;
public void setDao(DAO<Usuario, String> dao) {
this.dao = dao;
}
@Override
public List<Usuario> allUsers() {
return dao.list();
}
@Override
public void remove(Usuario usu) {
dao.remove(usu);
}
@Override
public Usuario save(Usuario usu) {
Usuario newUsu = usu.clone();
if (dao.find(usu.getLogin()) != null) {
return dao.merge(usu);
}
return dao.persist(newUsu);
}
@Override
public Usuario userWithId(String id) {
return dao.find(id);
}
@Override
public Usuario userWithLogin(String login, String senha) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("login", login);
params.put("senha", senha);
try {
return dao.getByNamedQuery("Usuario.getByLogin", params);
} catch (NoResultException nre) {
return null;
}
}
}
Testeusuario.java (JUnit4)
package mitologics.test;
import static junit.framework.Assert.*;
import java.util.List;
import mitologics.model.beans.Usuario;
import mitologics.model.infrastructure.UsuarioDecorator;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TesteUsuario {
private static UsuarioDecorator repo;
private static ApplicationContext ctx;
@BeforeClass
public static void prepare() {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
repo = (UsuarioDecorator) ctx.getBean("usuRepo");
}
@Test
public void Teste(){
Usuario admin = repo.userWithId("admin");
if (admin != null)
if (admin.getLogin() != null)
repo.remove(admin);
Usuario usu1 = (Usuario) ctx.getBean("usuTeste");
usu1 = repo.save(usu1);
assertNotNull(usu1);
assertEquals("ADMIN",usu1.getDireitos());
usu1.setDireitos("CLIENT");
usu1 = repo.save(usu1);
assertEquals("CLIENT",usu1.getDireitos());
Usuario usu2 = new Usuario("teste", "teste123");
assertNotNull(usu2);
usu2.setNome("Teste 2");
usu2 = repo.save(usu2);
assertNotNull(usu2);
List<Usuario> usus = repo.allUsers();
assertEquals(2,usus.size());
usu2 = null;
usu2 = repo.userWithLogin("teste", "teste456");
assertNull(usu2.getLogin());
usu2 = repo.userWithLogin("teste", "teste123");
assertNotNull("teste123", usu2.getSenha());
repo.remove(usu2);
usus = null;
usus = repo.allUsers();
assertEquals(1,usus.size());
repo.remove(usu1);
assertNull(repo.userWithId("usuario"));
Usuario usu3 = new Usuario("admin", "admin123", "Administrador", "ADMIN", true);
repo.save(usu3);
assertNotNull(repo.userWithId("admin"));
}
}
Log de erro
Log de erro
Contato.java
package mitologics.model.beans;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
@NamedQueries( { @NamedQuery(name = "Contato.getByName", query = "SELECT c FROM Contato c WHERE c.nome = :nome") })
public class Contato implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(nullable = false, unique = true)
private int id;
@Column(nullable = false)
private String nome;
private String apelido;
private String contato;
private String endereco;
private String numero;
private String bairro;
@Column(nullable = false)
private String cidade;
@Column(nullable = false)
private String uf;
private String complemento;
private String referencia;
private String cep;
@Column(nullable = false)
private String tel1;
private String tel2;
private String tel3;
private String email1;
private String email2;
private String site;
private String cpfcnpj;
private String rgie;
private String indicado;
private Date nascimento;
//Construtor padrão
public Contato(){
}
//Construtor minimo
public Contato(String nome, String cidade, String uf, String tel1) {
super();
this.nome = nome;
this.apelido = nome;
this.cidade = cidade;
this.tel1 = tel1;
this.uf = uf;
}
//Construtor completo
public Contato(int id, String nome, String apelido, String contato, String endereco,
String numero, String complemento, String bairro, String cidade, String uf,
String referencia, String cep, String tel1, String tel2, String tel3,
String email1, String email2, String site, String cpfcnpj, String rgie,
String indicado, Date nascimento) {
super();
this.apelido = apelido;
this.bairro = bairro;
this.cep = cep;
this.cidade = cidade;
this.complemento = complemento;
this.contato = contato;
this.cpfcnpj = cpfcnpj;
this.email1 = email1;
this.email2 = email2;
this.endereco = endereco;
this.id = id;
this.indicado = indicado;
this.nascimento = nascimento;
this.nome = nome;
this.numero = numero;
this.referencia = referencia;
this.rgie = rgie;
this.site = site;
this.tel1 = tel1;
this.tel2 = tel2;
this.tel3 = tel3;
this.uf = uf;
}
public final int getId() {
return id;
}
public final void setId(int id) {
this.id = id;
}
public final String getNome() {
return nome;
}
public final void setNome(String nome) {
this.nome = nome;
}
public final String getApelido() {
return apelido;
}
public final void setApelido(String apelido) {
this.apelido = apelido;
}
public final String getContato() {
return contato;
}
public final void setContato(String contato) {
this.contato = contato;
}
public final String getEndereco() {
return endereco;
}
public final void setEndereco(String endereco) {
this.endereco = endereco;
}
public final String getNumero() {
return numero;
}
public final void setNumero(String numero) {
this.numero = numero;
}
public final String getBairro() {
return bairro;
}
public final void setBairro(String bairro) {
this.bairro = bairro;
}
public final String getCidade() {
return cidade;
}
public final void setCidade(String cidade) {
this.cidade = cidade;
}
public final String getUf() {
return uf;
}
public final void setUf(String uf) {
this.uf = uf;
}
public final String getComplemento() {
return complemento;
}
public final void setComplemento(String complemento) {
this.complemento = complemento;
}
public final String getReferencia() {
return referencia;
}
public final void setReferencia(String referencia) {
this.referencia = referencia;
}
public final String getCep() {
return cep;
}
public final void setCep(String cep) {
this.cep = cep;
}
public final String getTel1() {
return tel1;
}
public final void setTel1(String tel1) {
this.tel1 = tel1;
}
public final String getTel2() {
return tel2;
}
public final void setTel2(String tel2) {
this.tel2 = tel2;
}
public final String getTel3() {
return tel3;
}
public final void setTel3(String tel3) {
this.tel3 = tel3;
}
public final String getEmail1() {
return email1;
}
public final void setEmail1(String email1) {
this.email1 = email1;
}
public final String getEmail2() {
return email2;
}
public final void setEmail2(String email2) {
this.email2 = email2;
}
public final String getSite() {
return site;
}
public final void setSite(String site) {
this.site = site;
}
public final String getCpfcnpj() {
return cpfcnpj;
}
public final void setCpfcnpj(String cpfcnpj) {
this.cpfcnpj = cpfcnpj;
}
public final String getRgie() {
return rgie;
}
public final void setRgie(String rgie) {
this.rgie = rgie;
}
public final String getIndicado() {
return indicado;
}
public final void setIndicado(String indicado) {
this.indicado = indicado;
}
public final Date getNascimento() {
return nascimento;
}
public final void setNascimento(Date nascimento) {
this.nascimento = nascimento;
}
}
ContatoDecorator.java
package mitologics.model.infrastructure;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.NoResultException;
import mitologics.dao.DAO;
import mitologics.model.beans.Contato;
public class ContatoDecorator implements ContatoRepository {
private DAO<Contato, Integer> dao;
public void setDao(DAO<Contato, Integer> dao) {
this.dao = dao;
}
@Override
public List<Contato> allContacts() {
return dao.list();
}
@Override
public Contato contByName(String nome) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("nome", nome);
try {
return dao.getByNamedQuery("Contato.getByName", params);
} catch (NoResultException nre) {
return null;
}
}
@Override
public Contato contWithId(int id) {
return dao.find(id);
}
@Override
public void remove(Contato con) {
dao.remove(con);
}
@Override
public Contato save(Contato con) {
Contato tmpCon;
if(con != null)
if(con.getId() != 0)
tmpCon = dao.merge(con);
tmpCon = dao.persist(con);
if (tmpCon.getId() != 0)
return tmpCon;
else
return contByName(tmpCon.getNome());
}
}
TesteContato.java (Junit4)
package mitologics.test;
import static junit.framework.Assert.*;
import mitologics.model.beans.Contato;
import mitologics.model.infrastructure.ContatoDecorator;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TesteContato {
private static ContatoDecorator repo;
private static ApplicationContext ctx;
@BeforeClass
public static void prepare() {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
repo = (ContatoDecorator) ctx.getBean("conRepo");
}
@Test
public void Teste() {
Contato mitologics = repo.contByName("Mitologics");
if (mitologics != null)
if (mitologics.getId() != 0)
repo.remove(mitologics);
Contato con1 = (Contato) ctx.getBean("conTeste");
con1 = repo.save(con1);
assertNotNull(con1);
System.out.println("Id do Contato gravado: " + con1.getId());
assertEquals(7, con1.getId());
}
}