HibernateUtil.java

Hibernate常用工具类
None.gif import net.sf.hibernate.HibernateException;
None.gifimport net.sf.hibernate.Session;
None.gifimport net.sf.hibernate.Transaction;
None.gifimport net.sf.hibernate.cfg.Configuration;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  HibernateUtil  dot.gif {
InBlock.gif
InBlock.gif     
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//** Holds a single instance of Session */
InBlock.gif    
private static final ThreadLocal threadLocal = new ThreadLocal();
InBlock.gif
InBlock.gif    
private static final ThreadLocal threadTransaction = new ThreadLocal();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//** The single instance of hibernate configuration */
InBlock.gif    
private static final Configuration cfg = new Configuration();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//** The single instance of hibernate SessionFactory */
InBlock.gif    
private static net.sf.hibernate.SessionFactory sessionFactory;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * Returns the ThreadLocal Session instance. Lazy initialize the
InBlock.gif     * <code>SessionFactory</code> if needed.
InBlock.gif     *
InBlock.gif     * @return Session
InBlock.gif     * @throws HibernateException
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static Session getSession() throws HibernateException dot.gif{
InBlock.gif        Session session 
= (Session) threadLocal.get();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (session == nulldot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (sessionFactory == nulldot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
try dot.gif{
InBlock.gif                    cfg.configure(CONFIG_FILE_LOCATION);
InBlock.gif                    sessionFactory 
= cfg.buildSessionFactory();
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 catch (Exception e) dot.gif{
InBlock.gif                    System.err.println(
"%%%% Error Creating SessionFactory %%%%");
InBlock.gif                    e.printStackTrace();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            session 
= sessionFactory.openSession();
InBlock.gif            threadLocal.
set(session);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return session;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * Close the single hibernate session instance.
InBlock.gif     *
InBlock.gif     * @throws HibernateException
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void closeSession() throws HibernateException dot.gif{
InBlock.gif        Session session 
= (Session) threadLocal.get();
InBlock.gif        threadLocal.
set(null);
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (session != nulldot.gif{
InBlock.gif            session.close();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**
InBlock.gif     * Default constructor.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
private HibernateUtil() dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void beginTransaction() throws HibernateException dot.gif{
InBlock.gif        Transaction tx 
= (Transaction) threadTransaction.get();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (tx == nulldot.gif{
InBlock.gif                tx 
= getSession().beginTransaction();
InBlock.gif                threadTransaction.
set(tx);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (HibernateException ex) dot.gif{
InBlock.gif            
throw new HibernateException(ex.toString());
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void commitTransaction() throws HibernateException dot.gif{
InBlock.gif        Transaction tx 
= (Transaction) threadTransaction.get();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
InBlock.gif                tx.commit();
InBlock.gif            threadTransaction.
set(null);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (HibernateException ex) dot.gif{
InBlock.gif            rollbackTransaction();
InBlock.gif            
throw new HibernateException(ex.toString());
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void rollbackTransaction() throws HibernateException dot.gif{
InBlock.gif        Transaction tx 
= (Transaction) threadTransaction.get();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            threadTransaction.
set(null);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) dot.gif{
InBlock.gif                tx.rollback();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (HibernateException ex) dot.gif{
InBlock.gif            
throw new HibernateException(ex.toString());
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 finally dot.gif{
InBlock.gif            closeSession();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/beyond_beyond/archive/2005/04/27/146435.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值