Java tutorial
/* * Leaves Framework * Copyright (C) 2015 Hugo Miard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package com.hmiard.leaves.framework.Utils.Persistence; import com.hmiard.leaves.framework.LeavesApplication; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import javax.persistence.Entity; import java.io.File; import java.lang.annotation.Annotation; public class HibernateUtils { private static SessionFactory sessionFactory; private static Configuration conf; public static void init(File confFile) { conf = new Configuration().configure(confFile); init(); } public static void init(String resource) { conf = new Configuration().configure(resource); init(); } private static void init() { if (conf == null) return; try { StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(conf.getProperties()); sessionFactory = conf.buildSessionFactory(builder.build()); } catch (Throwable ex) { LeavesApplication.say("ERROR : Initial Hibernate SessionFactory creation failed : " + ex); throw new ExceptionInInitializerError(ex); } } public static void shutdown() { if (sessionFactory != null) sessionFactory.close(); } /** * Checking if a class is annotated as Entity * * @param c Class * @return boolean */ public static boolean isAnEntityClass(Class<?> c) { return c.isAnnotationPresent(Entity.class); } public static SessionFactory getSessionFactory() { return sessionFactory; } public static Configuration getConf() { return conf; } }