Z
Z
Zaur Abdulgalimov2016-08-08 11:54:21
Java
Zaur Abdulgalimov, 2016-08-08 11:54:21

How to build a hibernate project in Gradle?

Good afternoon!
I'm trying to get acquainted with hibernate , for this I put together a simple gradle project:

group 'hb'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = 1.8
mainClassName = 'ru.fp.hb.App'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.hibernate:hibernate:3.5.4-Final"
    compile "org.hibernate:hibernate-core:5.2.1.Final"
    compile "org.hibernate:hibernate-annotations:3.5.6-Final"
    compile "org.hibernate:hibernate-commons-annotations:3.2.0.Final"
    compile "org.hibernate:hibernate-gradle-plugin:5.2.1.Final"
    compile "org.hibernate:hibernate-tools:5.0.1.Final"
    compile "org.hibernate:hibernate-entitymanager:5.1.0.Final"
    compile "mysql:mysql-connector-java:5.1.31"
}

The resources contain the hibernate.cfg.xml file :
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "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/hb_test?useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="connection.password">123</property>
        <mapping class="ru.fp.hb.model.UserModel"/>
    </session-factory>
</hibernate-configuration>

The UserModel class looks like this:
package ru.fp.hb.model;

import javax.persistence.*;

@Entity
@Table(name="users")
public class UserModel extends Model
{
    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "name", length=12)
    private String name;
    public UserModel()
    {

    }

    public Long getId() {return id;}
    public void setId(Long value)
    {
        this.id = value;
    }

    public String getName() {return name;}
    public void setName(String value)
    {
        this.name = value;
    }
}

The main App class of the application:
package ru.fp.hb;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import ru.fp.hb.model.UserModel;

import java.io.File;

/**
 * Created by sam on 22.07.16.
 */
public class App
{
    public static void main(String[] args)
    {
        System.out.println("new main");
        //
        AnnotationConfiguration aconf = new AnnotationConfiguration();
        aconf.addAnnotatedClass(UserModel.class);
        Configuration conf = aconf.configure(new File("hibernate.cgf.xml"));
        SessionFactory sessionFactory = conf.buildSessionFactory();
        //
        UserModel userModel = new UserModel();
        userModel.setName("Serious Sam");
        //
        Session session = sessionFactory.openSession();
        session.saveOrUpdate(userModel);
        session.getTransaction().commit();
        session.close();
    }
}

During execution I get an error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/cfg/Mappings
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
  at .....

Maybe I forgot to connect one? Who knows, tell me :)
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kornachev, 2016-08-08
@zelan

compile "org.hibernate:hibernate: 3.5.4-Final "
compile "org.hibernate:hibernate-core: 5.2.1.Final "
and in general I look at your versions in discord. They there from version to version remake everything very strongly, they can move / delete classes. You need to use all the dependencies of one version.
in 3.5.4 there may not be such a class, change the version to 5.2.1.
In general, it is better for a beginner to find the latest examples and repeat them in full, or use the official documentation for the current version.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question