Answer the question
In order to leave comments, you need to log in
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"
}
<?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>
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;
}
}
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 .....
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question