在一个简单的Maven休眠项目中得到错误“ java.lang.NoClassDefFoundError:org / hibernate / cfg / Mappings”

瓦希迪

我正在使用maven,hibernate和mysql进行项目。这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.heroku.realstate</groupId>
    <artifactId>realstate-database</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>ejb3-persistence</artifactId>
            <version>1.0.2.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-tools</artifactId>
            <version>4.3.2.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.2.0.Final</version>
        </dependency>

    </dependencies>

</project>

这是我的资源/ hibernate.cfg.xml:

<?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="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/$DATABASE_NAME
        </property>
        <property name="hibernate.connection.password">
            $PASSWORD
        </property>
        <property name="hibernate.connection.username">
            @USERNAME
        </property>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="show_sql">
            true
        </property>
        <property name="hbm2ddl.auto">
            create
        </property>

        <mapping class="com.heroku.realstate.database.user.UserEntity"/>
        <mapping class="com.heroku.realstate.database.user.ClientEntity"/>
        <mapping class="com.heroku.realstate.database.user.BotEntity"/>
        <mapping class="com.heroku.realstate.database.sms.SmsEntity"/>

    </session-factory>
</hibernate-configuration>

我已经安装了mysql并创建了一个名为$ DATABASE_NAME的数据库,但是我没有在数据库中创建任何表(因为我不是sql方面的专家,我希望休眠可以为像我这样的人做!)。这就是我使用休眠方式:

class HibernateUtils {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        // Create the SessionFactory from hibernate.cfg.xml
        return new AnnotationConfiguration().configure(new File("hibernate.cfg.xml")).buildSessionFactory();
    }

    static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

我通过maven构建了这个项目,并且构建成功。但是,当我运行应用程序JVM时,出现以下错误:java.lang.NoClassDefFoundError:org / hibernate / cfg / Mappings在此行:

return new AnnotationConfiguration().configure(new File("hibernate.cgf.xml")).buildSessionFactory();

问题是什么?我该如何解决?谢谢!

拉德涅夫

库的问题。您使用的是Hibernate5。因此,您不需要pom.xml

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>ejb3-persistence</artifactId>
            <version>1.0.2.GA</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.2.0.Final</version>
        </dependency>

Hibernate 5可以使用hibernate-commons-annotations-5.0.1.Final.jar,您无需指定它。因此,它是一个传递依赖。

您也不需要它,因为您不使用 JPA

   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.1.0.Final</version>
   </dependency>

AnnotationConfigurationHibernate 4和Hibernate 5中没有。它是Hibernate 3的形式!不知道从哪里得到的。

配置Hibernate 5

 private static SessionFactory buildSessionFactory() {
    return new Configuration().configure().buildSessionFactory();
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章