首页

关于HibernateUtil工具类获得sessionFactory、持久化配置configure及创建SessionFactory、packagesToScan扫描包等操作

标签:HibernateUtil,hibernate工具类,SessionFactory,AnnotationSessionFactoryBean     发布时间:2018-02-08   

通过hibernate工具类HibernateUtil对获取SessionFactory会话工厂、持久化配置configure、创建SessionFactory、设置hibernate连接属性、扫描包packagesToScan、获取会话getSession及关闭会话closeSession等(com.xwood.common.util.spring.SpringContext参考其他页)

import java.io.File;@b@import java.util.Hashtable;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Properties;@b@@b@import javax.sql.DataSource;@b@@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.hibernate3.annotation.AnnotationSessionFactoryBean;@b@@b@import com.xwood.common.util.spring.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@ * @refactor chenzhenling @b@ * @Date 2012-11-2@b@ */@b@public class HibernateUtil {@b@@b@	private static final ThreadLocal<Session> session = new ThreadLocal<Session>();@b@	private static HibernateUtil 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 HibernateUtil() {}@b@@b@	public static synchronized HibernateUtil getInstance() {@b@		if (hibernateUtil == null) {@b@			hibernateUtil = new HibernateUtil();@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@			AnnotationSessionFactoryBean asf = new AnnotationSessionFactoryBean();@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@		properties.put("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");@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.ast.ASTQueryTranslatorFactory");@b@		properties.put("hibernate.dialect", dialect);@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@	 * 	@b@	 * @param hibernate_conf@b@	 * @return@b@	 */@b@	public void packagesToScan(String dtCode, AnnotationConfiguration hibernate_conf, String dir) {@b@		List<Class<?>> clz = null;@b@		if (!JarPojoHandler.getPojoClasses().isEmpty()) {@b@			clz = JarPojoHandler.getPojoClasses().get(dtCode);@b@		} @b@		if(clz != null){@b@			Iterator<Class<?>> it = clz.iterator();@b@			while (it.hasNext()) {@b@				Class<?> str = it.next();@b@				hibernate_conf.addAnnotatedClass(str);@b@			}@b@		} else {		@b@			List<File> list = JarPojoHandler.getDirFiles(dir);@b@			for (File f : list) {@b@				clz = JarPojoHandler.getClazzInstances(f, "com.asc", dtCode);@b@				Iterator<Class<?>> it = clz.iterator();@b@				while (it.hasNext()) {@b@					Class<?> str = it.next();@b@					hibernate_conf.addAnnotatedClass(str);@b@				}@b@			}@b@		}@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@}