Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. This is my 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:

<?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!). 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. But when I run the application JVM gives me error: java.lang.NoClassDefFoundError: org/hibernate/cfg/Mappings in this line:

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

What is the problem? How should I fix it? And Thanks!

like image 936
HsnVahedi Avatar asked Dec 10 '22 17:12

HsnVahedi


1 Answers

The problem with libraries. You use Hibernate 5. So you don't need this in the 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. Because of, it is a transitive dependency.

You don't need it too, because of you don't use 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!

To configure Hibernate 5

 private static SessionFactory buildSessionFactory() {
    return new Configuration().configure().buildSessionFactory();
 }
like image 185
v.ladynev Avatar answered Feb 13 '23 03:02

v.ladynev