ExceptionInInitializerError in Hibernate

As part of the Hibernate practice, I have faced the Exception in thread "main" java.lang.ExceptionInInitializerError at the time of creating the SessionFactory. Here is the full stacktrace which I have captured from console.

INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
SessionFactory creation failed.org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.javatbrains.sample.User]
Exception in thread "main" java.lang.ExceptionInInitializerError
       at com.jtb.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
       at com.jtb.util.HibernateUtil.getSessionFactory(HibernateUtil.java:18)
       at com.jtb.test.DeleteUser.main(DeleteUser.java:11)


Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.javatbrains.sample.User]
       at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:229)
       at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.<init>(AnnotationMetadataSourceProcessorImpl.java:103)
       at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.<init>(MetadataBuildingProcess.java:147)
       at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:141)
       at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
       at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
       at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
       at com.jtb.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:10)
       ... 2 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.javatbrains.sample.User
       at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:217)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       at java.lang.Class.forName0(Native Method)
       at java.lang.Class.forName(Unknown Source)
       at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:226)
       ... 11 more

Solution:
In the above stacktrace two issues got included.
1. INFO HHH000424 ERROR. You just simply ignore this or you add the below property into your hibernate.cfg.xml file.
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>

2. System not able to identify the bean class which you have mapped in the hibernate.cfg.xml configuration file. Please pass the bean class with complete path like below.
<mapping class="com.hibernate.aiexample.model.User" />

Since I have placed the wrong package name in mapping tag, I have got this exception.

Popular posts from this blog

Hibernate auto increment with example

how to count the page views by using JSP

Multithreading in java with example

How to retrieve data from table by using JDBC with example

Prime, Fibonacci and Factorial number with example in java

How to insert images into database using JDBC?

How to sort list of objects in java with examples

String interview questions and answers

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Store file into table by using JDBC with example