Error in Hibernate

2

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

    
asked by anonymous 05.01.2016 / 02:20

3 answers

1

Apparently you're using version 3.2.4.

To use the AnnotationConfiguration#addAnnotatedClass() you need to import the jar file from hibernate-core into version 3.6.0 or higher.

    
05.01.2016 / 02:37
0

You should change the library settings, first because it has an error and second because it is very outdated. If you are using Maven, I recommend you take Hibernate-Core version 5.2.0. End of this link: MVNRepository - Hibernate Core

And from a certain version of Hibernate, the AnnotationConfiguration class is no longer used, which was moved to the Configuration class. Try setting up Hibernate sessions as follows:

public class HibernateUtil {

private static SessionFactory sessionFactory;

public static void configureSessionFactory() {
  try{
    Properties prop = new Properties();
    prop.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
    prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost...");
    prop.setProperty("hibernate.connection.username", "root");
    prop.setProperty("hibernate.connection.password", "123");
    prop.setProperty("dialect", "org.hibernate.dialect.OracleDialect");

    sessionFactory = new Configuration()
            .addPackage("pacote onde estao as entidades")
                .addProperties(prop)
                .addAnnotatedClass(Cliente.class)
                .buildSessionFactory();

    } catch (Throwable t) {
        throw new ExceptionInInitializerError(t);
    }
}
    
19.07.2016 / 00:00
0

This problem is simply a fact:

  • Session factory failed to map @Entity objects
  • Ie the factory, does not recognize these classes
  

cfg.addAnnotatedClass (Client.class);   This is not working.

Tip look for some method similar to that

cfg.afterPropertiesSet();
// ou algo
cfg.buildSessionFactory();

So the session factory will finish arming and you know you have mapped the Client

    
17.11.2016 / 02:20