0

I create a small java class for hibernate but its gives an exception.I'm using Hibernate 5.0.2 and java 8 on ubuntu 14.04. My classes and Hibernate configuration files is as follows.Hope someone can help me to find a solution to this.

This is the exception that occurs.

java.lang.NullPointerException
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.getResources(ClassLoaderServiceImpl.java:173)
at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:348)
at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393)
at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:324)
at org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(IntegratorServiceImpl.java:40)
at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:212)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
at com.gim.gimpro.HibernateTest.setUp(HibernateTest.java:43)
at com.gim.gimpro.HibernateTest.main(HibernateTest.java:18)

Here is my code

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.gim.hibe.UserDetails;


public class HibernateTest {

    private SessionFactory sessionFactory;

    public static  void main(String[] args){
        HibernateTest hibernateTest=new HibernateTest();

        try {
            hibernateTest.setUp();
            hibernateTest.save();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    void save(){
        UserDetails userDetails=new UserDetails();
        userDetails.setUserId(1);
        userDetails.setUserName("Gihan");
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save( userDetails );
        session.getTransaction().commit();
        session.close();
    }


    protected void setUp() throws Exception {

        sessionFactory = new Configuration().configure() .buildSessionFactory();
    }
 }

UserDetails class

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "USER_DETAILS")
public class UserDetails {

@Id
private int userId;
private String userName;


public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

}

Hibernate Configuration

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 5.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<property name="hibernate.hbm2ddl.auto">create</property>
		<mapping class="com.gim.hibe.UserDetails"/>
	</session-factory>
</hibernate-configuration>

1
  • "Hibernate Configuration DTD 5.0" FYI.. that DTD doesn't exist and never did, that may be part of the problem Commented Nov 6, 2020 at 22:40

1 Answer 1

1

This Exception can occur because of below 2 reasons:

  1. Please check if package com.gim.hibe for UserDetails class is correct.

  2. Also see if Hibernate libraries are properly included in the classpath.

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

5 Comments

I checked that two things. But that's also correct. @shankarsh15
check also if hibernate.cfg.xml is at the root of your classpath.
Yes hibernate.cfg.xml is also is in root. @shankarsh15
No i'm using eclipse IDE(mars) .
add your Hibernate libraries as User libraries and not system libraries which I think is the issue.

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.