Main Tutorials

Hibernate Error – An AnnotationConfiguration instance is required to use

The Hibernate annotation is required “AnnotationConfiguration” instead of normal “Configuration()” to build the session factory.


INFO: Configuration resource: /hibernate.cfg.xml
Initial SessionFactory creation failed.org.hibernate.MappingException: 
An AnnotationConfiguration instance is required to use <mapping class="com.mkyong.common.Stock"/>
Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.mkyong.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
	at com.mkyong.persistence.HibernateUtil.<clinit>(HibernateUtil.java:8)
	at com.mkyong.common.App.main(App.java:11)
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.mkyong.common.Stock"/>
	at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1600)
	at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
	at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
	at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
	at com.mkyong.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
	... 2 more

Solution

1. Download the Hibernate annotation library

You can download the library from Hibernate official website

Or

Add the dependency in Maven’s pom.xml


        <!-- Hibernate annotation -->
	<dependency>
		<groupId>hibernate-annotations</groupId>
		<artifactId>hibernate-annotations</artifactId>
		<version>3.3.0.GA</version>
	</dependency>

P.S You may need to include the JBoss repository in order to download the Hibernate annotation library.


<repositories>
    <repository>
      <id>JBoss repository</id>
      <url>http://repository.jboss.com/maven2/</url>
    </repository>
  </repositories>

2. Use AnnotationConfiguration to build session factory

Normal Hibernate XML file mapping is using Configuration()


          return new Configuration().configure().buildSessionFactory();  

For Hibernate annotation, you have to change it to “AnnotationConfiguration”


          return new AnnotationConfiguration().configure().buildSessionFactory();  
HibernateUtil.java

A full example of “HibernateUtil.java” of using “AnnotationConfiguration” for Hibernate annotation applacation.


package com.mkyong.persistence;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();
            
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public static void shutdown() {
    	// Close caches and connection pools
    	getSessionFactory().close();
    }

}

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
26 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Nitin
8 years ago

SessionFactory factory= new AnnotationConfiguration().configure().buildSessionFactory();

and it worked for me

amoolya
8 years ago

Hi

Im getting this exception only when i use xml based and annotation based hibernet together. that means, in config file, i have mapping for hbm as well as mapping for the classes for annotated class.

but seperately they work as expected. Please suggest me if there anything im missing

Soumi
10 years ago

Thanks.. It helped!

Diva
10 years ago

you can find some more details in the below link,”http://javadomain.in/solved-an-annotationconfiguration-instance-is-required-to-use-mapping-class/”

Anup
10 years ago

After changing Configuration to AnnotationConfiguration, I am facing below exception-
Exception in thread “main” java.lang.NoSuchFieldError: sqlResultSetMappings

Please let me know what needs to be done?

Thanks,
Anup

fashion
10 years ago

After study a few of the blog posts on your website now, and I really like your means of blogging. I bookmarked it to my bookmark website checklist and will be checking again soon. Pls check out my website online as nicely and let me know what you think.

Faras
10 years ago

Awesome website.. Superb content. 🙂

BrightSide
10 years ago

Mr Mkyong, Your tutorials rock, “Thank you” is not enough, I was wondering If you could take me up on my offer of dinner 😉

Febrie Subhan
11 years ago

i think can add commit();

Saurabh Naik
11 years ago

Hi
I am also facing same problem.
When i use AnnotationConfiguration it says ‘type AnnotationConfiguration is deprecated’
& when i switch to use configuration it gives error
org.hibernate.MappingException: An AnnotationConfiguration instance is required to use mapping class

Saurabh Naik
11 years ago

Hi
I am also facing same problem.
When i use AnnotationConfiguration it says ‘type AnnotationConfiguration is deprecated’
& when i switch to use configuration it gives error
org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="”>
please suggest if there is any alternate.

Skyhan
12 years ago

IS the annotations library compatible with Hibernate 3.2.5?

Which version are you guys using?

Hassan
12 years ago

thanks a lot .

Thiru Neela
13 years ago

It solved my problem, the following is syntax used

//sessionFactory = new Configuration().configure().buildSessionFactory();
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

H3nrique
12 years ago
Reply to  Thiru Neela

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

// sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
sessionFactory = new Configuration().configure().buildSessionFactory();

Thiru Neela
13 years ago

Very Useful, it solved my problem as well.

ezjava
13 years ago

Very useful post ! It helped a lot. Tks

hi
13 years ago

Hi. AnnotationConfiguration is deprecated now. How to use it with Configyration?

Reza
13 years ago

Thank you! the second solution solved my problem…

George W. Bush
13 years ago

this solved my immediate problem, thx for posting it!

mini998
13 years ago

Many thanks , u solved my problem

Eder
13 years ago
Reply to  mini998

Hi, how can you solve this problem.??

sreenu
10 years ago
Reply to  Eder

SessionFactory sf=new AnnotationConfiguration().configure().buildSessionFactory();

i am getting xception in thread “main” org.hibernate.MappingException: An AnnotationConfiguration instance is required to use

Yolande
11 years ago
Reply to  mkyong

I still got that error and on top of that AnnotationConfiguration is deprecated.

What do you use?

Saurabh Naik
10 years ago
Reply to  Yolande

I am also facing similar problem