首页 > 代码库 > 蜗牛—Hibernate之自写固定组件

蜗牛—Hibernate之自写固定组件

Hibernate的存储都是在session中进行的,此session非彼session

下面就是从生成的Hibernate配置文件获得连接数据库,然后再得到session等等

package com.attence.tools;

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

public class HibernateSessionFactory {

	private static Configuration configuration;
	private static SessionFactory sessionFactory;
	private static final String HIBERNATE_CONFIG = "hibernate.cfg.xml";

	static {
		configuration = new Configuration().configure(HIBERNATE_CONFIG);
		sessionFactory = configuration.buildSessionFactory();
	}

	public static Session getSession() {
		return sessionFactory.openSession();
	}

}