hibernatetools.java

来自「实现留言薄和发表文章的功能」· Java 代码 · 共 35 行

JAVA
35
字号
package com.test.bbs.dao.impl;

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

public final class HibernateTools {
	private static SessionFactory sessionFactory;

	private static ThreadLocal session = new ThreadLocal();

	private HibernateTools() {
	}

	static {
		sessionFactory = new Configuration().configure(
				"com/test/bbs/dao/impl/hibernate.cfg.xml")
				.buildSessionFactory();
	}

	@SuppressWarnings("unchecked")
	static Session getCurrentSession() {
		Session s = (Session) session.get();
		if (s != null)
			return s;
		s = sessionFactory.openSession();
		session.set(s);
		return s;
	}

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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?