Hibernate – The type AnnotationConfiguration is deprecated

Problem

Working with Hibernate 3.6, noticed the previous “org.hibernate.cfg.AnnotationConfiguration“, is marked as “deprecated“.

Code snippets …


import org.hibernate.cfg.AnnotationConfiguration;
//...
private static SessionFactory buildSessionFactory() {
	try {

		return new AnnotationConfiguration().configure().buildSessionFactory();
			
	} catch (Throwable ex) {
	
		System.err.println("Initial SessionFactory creation failed." + ex);
		throw new ExceptionInInitializerError(ex);
	}
}

The code is still working, just keep displaying the deprecated warning message, is there any replacement for “AnnotationConfiguration” ?

Solution

In Hibernate 3.6, “org.hibernate.cfg.AnnotationConfiguration” is deprecated, and all its functionality has been moved to “org.hibernate.cfg.Configuration“.

So , you can safely replace your “AnnotationConfiguration” with “Configuration” class.

Code snippets …


import org.hibernate.cfg.Configuration;
//...
private static SessionFactory buildSessionFactory() {
	try {

		return new Configuration().configure().buildSessionFactory();
			
	} catch (Throwable ex) {
	
		System.err.println("Initial SessionFactory creation failed." + ex);
		throw new ExceptionInInitializerError(ex);
	}
}

References

  1. http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/cfg/AnnotationConfiguration.html
  2. http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/cfg/Configuration.html

About the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
M|ChaVeZ
Guest
M|ChaVeZ

What happend with Hibernate 5??? where is the annotationConfiguration class??

Yonatan Wilkof
Guest
Yonatan Wilkof

Great explanation! Thanks!

Siva K Divi
Guest
Siva K Divi

Your explanation is Awesome. Thanks Yong.

Joker Wu
Guest
Joker Wu

i’ve replayed JUnit test method to this:

        @BeforeClass
	public static void beforeClass() {
		//new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
		//sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		sessionFactory = new Configuration().configure().buildSessionFactory();
	}

and the method is still remarked with a deleted line through it(still deprecated).

Fikri Fadzil
Guest
Fikri Fadzil

Method

buildSessionFactory()

is deprecated in Hibernate 4. Instead of using the old way, you may also try a tutorial from RoseIndia at http://www.roseindia.net/hibernate/Hibernate4Example.shtml. The configuration is written for Hibernate 4.

Joker Wu
Guest
Joker Wu

Got it .
Thanks a lot.Best wishes for you.

himanshu
Guest
himanshu

but using new configuration() will gives the error required annotated instance to work..

Fikri Fadzil
Guest
Fikri Fadzil

hello himanshu. use this..

new Configuration().addAnnotatedClass(Users.class).configure();

simply call addAnnotatedClass() method and pass your class which contains annotations.

Joker Wu
Guest
Joker Wu

The References says:
Parameters:
annotatedClass – The class containing annotations.
But in my project,there are many classes configed in the annotation way.So, how can i use the method and set peremeter.

thanX.

Fikri Fadzil
Guest
Fikri Fadzil

in that case, you can create a method and pass your class such this.

public void getInitializedConfiguration(Class kelas) {
	new Configuration().addAnnotatedClass(kelas).configure();
}
trackback
Maven 3 + Hibernate 3.6 + Oracle 11g Example (Annotation)

[…] No update on “HibernateUtil.java“, since Hibernate 3.6, both XML mapping and annotation are sharing the same “org.hibernate.cfg.Configuration” class. Bye bye AnnotationConfiguration Read this – AnnotationConfiguration is deprecated in Hibernate 3.6 […]