eclipse 休眠映射错误:MappingException:AnnotationConfiguration 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16913225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:39:45  来源:igfitidea点击:

Hibernate Mapping Error: MappingException: An AnnotationConfiguration instance

javaeclipsehibernateannotationsmapping

提问by Davidin073

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

我有一个 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)

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?

任何的想法?

回答by eternay

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:

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

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

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

然后使用类AnnotationConfiguration而不是 Configuration 来配置您的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.

然后您必须更正类的限定名称,正如@JB Nizet 在他的评论中建议的那样。

I think this will solve your problem.

我认为这将解决您的问题。

EDIT: sorry, I think you have the annotations library in your classpath, just use the AnnotationConfigurationclass instead of Configurationwhen you build your sessionFactory.

编辑:对不起,我想你在你的类路径中的注释库,只需使用AnnotationConfiguration类,而不是Configuration当你建立你的sessionFactory

回答by Davidin073

The problem was of libraries. This is the solutions:

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

enter image description here

在此处输入图片说明

A lot of thank′s to Eternay and JB Nizet.

非常感谢 Eternay 和 JB Nizet。