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

INFO: Configuration resource: /hibernate.cfg.xml
Initial SessionFactory creation 
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(
	at com.mkyong.persistence.HibernateUtil.<clinit>(
	at com.mkyong.common.App.main(
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.mkyong.common.Stock"/>
	at org.hibernate.cfg.Configuration.parseMappingElement(
	at org.hibernate.cfg.Configuration.parseSessionFactory(
	at org.hibernate.cfg.Configuration.doConfigure(
	at org.hibernate.cfg.Configuration.doConfigure(
	at org.hibernate.cfg.Configuration.configure(
	at org.hibernate.cfg.Configuration.configure(
	at com.mkyong.persistence.HibernateUtil.buildSessionFactory(
	... 2 more


1. Download the Hibernate annotation library

You can download the library from Hibernate official website


Add the dependency in Maven’s pom.xml

        <!-- Hibernate annotation -->

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

      <id>JBoss repository</id>

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();

A full example of “” 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