简体   繁体   English

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

[英]getting error “java.lang.NoClassDefFoundError: org/hibernate/cfg/Mappings” in a simple maven hibernate project

I'm working on a project using maven, hibernate and mysql.我正在使用 maven、hibernate 和 mysql 开发一个项目。 This is my pom.xml:这是我的 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>

and here is my resources/hibernate.cfg.xml:这是我的资源/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>

I have installed mysql and created a database named $DATABASE_NAME but I haven't created any table in database (because I'm not expert in sql and I hope hibernate can do it for people like me!).我已经安装了 mysql 并创建了一个名为 $DATABASE_NAME 的数据库,但我没有在数据库中创建任何表(因为我不是 sql 专家,我希望 hibernate 可以为像我这样的人做!)。 This is the way I'm using hibernate:这是我使用休眠的方式:

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

I build this project by maven and it successfully builds.我通过 maven 构建了这个项目,它成功构建了。 But when I run the application JVM gives me error: java.lang.NoClassDefFoundError: org/hibernate/cfg/Mappings in this line:但是当我运行应用程序时,JVM 给了我错误:java.lang.NoClassDefFoundError: org/hibernate/cfg/Mappings 在这一行:

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

What is the problem?问题是什么? How should I fix it?我该如何解决? And Thanks!谢谢!

The problem with libraries.图书馆的问题。 You use Hibernate 5. So you don't need this in the pom.xml您使用 Hibernate 5。因此您不需要在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 uses hibernate-commons-annotations-5.0.1.Final.jar and you don't need to specify it. Hibernate 5 使用hibernate-commons-annotations-5.0.1.Final.jar并且您不需要指定它。 Because of, it is a transitive dependency.因为,它是一个传递依赖。

You don't need it too, because of you don't use JPA你也不需要它,因为你不使用JPA

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

There is not AnnotationConfiguration in Hibernate 4 and Hibernate 5. It is form Hibernate 3! Hibernate 4 和 Hibernate 5 中没有AnnotationConfiguration 。它是 Hibernate 3 的形式!

To configure Hibernate 5配置休眠 5

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

Try to replace this line:尝试替换此行:

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

With:和:

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

Because Deprecated .因为Deprecated Use the mappings and properties specified in the given application file.使用给定应用程序文件中指定的映射和属性。

For more info go to AnnotationConfiguration有关更多信息,请访问AnnotationConfiguration

If you are using Hibernate above version 3 like builds 4 or 5 , simply use Configuration instead of AnnotationConfiguration class because AnnotationConfiguration is removed after version 3.如果您使用版本 3 以上的Hibernate像构建4 或 5 ,只需使用Configuration而不是AnnotationConfiguration类,因为AnnotationConfiguration在版本 3 之后被删除。

Lets have a snapshot example with 11 dependencies and configuration to use Annotation based mapping configuration.让我们有一个带有 11 个依赖项和配置的快照示例,以使用基于注解的映射配置。

1.  antlr-2.7.7.jar
2.  classmate-1.3.0.jar
3.  dom4j-1.6.1.jar
4.  geronimo-jta_1.1_spec-1.1.1.jar
5.  hibernate-commons-annotations-5.0.1.Final.jar
6.  hibernate-core-5.1.10.Final.jar
7.  hibernate-jpa-2.1-api-1.0.0.Final.jar
8.  jandex-2.0.3.Final.jar
9.  javassist-3.20.0-GA.jar
10. jboss-logging-3.3.0.Final.jar
11. mysql-connector-java-5.1.38-bin.jar

Configuration file is as follows:配置文件如下:

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver.class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://zampylob:3306/hibernatedb</property>
    <property name="hibernate.connection.username">hiberuser</property>
    <property name="hibernate.connection.password">XXXXXXXXXX</property>
    <property name="hibernate.connection.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.show_sql">true</property>
    <property name="hibernate.connection.format_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">create</property>

    <mapping class="com.whodesire.model.Student" />
</session-factory>
</hibernate-configuration>

Student POJO class is:学生POJO类是:

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Student {

    @Id
    private Integer rollNo;

    private String firstName;
    private String middleName;
    private String lastName;
    private Date dateOfBirth;
    private String gender;

    public Integer getRollNo() {
        return rollNo;
    }

    public void setRollNo(Integer rollNo) {
        this.rollNo = rollNo;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
}

and finally the Main Class is :最后主类是:

SessionFactory factory = new Configuration().configure("resources/hibernate.cfg.xml").buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();

Student student = new Student();
student.setRollNo(1002);
student.setFirstName("Maaherin");
student.setMiddleName("Zubbair");
student.setLastName("Gulfam");
student.setDateOfBirth(new Date());
student.setGender("Male");

session.persist(student);
transaction.commit();
session.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NoClassDefFoundError:org / hibernate / cfg / AnnotationConfiguration - java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration MultiException堆栈1 of 2 java.lang.NoClassDefFoundError:org / hibernate / cfg / Configuration - MultiException stack 1 of 2 java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration java.lang.NoClassDefFoundError: org/hibernate/cfg/配置 - java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration Maven java.lang.NoClassDefFoundError: org/hibernate/query/Query - Maven java.lang.NoClassDefFoundError: org/hibernate/query/Query java.lang.NoClassDefFoundError:org / hibernate / MappingException - java.lang.NoClassDefFoundError: org/hibernate/MappingException 线程“ main”中的异常java.lang.NoClassDefFoundError:org / hibernate / cfg / Configuration - Exception in thread “main” java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration java.lang.NoClassDefFoundError:无法初始化类org.hibernate.cfg.AnnotationConfiguration - java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.cfg.AnnotationConfiguration 注入自动连接的依赖项失败; 嵌套异常是java.lang.NoClassDefFoundError:org / hibernate / cfg / Configuration - Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration 在jboss 6.0.0 AS上将Hibernate 3.6升级到hibernate 4.3.6,获取java.lang.NoClassDefFoundError:org / hibernate / classic / Session - Upgrade Hibernate 3.6 to hibernate 4.3.6 on jboss 6.0.0 AS getting java.lang.NoClassDefFoundError: org/hibernate/classic/Session 在 JavaEE Maven 项目中使用 Hibernate 时出现“java.lang.NoClassDefFoundError: javax/persistence/criteria/Selection”错误 - "java.lang.NoClassDefFoundError: javax/persistence/criteria/Selection" error while using Hibernate in a JavaEE Maven project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM