0

In hibernate framework, we provide all the database information in hibernate.cfg.xml file.

But if we are going to integrate the hibernate application with spring, we don't need to create the hibernate.cfg.xml file. We can provide all the information in the applicationContext.xml file.

But my question , Is it possible to integrate spring and hibernate using hibernate.cfg.xml without providing the hibernate configuration in applicationContext.xml file?

1 Answer 1

1

Yes you can do that. Create Config class (@Configuration) and initialize Session Factory bean

Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");

//creating seession factory object
SessionFactory factory=cfg.buildSessionFactory();

Now you can Autowire SessionFactory where were you need session.

1
  • Thanks Satyendra Sharma.While using Configuration annotation, I should also add @Bean annotation to the getSessionFactory method in HibernateUtil.java class, right ?
    – vishnu
    Jun 5, 2020 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.