Thread: Bootstrapping Hibernate..

Forum : Hibernate Core and Annotations.   5 Jan, 2017 05:00
User Image
sripanth4076

Newbie

Joined: 20 Apr, 2017

Points: 100

Threads: 4

Posts: 4

Bootstrapping Hibernate..

| Quote Date : 5 Jan, 2017    Views:1910    

can anyone explain about modern approach of Bootstrapping Hibernate using Service-Registryes..If it is possible post me working examples please..

tags: hibernate

Comments

User Image
Raju3803

Newbie

Joined : 20 Apr, 2017

Points : 100

Threads: 2

Posts: 6

Re: Bootstrapping Hibernate..

Reply Date : 5 Jan, 2017

package com.mb.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import com.mb.service.JDBCConnectionProviderImpl; public class ModernHibernateSessionFactory { private static SessionFactory sessionFactory; static { Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); builder.applySettings(configuration.getProperties()); ConnectionProvider connectionProvider = new JDBCConnectionProviderImpl(); builder.addService(ConnectionProvider.class, connectionProvider); StandardServiceRegistry registry = builder.build(); sessionFactory = configuration.buildSessionFactory(registry); } public SessionFactory getSessionFactory() { return sessionFactory; } public void closeSessionFactory() { if (sessionFactory != null) { sessionFactory.close(); } } }

User Image
Raju3803

Newbie

Joined : 20 Apr, 2017

Points : 100

Threads: 2

Posts: 6

Re: Bootstrapping Hibernate..

Reply Date : 5 Jan, 2017

Modern Bootstrapping [$java] package com.mb.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import com.mb.service.JDBCConnectionProviderImpl; public class ModernHibernateSessionFactory { private static SessionFactory sessionFactory; static { Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); builder.applySettings(configuration.getProperties()); ConnectionProvider connectionProvider = new JDBCConnectionProviderImpl(); builder.addService(ConnectionProvider.class, connectionProvider); StandardServiceRegistry registry = builder.build(); sessionFactory = configuration.buildSessionFactory(registry); } public SessionFactory getSessionFactory() { return sessionFactory; } public void closeSessionFactory() { if (sessionFactory != null) { sessionFactory.close(); } } } [:$java]

User Image
Venkat2121

Newbie

Joined : 20 Apr, 2017

Points : 100

Threads: 0

Posts: 2

Re: Bootstrapping Hibernate..

Reply Date : 25 Jan, 2017

Can anyone please explain what exactly the lines which are there inside the static block can do each line? Thanks in advance.

User Image
shiv4084

Newbie

Joined : 20 Apr, 2017

Points : 100

Threads: 0

Posts: 3

Re: Bootstrapping Hibernate..

Reply Date : 25 Jan, 2017

[$java] static { try { Configuration config = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); builder.applySettings(config.getProperties()); StandardServiceRegistry registy = builder.build(); sessionfactory = config.buildSessionFactory(registy); } catch (Exception e) { e.printStackTrace(); } [:$java]

Load More

No More Comments