hibernateutil.java
来自「网上商店 新增功能: 1」· Java 代码 · 共 165 行
JAVA
165 行
package netstore.service;
import netstore.framework.exceptions.*;
//hibernate 3
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.apache.commons.logging.*;
import netstore.businessobjects.*;
/**
*
* @author huangyongfeng
*工具类
*/
public class HibernateUtil {
//apache commons log使用方法:
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static Configuration configuration;
private static SessionFactory sessionFactory;
// Create the initial SessionFactory from the default configuration files
// 初始化
static {
try {
//初始化配置
configuration = new Configuration();
configuration.addClass(Category.class)
.addClass(Customer.class)
.addClass(Item.class)
.addClass(Order.class);
sessionFactory = configuration.buildSessionFactory();
//也可以将上述加载过程放入配置文件,用下面方法加载
// We could also let Hibernate bind it to JNDI:
// configuration.configure().buildSessionFactory()
} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
log.error(ex.getMessage());
throw new ExceptionInInitializerError(ex);
}
}
/**
* Returns the SessionFactory used for this static class.
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
/* Instead of a static variable, use JNDI:
SessionFactory sessions = null;
try {
Context ctx = new InitialContext();
String jndiName = "java:hibernate/HibernateFactory";
sessions = (SessionFactory)ctx.lookup(jndiName);
} catch (NamingException ex) {
throw new InfrastructureException(ex);
}
return sessions;
*/
return sessionFactory;
}
/**
* Returns the original Hibernate configuration.
*
* @return Configuration
*/
public static Configuration getConfiguration() {
return configuration;
}
/**
* Rebuild the SessionFactory with the static Configuration.
*
*/
public static void rebuildSessionFactory()
throws DatastoreException {
synchronized(sessionFactory) {
try {
sessionFactory = getConfiguration().buildSessionFactory();
} catch (Exception ex) {
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
}
/**
* Rebuild the SessionFactory with the given Hibernate Configuration.
*
* @param cfg
*/
public static void rebuildSessionFactory(Configuration cfg)
throws DatastoreException {
synchronized(sessionFactory) {
try {
sessionFactory = cfg.buildSessionFactory();
configuration = cfg;
} catch (Exception ex) {
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
}
/**
* 获取一个临时Session
* @return
* @throws DatastoreException
*/
public static Session getSession() throws DatastoreException {
try {
return sessionFactory.openSession();
} catch (Exception ex) {
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
/**
* 关闭工厂
*/
public static void close() {
try {
sessionFactory.close();
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
/**
* 关闭临时session
* @param session
* @throws DatastoreException
*/
public static void closeSession(Session session) throws DatastoreException {
try {
session.close();
} catch (Exception ex) {
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
/**
* 事务回滚,当操作数据库失败时调用
* @param transaction
* @throws DatastoreException
*/
public static void rollbackTransaction(Transaction transaction)
throws DatastoreException {
try {
if (transaction != null)
transaction.rollback();
} catch (Exception ex) {
log.error(ex.getMessage());
throw DatastoreException.datastoreError(ex);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?