I'm trying to make a small interaction with the MySQL database but when I run the test class the following error appears:
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.cfg.Mappings (Ljava / util / Map; Ljava / util / Map; Ljava / util / Map; Ljava / / Ljava / util / Map / Ljava / util / List Ljava / util / List Lorg / hibernate / cfg / NamingStrategy; at org.hibernate.cfg.ExtendedMappings. (ExtendedMappings.java:43) at org.hibernate.cfg.AnnotationConfiguration.createExtendedMappings (AnnotationConfiguration.java:125) at org.hibernate.cfg.AnnotationConfiguration.addAnnotatedClass (AnnotationConfiguration.java:94) at test.main (test.java:13)
Follow the connection codes:
package com.cavalcanteTech.dao;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/* @author Emanuel Cavalcante*/
public class HibernateUtil {
public static final SessionFactory session = buildSession();
private static SessionFactory buildSession() {
try {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
return cfg.buildSessionFactory();
} catch (Throwable b) {
System.out.println("Não deu \n" + b);
throw new ExceptionInInitializerError();
}
}
public static SessionFactory getSessionFactory() {
return session;
}
}
Follow my template:
package com.cavalcanteTech.modelo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/* @author Emanuel Cavalcante*/
@Entity
public class Cliente {
@Id
@GeneratedValue
private long id;
@Column(name = "nome")
private String nome;
@Column(name = "endereco")
private String endereco;
@Column(name = "telefone")
private String telefone;
@Column(name = "dataDePagamento")
private String dataDePagamento;
//gets e sets
}
My class CustomerID:
package com.cavalcanteTech.dao;
import org.hibernate.Session;
import com.cavalcanteTech.modelo.Cliente;
public class ClienteDao {
private Session session;
public void Inserir(Cliente cliente) {
session = HibernateUtil.getSessionFactory().openSession();
try {
System.out.println("Passou1");
session.beginTransaction();
System.out.println("Passou1");
session.save(cliente);
System.out.println("Passou1");
session.getTransaction().commit();
} finally {
session.close();
}
}
}
Now, my class that runs the test:
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import com.cavalcanteTech.dao.ClienteDao;
import com.cavalcanteTech.dao.HibernateUtil;
import com.cavalcanteTech.modelo.Cliente;
public class teste {
public static void main(String[] args) {
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass(Cliente.class);
Cliente cliente = new Cliente();
ClienteDao dao = new ClienteDao();
cliente.setDataDePagamento("3213");
cliente.setEndereco("312321");
cliente.setNome("432432");
cliente.setTelefone("321312");
cliente.setId(1);
System.out.println(cliente.toString());
dao.Inserir(cliente);
}
}
My libraries used in the project:
Andmyhibernate.cfg.xml:
I've looked for this error in several places, but until now I could not solve it, if anyone knows what I'm doing wrong, thank you right away. : D