⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basehibernatedao.java

📁 hibernate应用测试,初学hibernate 的会员可以看看.
💻 JAVA
字号:
package gdpe.hibernate.department;

import gdpe.HibernateSessionFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;


/**
 * Data access object (DAO) for domain model
 * @author MyEclipse - Hibernate Tools
 */
//implements IBaseHibernateDAO 
public class BaseHibernateDAO {
	private static Log log=LogFactory.getLog(BaseHibernateDAO.class); 
	//private static Configuration configuration;
	//private static HibernateSessionFactory sessionFactory;
//	private  static Session session=null;
//	private  static Transaction tx=null;
//	static {
//	 try {
//		 //configuration =new Configuration();
//		 session= HibernateSessionFactory.getSession();
//		 
//	 }catch(Throwable e) {}	
//	}
	
//	public  Session getSession() {
//		return HibernateSessionFactory.getSession();
//	}
	public static Session getSession() {
	  try {
		  Session session=HibernateSessionFactory.getSession();
		  return session;
	  }catch(Exception e) {
		  log.debug("得不到session");
		  log.error(e.getMessage());
		  //throw DatastoreException.datastoreError(e);
	  }
	  return null;
	}
//	public static void startTransaction(Session session) {
//		try {
//			Transaction tx=session.beginTransaction();
//		  }catch(Exception e) {
//			  log.debug("没有开始事务");
//			  log.error(e.getMessage()); 
//		  }
//	
//	}
//	public static void commit()throws HibernateException {
//		try {
//			  tx.commit();
//		  }catch(Exception e) {
//			  System.out.println("提交不成功");
//			  log.debug("提交不成功");
//			  rollback();
//		  }
//	}
	public static  void close(Session session){
    	try {
    		if(session!=null)
    		session.close();
    	}catch(Exception e) {
    		log.debug("session关闭问题");
    		log.error(e.getMessage());
    	}
    }
	
//	public static void rollback() {
//    	try {
//    		if(tx!=null) {
//        		tx.rollback();
//        	}
//    	}catch(Exception e) {
//    		log.debug("回滚异常");
//    		log.error(e.getMessage());
//    	}
//    }
	
	
//	public void endTransaction(boolean commit)   
//    throws HibernateException{   
//       //,DAOException     
//		try {
//			 if (commit) {   
//			        tx.commit();   
//			    } else {   
//			        // Don't commit the transaction, can be faster for read-only operations   
//			        tx.rollback();   
//			    }   
//			    
//		}catch(Exception e) {
//			System.out.println("不能提交");
//		}
//		close(); 
//}//end endTransaction()   

}

⌨️ 快捷键说明

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