3

I'm a newbie at hibernate and I'm trying to test my entities (persistance) and this error keep showing

Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/spi/ClassLoaderService
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:80)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at Pers.PersistTest.main(PersistTest.java:20)
Caused by: java.lang.ClassNotFoundException:org.hibernate.boot.registry.classloading.spi.ClassLoaderService
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more

This is my entity

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
 public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int idUser;
...
private boolean isAdmin;

(with getters and setters and constructor of course)

and this is the test class

public class PersistTest {
public static void main(String[] args) { 

    EntityManagerFactory emf=Persistence.createEntityManagerFactory("PU");
    EntityManager em = emf.createEntityManager();
    Session s = new AnnotationConfiguration()  
            .configure().buildSessionFactory().openSession();
    Transaction t = s.beginTransaction();

    User c = new User();
    c.setIdUser(1);
    ...
    c.setAdmin(true);

    em.persist(c);
    t.commit();
    s.close();
    System.out.print("success");
}

and this is persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">
  <persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
           <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/GBO1" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />
  </properties>

and this is my hibernate dependencies

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
<version>3.3.2.Beta1</version>
</dependency>

and thanks in advance :)

1
  • whats with all of the Hibernate-specific Session/Transaction nonsense? JPA does all of that. NoClassDefFound means you either don't have that class or one of its dependents present. And bearing in mind that "hibernate-annotations" hasn't been released in years, perhaps you don't need that at all. I'm sure the Hibernate docs should tell you Commented Apr 5, 2016 at 15:51

1 Answer 1

5
  1. You mix a JPA approach (using EntityMananger) and a Hibernate Session approach.
  2. You use Hibernate 3 libraries (3.5.6-Final) and Hibernate 5 hibernate-entitymanager (5.0.6.Final).

You can use to getting started

Hibernate Getting Started Guide

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.