Last active
December 11, 2015 01:29
-
-
Save RicardoMurad/4524014 to your computer and use it in GitHub Desktop.
* Helper class to Hibernate 4.1 startup * in plain java applications;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package foo.bar; | |
| import java.util.Properties; | |
| import org.hibernate.SessionFactory; | |
| import org.hibernate.cfg.Configuration; | |
| import org.hibernate.service.ServiceRegistry; | |
| import org.hibernate.service.ServiceRegistryBuilder; | |
| /** | |
| * @author rmurad | |
| * Helper class to Hibernate startup | |
| * in plain java applications; | |
| * http://ricardomurad.com | |
| */ | |
| public class HibernateUtil { | |
| private static SessionFactory sessionFactory; | |
| static{ | |
| try { | |
| Configuration configuration = new Configuration(); | |
| Properties properties = configuration.getProperties(); | |
| ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() | |
| .applySettings(properties) | |
| .buildServiceRegistry(); | |
| sessionFactory = configuration.buildSessionFactory(serviceRegistry); | |
| } catch (Throwable e) { | |
| throw new ExceptionInInitializerError(e); | |
| } | |
| } | |
| public static final SessionFactory getSessionFactory() { | |
| return sessionFactory; | |
| } | |
| public static final void shutdown(){ | |
| getSessionFactory().close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment