码迷,mamicode.com
首页 > Web开发 > 详细

HibernateUtil

时间:2014-10-17 15:42:00      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:hibernate   工具类   使用步骤   

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

标签:hibernate   工具类   使用步骤   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!