I'm learning next to a tutorial to develop a petshop system and am getting the following Hibernate error:
Hibernate: select client0_.idClient idClient2_2_, client0_.email as email3_2_, client0_.name the_name_2_, client0_.password the password5_2_, client0_.type as type1_2_ from Client client0_ left outer join PF client0_1_ on client0_.idClient = client0_1_. idClient left outer join PJ client0_2_ on client0_.idClient = client0_2_.idClient where client0_.email =? and cliente0_.senha =? Authentication Error
Authenticate method (from the ClientMTD 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();
}
}
Authentication.java:
package br.com.diego.controle;
public interface Autenticavel {
public Autenticavel autenticar(String usuario, String senha);
public boolean existe(String usuario);
}
TestingHibernate.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? Thank you 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>