这边通过定义Hibernate的工具类实现会话工厂SessionFactory及数据库等相关依赖配置,如数据源配合引入等,调用之前先定义初始化配置文件工具类InitConfUtils、SpringConext全局应用bean对象实例,具体参见代码
import java.io.IOException;@b@import java.io.InputStream;@b@import java.util.Hashtable;@b@import java.util.List;@b@import java.util.Properties;@b@import javax.sql.DataSource;@b@import org.hibernate.HibernateException;@b@import org.hibernate.Session;@b@import org.hibernate.SessionFactory;@b@import org.hibernate.cfg.AnnotationConfiguration;@b@import org.springframework.orm.hibernate4.LocalSessionFactoryBean;@b@import com.woopa.common.util.InitConfUtils;@b@import com.woopa.common.persist.SpringContext;@b@@b@/**@b@ * To deal with exception for [closed connection], something to change: @b@ * 1. session factory's configuration is created by spring@b@ * 2. use proxool's connection pool instead of hibernate's@b@ * @Date 2013-6-6@b@ */@b@@SuppressWarnings("deprecation")@b@public class Hibernate4Util {@b@@b@ private static final ThreadLocal<Session> session = new ThreadLocal<Session>();@b@ private static Hibernate4Util hibernateUtil = null;@b@ @b@ private static final String dialect = "org.hibernate.dialect.Oracle10gDialect";@b@@b@ private static Hashtable<String, SessionFactory> sessionFactoys = new Hashtable<String, SessionFactory>();@b@@b@ private Hibernate4Util() {}@b@@b@ public static synchronized Hibernate4Util getInstance() {@b@ if (hibernateUtil == null) {@b@ hibernateUtil = new Hibernate4Util();@b@ }@b@ return hibernateUtil;@b@ }@b@ @b@ /**@b@ * 获得sessionFactory@b@ * @param dsCode@b@ * @return@b@ */@b@ public SessionFactory getSessionFactory(String dsCode) {@b@ try {@b@ return sessionFactoys.get(dsCode);@b@ } catch (Throwable ex) {@b@ ex.printStackTrace();@b@ }@b@ return null;@b@ }@b@ @b@ /**@b@ * 持久化配置@b@ */@b@ public Boolean configure(String dtCode, DataSource datasource, String dia,@b@ Class<?>[] clazz, String[] packagesToScan) {@b@ try {@b@ sessionFactoys.put(dtCode, buildSessionFactory(dtCode, datasource, dia == null ? dialect : dia, clazz, packagesToScan));@b@ return Boolean.valueOf(true);@b@ } catch (Throwable ex) {@b@ ex.printStackTrace();@b@ }@b@ return Boolean.valueOf(false);@b@ }@b@@b@ /**@b@ * 创建SessionFactory@b@ * @param p@b@ * @return@b@ */@b@ private SessionFactory buildSessionFactory(String dtCode,@b@ DataSource datasource, String dia, Class<?>[] clazz,@b@ String[] packagesToScan) {@b@ try {@b@ InitConfUtils.getParamValue("");@b@ @b@ LocalSessionFactoryBean asf = new LocalSessionFactoryBean();@b@ asf.setDataSource(datasource);@b@ asf.setHibernateProperties(newProperties(dia));@b@ @b@ asf.setAnnotatedClasses(clazz);@b@ asf.setPackagesToScan(packagesToScan);@b@@b@ asf.afterPropertiesSet();@b@ @b@ SpringContext.registerSingleton("sessionFactory_" + dtCode, asf, true);@b@ @b@ return asf.getObject();@b@ @b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ return null;@b@ }@b@@b@ /**@b@ * 设置hibernate连接属性@b@ * @return@b@ */@b@ public Properties newProperties(String dialect) {@b@ Properties properties = new Properties();@b@ InputStream is = null;@b@ try {@b@ properties.put("hibernate.cache.provider_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");@b@ properties.put("hibernate.cache.use_query_cache", "false");@b@ properties.put("hibernate.cache.use_second_level_cache", "false");@b@ properties.put("hibernate.format_sql", "false");@b@ properties.put("hibernate.show_sql", "false");@b@ properties.put("hibernate.jdbc.fetch_size", "50");@b@ properties.put("hibernate.jdbc.batch_size", "30");@b@ properties.put("hibernate.query.factory_class", "org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory");@b@ properties.put("hibernate.dialect", dialect);@b@ @b@ //overwrite properties@b@ is = getClass().getResourceAsStream("/hibernate.properties");@b@ if(is != null){@b@ properties.load(is);@b@ }@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ } finally {@b@ if(is != null)@b@ try {@b@ is.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ return properties;@b@ }@b@ @b@ public void addClazz(AnnotationConfiguration conf, List<Class<?>> clazz){@b@ if(clazz != null && !clazz.isEmpty()){@b@ for(Class<?> clz : clazz){@b@ conf.addAnnotatedClass(clz);@b@ }@b@ } @b@ clazz = null;@b@ }@b@ @b@ /**@b@ * 获得session@b@ * @param key@b@ * @return@b@ * @throws HibernateException@b@ */@b@ public Session getSession(String key) throws HibernateException {@b@ Session s = (Session) session.get();@b@ if (s == null) {@b@ s = getSessionFactory(key).openSession();@b@ session.set(s);@b@ }@b@ return s;@b@ }@b@ @b@ /**@b@ * 关闭session@b@ * @throws HibernateException@b@ */@b@ public void closeSession() throws HibernateException {@b@ Session s = (Session) session.get();@b@ session.set(null);@b@ if (s != null) s.close();@b@ }@b@ @b@ public void realese() {@b@ sessionFactoys.clear();@b@ }@b@ @b@}