Hibernate Mapping错误:MappingException:AnnotationConfiguration实例

时间:2013-06-04 08:37:10

标签: java eclipse hibernate annotations mapping

我有一个Hibernate项目。我试着读取配置文件hibernate.cfg并得到了这个错误:

 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)

我的配置文件很简单:

<?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>

不接受映射类行。

我创建类似于此代码的de配置:

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

我的项目库是:

enter image description here

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我认为您需要在类路径中使用Hibernate Annotation库。您可以手动添加它,从Hibernate站点下载它,或者您可以添加此Maven依赖项:

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

然后使用课程AnnotationConfiguration代替配置来配置您的sessionFactory

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

然后你必须更正班级的合格名称,正如@JB Nizet在评论中建议的那样。

我认为这可以解决您的问题。

编辑:抱歉,我认为您的类路径中有注释库,只需在构建AnnotationConfiguration时使用Configuration类而不是sessionFactory

答案 1 :(得分:0)

问题出在图书馆。 这是解决方案:

enter image description here

非常感谢Eternay和JB Nizet。