Java tutorial
/* * Copyright 2012 Lorenzo Gonzlez. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.fpmislata.seguros.datos.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.context.internal.ThreadLocalSessionContext; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil { private static SessionFactory sessionFactory; public static synchronized void buildSessionFactory() { if (sessionFactory == null) { Configuration configuration = new Configuration(); configuration.configure(); configuration.setProperty("hibernate.current_session_context_class", "thread"); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } } public static void openSessionAndAttachToThread() { Session session = sessionFactory.openSession(); ThreadLocalSessionContext.bind(session); } public static SessionFactory getSessionFactory() { if (sessionFactory == null) { buildSessionFactory(); } if (sessionFactory.isClosed() == true) { throw new RuntimeException("El objeto sessionFactory est cerrado"); } return sessionFactory; } public static void closeSessionAndDeattachFromThread() { Session session = ThreadLocalSessionContext.unbind(sessionFactory); if (session != null) { if (session.isOpen() == true) { session.close(); } } } public static void closeSessionFactory() { if ((sessionFactory != null) && (sessionFactory.isClosed() == false)) { sessionFactory.close(); } } }