eclipse HibernateMap错误:Map异常错误:一个AnnotationConfiguration示例

hgqdbh6s  于 2022-11-04  发布在  Eclipse
关注(0)|答案(2)|浏览(100)

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

不接受Map类行。
我创建了类似于以下代码的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);
        }
    }

我的项目库是:

你知道吗?

bsxbgnwa

bsxbgnwa1#

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

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

然后使用类AnnotationConfiguration而不是Configuration来配置sessionFactory

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

然后,您必须更正类的限定名,就像@ JBNizet在他的评论中建议的那样。
我想这会解决你的问题。
编辑:对不起,我认为你的类路径中有注解库,当你构建sessionFactory时,只需要使用AnnotationConfiguration类而不是Configuration

kh212irz

kh212irz2#

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

非常感谢Eternay和JB Nizet。

相关问题