首页 > Web开发 > 详细

HibernateUtil

时间:2014-10-17 15:39:54      阅读:186      评论:0      收藏:0      [点我收藏+]

Hibernate工具类

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

	private static final SessionFactory sessionFactory = buildSessionFactory();

	private static SessionFactory buildSessionFactory() {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			return new Configuration().configure().buildSessionFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}

主要使用步骤:

1、得到 Session

2、打开 Transaction

3、执行业务逻辑

4、提交 Transaction

5、关闭 Session

HibernateUtil

原文:http://blog.csdn.net/u011506951/article/details/40184759

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!