[Hibernate] The method buildSessionFactory() from the type Configuration is deprecated


Posted on : Sat, 19th Sep 15 09:04 am UTC
2314 votes

If you are using hibernate version 4.x.x then you might have seen that when we create a SessionFactory object we get an warning saying The method buildSessionFactory() from the type Configuration is deprecated.

File : Hibernate Snippet Language : Java
	SessionFactory factory = new Configuration().
	  configure().buildSessionFactory();
The method buildSessionFactory from the type Configuration is deprecated


Warning : The method buildSessionFactory() from the type Configuration is deprecated

Solution :

Replace the SessionFactory code with the below lines :

File : Snippet Language : Java
Configuration conf = new Configuration().configure("hibernate.cfg.xml");	
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(conf.getProperties());
SessionFactory factory = conf.buildSessionFactory(builder.build());

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +