1
I’m learning together with a tutorial to develop a petshop system and I’m getting the following hibernate error:
Hibernate: selectcliente0_.idClient as idClient2_2_, cliente0_.email as email3_2_, cliente0_.name as name4_2_, cliente0_.password as password5_2_, cliente0_.tipo as tip1_2_ from Client cliente0_ left Outer Join PFcliente0_1_ on cliente0_.idCliente=cliente0_1_.idCliente left Outer Join PJ cliente0_2_ on cliente0_.idCliente=cliente0_2_.idCliente Where cliente0_.email=? and cliente0_.password=? Error in authentication
Method authenticate (Clientemtd class):
public static Autenticavel autenticar(String usuario, String senha) {
Session session = HibernateUtil.getInstance();
Transaction tx = null;
Cliente p = null;
try {
Query q;
tx = session.beginTransaction();
q = session.createQuery("FROM Cliente as p where p.email=:usuario and p.senha=:senha");
q.setParameter("usuario", usuario);
q.setParameter("senha", senha);
List resultados = q.list();
if (resultados.size() > 0) {
p = (Cliente) resultados.get(0);
}
return p;
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
return p;
} finally {
session.close();
}
}
Authenticatable.java:
package br.com.diego.controle;
public interface Autenticavel {
public Autenticavel autenticar(String usuario, String senha);
public boolean existe(String usuario);
}
Testandohibernate.java:
public class TestandoHibernate {
public static void main(String[] args) {
GerarTabelas();
if (autenticar("[email protected]", "root") != null) {
System.out.println("Usuário autenticado");
} else {
System.out.println("Erro na autenticação");
}
try {
persistindo();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Erro" + ex.getMessage());
}
}
public static Autenticavel autenticar(String usuario, String senha) {
return new PF().autenticar(usuario, senha);
}
private static void persistindo() throws Exception {
PF pf = new PF();
pf.setNome("Diego Juarez");
pf.setEmail("[email protected]");
pf.setSenha("root");
DocReceita CPF = new CPF(" 122. 379. 567-58 ");
pf.setDocReceita(CPF);
CPF.setCliente(pf);
if (pf.existe(CPF) == true) {
throw new RuntimeException("<br><center><font face='verdana' color='red' size='2'><br />já existe usuário cadastrado com o CPF ou CNPJ informado</font></center><br>");
}
Endereco end = new Endereco();
end.setBairro("Centro");
end.setCep("28610175");
end.setCidade("Nova Friburgo");
end.setLograd("Pca Presidente Getulio Vargas 220");
end.setNumero("402");
end.setUf("RJ");
end.setCliente(pf);
Collection<Endereco> e = new ArrayList<Endereco>();
e.add(end);
pf.setEndereco(e);
Telefone t = new Telefone();
t.setCodArea(21);
t.setNumero("988336760");
t.setCliente(pf);
Collection<Telefone> tel = new ArrayList<Telefone>();
tel.add(t);
pf.setTelefone(tel);
Pet a = new Cachorro();
Date d = new Date(2010, 04, 26);
a.setDataNascimento(d);
a.setNome("Rex");
a.setRaca("Bulldog");
a.setSexo('M');
a.setObs("Peida muito!");
a.setCliente(pf);
Servico s = new Consulta();
s.setData(new Date());
s.setDescricao("Problema de peso");
s.setValor(20);
s.setPet(a);
Collection<Servico> serv = new ArrayList<Servico>();
serv.add(s);
a.setServico(serv);
Collection<Pet> peti = new ArrayList<Pet>();
peti.add(a);
pf.setPet(peti);
PF.salvar(pf);
}
}
Can anyone help me find the error? Thanks in advance!
Hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.url">jdbc:mysql://localhost:3306/petshop1</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">6452software</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="br.com.diego.controle.Cliente"/>
<mapping class="br.com.diego.controle.PF"/>
<mapping class="br.com.diego.controle.PJ"/>
<mapping class="br.com.diego.controle.DocReceita"/>
<mapping class="br.com.diego.controle.CPF"/>
<mapping class="br.com.diego.controle.CNPJ"/>
<mapping class="br.com.diego.controle.Endereco"/>
<mapping class="br.com.diego.controle.Pet"/>
<mapping class="br.com.diego.controle.Cachorro"/>
<mapping class="br.com.diego.controle.Gato"/>
<mapping class="br.com.diego.controle.Servico"/>
<mapping class="br.com.diego.controle.Banho"/>
<mapping class="br.com.diego.controle.Consulta"/>
<mapping class="br.com.diego.controle.Telefone"/>
<mapping class="br.com.diego.controle.Tosa"/>
<mapping class="br.com.diego.controle.Vacina"/>
</session-factory>
</hibernate-configuration>
Did you try running the query directly on Mysql to see if it really thinks the record? I would run this on mysql:
select cliente0_.idCliente as idClient2_2_, cliente0_.email as email3_2_, cliente0_.nome as nome4_2_, cliente0_.senha as senha5_2_, cliente0_.tipo as tipo1_2_ from Cliente cliente0_ left outer join PF cliente0_1_ on cliente0_.idCliente=cliente0_1_.idCliente left outer join PJ cliente0_2_ on cliente0_.idCliente=cliente0_2_.idCliente where cliente0_.email='[email protected]' and cliente0_.senha='root'
– sergiopereira
Post your persistence.xml, I have a slight suspicion that I know where the error is occurring.
– Weslley Tavares
do not possess persistence.xml only Hibernate.cfg.xml
– Diego Juarez