0

When I use old deprecated hibernate method for building session factory, it works fine:

SessionFactory sessionFactory = new Configuration()
      .configure().buildSessionFactory();

When I replace this with new method, its gets compiled but at runtime get unknown entity exception, seems like the new method is not picking up mapping resource="xyz.hbm.xml" property:

  Configuration configuration = new Configuration().configure();
      ServiceRegistry serviceRegistry
          = new StandardServiceRegistryBuilder()
              .applySettings(configuration.getProperties()).build();

      // builds a session factory from the service registry
      SessionFactory sessionFactory = `configuration.buildSessionFactory(serviceRegistry);`

My question is, do I need to make some changes in the configuration or add some property in hibernate.cfg.xml file so that the property is read and loaded while using the new method for building session factory?

I tried with below as well:

Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); SessionFactory sessionFactory = configuration .buildSessionFactory(serviceRegistry); 

1 Answers1

0

In new versions of Hibernate you can't mix configurations with new Configuration().configure() and config.buildSessionFactory(registry). You should do all configuration with StandardServiceRegistryBuilder. See this and this for details.

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • thanks, it worked, feels like jboss should pay more attention while writing the Hibernate reference, I just followed what they had mentioned in there. – Manoj Jayaswal Nov 02 '15 at 22:43
  • @ManojJayaswal For old versions of Hibernate your approach is correct. Maybe, you saw it in an old version documentation. And, please, mark my answer as the correct one. – v.ladynev Nov 04 '15 at 06:33