Subversion Repositories bacoAlunos

Rev

Rev 2005 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.es.exemplos.hibernate;


import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.proxy.HibernateProxy;

public class HibernateUtils {

        //    private static Session session;
        private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(HibernateUtils.class);
        private static SessionFactory sessionFactory;
        private static SessionFactory migrationSessionFactory;

        static
        {
                try
                {
                        createFactory();

//            session = sessionFactory.openSession();
                }
                catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
                {
                        logger.fatal(e,e);
                        throw new 1.5.0/docs/api/java/lang/ExceptionInInitializerError.html">ExceptionInInitializerError(e);
                }
        }

        private static void createFactory()
        {
                1.5.0/docs/api/javax/security/auth/login/Configuration.html">Configuration config = new 1.5.0/docs/api/javax/security/auth/login/Configuration.html">Configuration();
                config.configure(1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getContextClassLoader().getResource("pt/estgp/es/exemplos/hibernate/hibernate.cfg.xml"));
                //config.setInterceptor(new DirtyCheckInterceptor());
                sessionFactory = config.buildSessionFactory();

        }



        public static SessionFactory getSessionFactory()
        {
                if(sessionFactory.isClosed())
                        createFactory();
                return sessionFactory;
        }

        public static synchronized Session openSession()
        {
//        if(!session.isOpen())
                //      return sessionFactory.openSession();
//        return session;
                return getCurrentSession();
        }
        public static synchronized Session getCurrentSession() throws 1.5.0/docs/api/java/lang/IllegalStateException.html">IllegalStateException
        {
//        if(!examples.isOpen())
//            examples = sessionFactory.openSession();
//        return examples;
                return getSessionFactory().getCurrentSession();
        }

        /**
         *
         * @param o
         * @return
         */

        public static 5+0%2Fdocs%2Fapi+Object">Object narrow(5+0%2Fdocs%2Fapi+Object">Object o)
        {
                if(o == null)
                        return null;

                if (o instanceof HibernateProxy)
                {
                        return ((HibernateProxy)o).getHibernateLazyInitializer().getImplementation();
                }

                return o;
        }


}