Last Updated: February 25, 2016
·
1.635K
· dixonwh

Creating A SessionFactory In Hibernate 4

In Hibernate 4, Configuration.buildSessionFactory is deprecated in a favor of a new overload which builds a SessionFactory using a Service Registry.

Configuration cfg = new Configuration();
cfg.configure();

ServiceRegistryBuilder builder = new ServiceRegistryBuilder();
builder.applySettings(cfg.getProperties());
ServiceRegistry sr = builder.buildServiceRegistry();
cfg.buildSessionFactory(sr);

This comes in handy if you need to manually create a SessionFactory (aren't using JPA).