Hibernate exception on startup

hibernate exceptions
spring boot jpa exception handling
hibernate exception hierarchy
hibernate session exception
hibernate stale object exception
how to handle unique constraint exception in spring boot
does hibernate support polymorphism
how to create custom exception in spring boot

I have just started learning Hibernate. For that I have written a simple Java Program. But when I try to execute the program I am getting following exception.

Apr 30, 2014 1:54:18 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Apr 30, 2014 1:54:18 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.hibernate.cfg.Configuration.reset(Configuration.java:324)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:289)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:293)
    at com.hiber.main.Main.main(Main.java:19)
Caused by: java.lang.NullPointerException
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
    at org.hibernate.cfg.Environment.<clinit>(Environment.java:221)
    ... 4 more

Following is the program:

package com.hiber.main;

import javax.naming.NamingException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class Main {

    public static void main(String[] args) throws NamingException {
        //SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(new StandardServiceRegistryBuilder().build());

        ServiceRegistry serviceRegistry;
        SessionFactory sessionFactory;

        Configuration configuration = new Configuration();
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        Session sess = sessionFactory.getCurrentSession();

        Test1 t = new Test1();




Following is the cfg xml:

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root1234</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.default_schema">test</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>

I have included all the dependencies. Hopefully I haven't missed anything. Can't add image currently.

Please anyone have a look at the code and spare a thought on what might be going wrong here. Thanks in advance.

Go through this tutorial here

its about step by step Hibernate Mapping using Annotation.

Common Hibernate Exceptions, Have a look at some common errors in Hibernate and how to solve them. new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2: These exceptions mostly extend from HibernateException. I'm getting a nasty exception on startup. Probably configuration but I'm running out of ideas. I'm using JBoss 6 M3, hibernate3, hibernate-validator-4.1.0.Final, hibernate-jpa-2.0-api-1.0.0.Final.

I think you forgot the mapping class at hibernate cfg.

add mapping class before tag


at your hibernate cfg and don't forget to add the annotation at your class.

and the mapping looks like this:

<mapping class="Curriculums"/>
<mapping class="CurriculumsHasStudentId"/>
<mapping class="Students"/>

your java class with the annotation looks like this:

@Table(name = "students")
public class Students implements Serializable{

@Column(name="studentsNrp", unique=true)
private String studentsNrp;

@Column(name = "studentsName")
private String studentsName;

@Column(name = "studentsAddress")
private String studentsAddress;

@Column(name = "studentsCity")
private String studentsCity;

@Column(name = "studentsPhone")
private String studentsPhone;

Hibernate Exception, Hibernate throws a LazyInitializationException if you try to access a not As you can see, I use two independent EntityManagers and start a  Have the hibernate.cfg.xml in the classpath and try with the code like this Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Have the hibernate.cfg.xml in the classpath and try with the code like this

Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

For example if you use maven, it will be in location \src\main\resources\hibernate.cfg.xml


Check if you have the right dependencies (jars). I have these dependencies in my maven project

Hibernate Exception Handling Example, In this tutorial, we will discuss the Hibernate exceptions that exist in the code while working with the Hibernate framework. When this happens Hibernate throws an exception. The exception is difficult to translate and read from the log, because it has nested exceptions and consists of a lot of text: like I said, very difficult to understand. I want to know if it's possible to handle exceptions on entity level, throwing maybe a customized exception.

1 Remove the dependencies in your project. 2 Add the right dependencies (jars).

How to prevent OptimisticLockException with Hibernate versionless , that column name. There are examples of the role of the org.hibernate.boot.​model.naming. Setting it on the java.time classes throws the following exception:. Windows 10’s Fast Startup (called Fast Boot in Windows 8) works similarly to the hybrid sleep mode of previous versions of Windows. By saving the operating system state to a hibernation file, it can make your computer boot up even faster, saving valuable seconds every time you turn your machine on.

5 Common Hibernate Exceptions and How to Fix Them, PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception. The LazyInitializationException is undoubtedly one of the most common exceptions you can get when using Hibernate. This article is going to summarize the best and the worst ways of handling lazy associations. Fetching 101 With JPA, not only you can fetch entities from the database, but you can also fetch entity associations as well.

Hibernate ORM 5.4.15.Final User Guide, PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory at io.quarkus.hibernate.orm.runtime.boot. Hi, I am having a similar issue. here are the versions I am using: Hibernate 3.2.1 Oracle 10g ojdbc14 connection pool: c3p0-0.9 stack trace: 15:57:36,975 INFO C3P0ConnectionProvider:54 - autocommit mode: false

VMSS instance failes to connect to DB on first startup, How to implement exception handling with Spring Boot for RESTful .com/spring​-boot-crud-rest-service-with-jpa-hibernate, we set up a simple  If it's pointing to the H2 database, make sure to take a copy of the home directory and then start the Confluence with empty home directory, this time it would prompt to enter the Database details and make sure to add the PostgreSQL database details.

  • You say cfg xml but in your code reference it as hibernate.cfg.xml. Have you unzipped your generated JAR and checked the file is indeed where your code says it is?
  • Yes I added the mapping class but still same issue.
  • have you added the annotation on your java class?
  • I am running application as Eclipse project. I have tried with the above code too but still same exception. Plus to not miss the classpath I have copied cfg file under SRC folder. I hope this should be enough to have it in classpath ?
  • If you keep inside src folder, try with configuration.configure("/hibernate.cfg.xml");
  • Other option try with the full complete path including the drive. For example C:/Users/jjayarman/Google Drive/Java Trials/hibernate_434/src/main/resources/hibernate.cfg.xml
  • Configuration configuration = new Configuration().configure(new File("D:\\gaurav\\softwares\\spring_install_suite\\ws\\Hiber\\src\\hibernate.cfg.xml")); this failed as well and gave same exception.
  • Ok what are the libraries (jars) you have in the classpath ?