Create a SessionFactory in Hibernate.

There are following steps for creating a SessionFactory in Hibernate.

First way :-
Configuration : First create a Configuration object. This will refer to the path of configuration file.
Resource : Add config file resource to Configuration object.
Properties : Set properties in the Configuration object.
SessionFactory : Use Configuration object to build SessionFactory.

Example :-
Configuration config = new Configuration();
config.addResource(“configuration.hbm.xml”);
config.setProperties( System.getProperties() );
SessionFactory sessions = config.buildSessionFactory();

Second way :-
The methods ServiceRegistryBuilder and buildSessionFactory in Hibernate 4.3.4 are deprecated.

Configuration conf = new Configuration().configure();
ServiceRegistry sr = new StandardServiceRegistryBuilder().
                                     applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(sr);

Third Way :-
The method ServiceRegistryBuilder has been deprecated in Hibernate 4.3.0 version.
we need to import org.hibernate.boot.registry.StandardServiceRegistryBuilder for this.

Configuration cfg=new Configuration().configure();
StandardServiceRegistryBuilder builder= new StandardServiceRegistryBuilder()
                                                                        .applySettings(cfg.getProperties());
SessionFactory factory= cfg.buildSessionFactory(builder.build());