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

Snail—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();
	}

}



Snail—Hibernate之自写固定组件