'Hibernate Mapping Error: MappingException: An AnnotationConfiguration instance

I have a project with Hibernate.I tried to read the configuration file hibernate.cfg and got this error:

 Error creando una factoria de session.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com/david/Categoria"/>
jun 04, 2013 10:19:26 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: El Servlet.service() para el servlet [ControladorLibros] en el contexto con ruta [/App01HTML] lanzó la excepción [La ejecución del Servlet lanzó una excepción] con causa raíz
org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com/david/Categoria"/>
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1524)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1479)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1338)
    at com.david.HibernateHelper.buildSessionFactory(HibernateHelper.java:13)

my configuration file is so simple:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/arquitecturajava</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.pool_size">5</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        <mapping class="com/david/Categoria"></mapping>
        <mapping class="com/david/Libro"></mapping>
    </session-factory>
</hibernate-configuration>

don´t accept mapping class lines.

I create de configurations similar to this code:

private static SessionFactory buildSessionFactory() 
    {
        try
        {

            return new Configuration().configure().buildSessionFactory(); 
        }
        catch (Throwable ex) 
        {
            System.err.println("Error creando una factoria de session." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

and my project libraries are:

enter image description here

Any idea?



Solution 1:[1]

I think you need the Hibernate Annotation library in your classpath. You can add it manually, downloading it from Hibernate site, or you can add this Maven dependency:

<dependency>
    <groupId>hibernate-annotations</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.3.0.GA</version>
</dependency>

And then use the class AnnotationConfiguration instead of Configuration to configure your sessionFactory.

return new AnnotationConfiguration().configure().buildSessionFactory();

Then you have to correct the qualified name of the class, as @JB Nizet suggested you in his comment.

I think this will solve your problem.

EDIT: sorry, I think you have the annotations library in your classpath, just use the AnnotationConfiguration class instead of Configuration when you build your sessionFactory.

Solution 2:[2]

The problem was of libraries. This is the solutions:

enter image description here

A lot of thankĀ“s to Eternay and JB Nizet.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eternay
Solution 2 Davidin073