Skip to content

Instantly share code, notes, and snippets.

@Pudding124
Created April 21, 2020 06:13
Show Gist options
  • Save Pudding124/5b84c46a49f0c9e1752bb4aa2a8fc1c8 to your computer and use it in GitHub Desktop.
Save Pudding124/5b84c46a49f0c9e1752bb4aa2a8fc1c8 to your computer and use it in GitHub Desktop.
package util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
getSessionFactory().close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment