Error in Hibernate

Asked

Viewed 523 times

2

I am trying to make a small interaction with the Mysql database but when I run the test class appears the following error:

Exception in thread "main" java.lang.Nosuchmethoderror: org.hibernate.cfg.Mappings.(Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/List;Ljava/util/List;Lorg/hibernate/cfg/NamingStrategy;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;) V 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;
    }
}

Just follow my lead:

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, my clientele:

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 running 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: bibliotecas

And my Ibernate.cfg.xml: hibernate.cfg.xml

I have looked for this error in several places, but so far I could not solve, if someone knows what I am doing wrong, I thank you already. : D

3 answers

1


  • Then I remove the hibernate3-3.2.4.sp1.jar and add the Hibernate-core-4.3.8.Final. jar, theoretically it’s for all right?

  • Replaces and appears this error: Caused by: java.lang.Classnotfoundexception: org.hibernate.Annotations.common.Reflection.Reflectionmanager at java.net.Urlclassloader$1.run(Unknown Source) at java.net.Urlclassloader$1.run(Unknown Source) at java.security.Accesscontroller.doPrivileged(Native Method) at java.net.Urlclassloader.findClass(Unknown Source) at java.lang.Classloader.loadClass(Unknown Source) at sun.misc.Launcher$Appclassloader.loadClass(Unknown Source) at java.lang.Classloader.loadClass(Unknown Source) ... 1 more

0

This problem is simply a fact:

  • Factory, failed to map the @Entity objects
  • In other words, Factory does not recognize these classes

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

Hint look for a method similar to that

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

So the Factory Session will finish arming and knows that it has mapped the object Client

0

You should change the library settings, first because you are in error and second because it is very outdated. If you are using Maven, I recommend that you take version 5.2.0.Final of Hibernate-Core at this link: Mvnrepository - Hibernate Core

And from a certain version of Hibernate, the Annotationconfiguration class is no longer used, which has been moved to the Configuration class. Try to configure the Hibernate Sesssions according to the code below:

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);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.