Добрый день!
Пытаюсь познакомиться с
hibernate, для этого собрал простой
gradle проект:
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"
}
В ресурсах лежит файл
hibernate.cfg.xml:
<?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&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>
Класс
UserModel выглядит так:
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;
}
}
Главный класс
App приложения:
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();
}
}
Во время выполнения получаю ошибку:
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 .....
Может быть я забыл какую то либо подключить? Кто знает, подскажите :)
Спасибо.