📄 hibernatebase.java
字号:
/*
* Created on 2004-10-12 TODO To change the template for this generated file go to Window - Preferences - Java - Code
* Style - Code Templates
*/
package com.hongsoft.agile.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.apache.log4j.xml.DOMConfigurator;
import com.hongsoft.agile.*;
import java.io.*;
public class HibernateBase {
private static SessionFactory sessionFactory; // 会话工厂
protected final static String CONFIG_FILE = Global.getConfPath() + "hibernate.cfg.xml";
protected final static String PROCESS_DEFINE = Global.getConfPath() + "agile.hbm.xml";
protected final static String BUSINESS_DEFINE = Global.getConfPath() + "business.hbm.xml";
protected final static String RES_DEFILE = Global.getConfPath() + "res.hbm.xml";
public static final ThreadLocal session = new ThreadLocal();
static {// init
synchronized (CONFIG_FILE) {
if (sessionFactory == null) {
DOMConfigurator.configure(Global.getConfPath() + "log4j.xml");
try {
Configuration conf = new Configuration().addFile(PROCESS_DEFINE).addFile(BUSINESS_DEFINE).addFile(
RES_DEFILE);
conf.configure(new File(CONFIG_FILE));
sessionFactory = conf.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
}
public static Session getSession() throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -