一、前言
关于hibernate-util源码包中net.sf.gilead.core.hibernate.jboss.HibernateJBossUtil、net.sf.gilead.core.hibernate.jpa.HibernateJpaUtil、net.sf.gilead.core.hibernate.spring.HibernateSpringUtil三个工具类,分别注入jboss、jpa、spring容器实现的org.hibernate.SessionFactory会话工厂对象,详情参见源码说明。
二、源码说明
三个HibernateJBossUtil、HibernateJpaUtil、HibernateSpringUtil工具类(继承net.sf.gilead.core.hibernate.HibernateUtil基类)
package net.sf.gilead.core.hibernate.jboss;@b@@b@import java.lang.reflect.Method;@b@import net.sf.gilead.core.hibernate.HibernateUtil;@b@import org.hibernate.SessionFactory;@b@import org.hibernate.ejb.HibernateEntityManagerFactory;@b@import org.jboss.ejb3.entity.InjectedSessionFactory;@b@import org.jboss.jpa.injection.InjectedEntityManagerFactory;@b@@b@public class HibernateJBossUtil extends HibernateUtil@b@{@b@ public void setEntityManagerFactory(Object entityManagerFactory)@b@ {@b@ if (entityManagerFactory instanceof InjectedEntityManagerFactory)@b@ {@b@ entityManagerFactory = ((InjectedEntityManagerFactory)entityManagerFactory).getDelegate();@b@ }@b@@b@ if (entityManagerFactory instanceof HibernateEntityManagerFactory)@b@ {@b@ setSessionFactory(((HibernateEntityManagerFactory)entityManagerFactory).getSessionFactory());@b@ }@b@ else@b@ {@b@ throw new IllegalArgumentException("Cannot find Hibernate entity manager factory implementation for " + entityManagerFactory);@b@ }@b@ }@b@@b@ public void setSessionFactory(SessionFactory sessionFactory)@b@ {@b@ if (sessionFactory instanceof InjectedSessionFactory)@b@ {@b@ try@b@ {@b@ Method getDelegate = InjectedSessionFactory.class.getDeclaredMethod("getDelegate", (Class[])null);@b@ getDelegate.setAccessible(true);@b@@b@ sessionFactory = (SessionFactory)getDelegate.invoke(sessionFactory, (Object[])null);@b@ }@b@ catch (Exception ex)@b@ {@b@ throw new RuntimeException(ex);@b@ }@b@@b@ }@b@@b@ super.setSessionFactory(sessionFactory);@b@ }@b@}
package net.sf.gilead.core.hibernate.jpa;@b@@b@import net.sf.gilead.core.hibernate.HibernateUtil;@b@import net.sf.gilead.util.IntrospectionHelper;@b@import org.hibernate.ejb.HibernateEntityManagerFactory;@b@@b@public class HibernateJpaUtil extends HibernateUtil@b@{@b@ public void setEntityManagerFactory(Object entityManagerFactory)@b@ {@b@ if (!(entityManagerFactory instanceof HibernateEntityManagerFactory))@b@ {@b@ entityManagerFactory = IntrospectionHelper.searchMember(HibernateEntityManagerFactory.class, entityManagerFactory);@b@@b@ if (entityManagerFactory == null)@b@ {@b@ throw new IllegalArgumentException("Cannot find Hibernate entity manager factory implementation !");@b@ }@b@@b@ }@b@@b@ setSessionFactory(((HibernateEntityManagerFactory)entityManagerFactory).getSessionFactory());@b@ }@b@}
package net.sf.gilead.core.hibernate.spring;@b@@b@import net.sf.gilead.core.hibernate.HibernateUtil;@b@import org.hibernate.SessionFactory;@b@import org.hibernate.ejb.HibernateEntityManagerFactory;@b@import org.springframework.aop.framework.AopProxy;@b@@b@public class HibernateSpringUtil extends HibernateUtil@b@{@b@ public void setEntityManagerFactory(Object entityManagerFactory)@b@ {@b@ if (entityManagerFactory instanceof AopProxy)@b@ {@b@ entityManagerFactory = ((AopProxy)entityManagerFactory).getProxy();@b@ }@b@@b@ if (entityManagerFactory instanceof HibernateEntityManagerFactory)@b@ {@b@ setSessionFactory(((HibernateEntityManagerFactory)entityManagerFactory).getSessionFactory());@b@ }@b@ else@b@ {@b@ throw new IllegalArgumentException("Cannot find Hibernate entity manager factory implementation for " + entityManagerFactory);@b@ }@b@ }@b@@b@ public void setSessionFactory(SessionFactory sessionFactory)@b@ {@b@ if (sessionFactory instanceof AopProxy)@b@ {@b@ sessionFactory = (SessionFactory)((AopProxy)sessionFactory).getProxy();@b@ }@b@@b@ super.setSessionFactory(sessionFactory);@b@ }@b@}