刚刚回答过一样的问题:http://community.csdn.net/Expert/topic/3562/3562866.xml?temp=.806698
解决方案 »
- hibernate调用带返回值的存储过程,结果总是1
- “123456”,使用utf8编码进行base64,结果的utf8编码再进行md5,结果是什么
- 站内消息是怎么做的?
- 请各位帮小弟看看
- 刚发现Hibernate未有效利用mysql的查询缓存!急!!!
- Struts+myEclipse+tomcat开发
- struts1.1中要验证文字长度不大于50在validation.xml文件中怎么写??(高分求解,进者有分),菜鸟真苦恼啊!
- 求助:Jsp 无法调用javaBean
- 初学者问题.使用Weblogic是不是必须在本地?急
- 有没有这几个命令?要打包ear
- 谁有WebLogic 8.1中文文档资料?
- 问一个笑破大牙,笑掉肚子的问题,哈哈,action类是在什么时候执行的?
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory { /**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "src/hibernate.cfg.xml"; /** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal(); /** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration(); /** The single instance of hibernate SessionFactory */
private static net.sf.hibernate.SessionFactory sessionFactory; /**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
} return session;
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* Default constructor.
*/
}