论坛首页 Java企业应用论坛

利用模板方式写一个管理hibernate 中session的方法

浏览 4829 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-02-23  
package com.t48.hr.util;

import org.hibernate.Session;
import org.hibernate.Transaction;
import com.t48.hr.util.HBSessionFactory;

public abstract class TransManager {
  public Session sess;	
  public abstract void process() throws Exception;

  public void execute() throws Exception
  {
	 Transaction trans=null;
	 try {
		sess=HBSessionFactory.getCurrentSession();
		trans=sess.beginTransaction();
		process();
		trans.commit();
	} catch (Exception e) {
		trans.rollback();
		e.printStackTrace();
		throw e;
		// TODO: handle exception
	}
	finally{
		HBSessionFactory.closeCurrentSession();
	}
  }

}
   发表时间:2007-02-23  
package com.t48.hr.admin.bo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.*;

import com.t48.hr.util.*;
import com.t48.hr.sys.dao.TParamDAO;
import com.t48.hr.sys.vo.TParam;
import com.t48.hr.sys.vo.TParamId;
import com.t48.hr.util.HBSessionFactory;

/*
 * 
 * 基本参数业务处理逻辑
 * */
public class baseBO {

	static  TParamDAO dao = new TParamDAO();//生成一个dao实例
	static Session session = null;
	//增加基本参数的大类一条记录
	public static void insertbase(final TParam info){
		try {
			
		TransManager demo = new TransManager(){

			public void process() throws Exception {
				
			 dao.save(info, sess);
			}
		};
		demo.execute();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
//	根据code返回一个集合
	public static List getFormNameList(long code){
		List list = null;
		try
		{
			Session session =HBSessionFactory.getCurrentSession();
			//根椐code号查出id号相等的字段
		    String again ="from TParam where id.code = "+code;
			Query againquery = session.createQuery(again);
			list = againquery.list();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return list;
	}
	//修改一条记录
	public static void updatebase(){
		
		
	}
	//删除一条记录
	public static int deletesplit(Long f){
		
		System.out.println("I come to delete f "+f);
		int flag=0;
		 final TParam demo = new TParam();
	     final TParamId id = new TParamId() ;
	     id.setId(f);
	     id.setCode(new Long(0));
	     demo.setId(id);
	     try{
	    		TransManager delete = new TransManager(){
	    			public void process() throws Exception {
	    			    dao.delete(demo, sess);   			 
	    			}
	    		};
	    	delete.execute();
	    	flag=1;
	     }
	     catch(Exception e){
	    	 flag=0;
	    	 e.printStackTrace();
	     }
	     return flag;
	 
	}

	//查询取ID最大值
	public static Long selectbaseID(){
		
		Long baseID = new Long(0);
		try{
    		TransManager delete = new TransManager(){
    			public void process() throws Exception {
    				
    				prameter1 =  sess.createQuery("select max(t.id.id) from TParam t").uniqueResult();
    			    System.out.println("dsfsdfsdf");
  	   			 
    			}
    		};
    	delete.execute();
    	baseID=(Long)delete.prameter1;
     }
     catch(Exception e){
  
    	 e.printStackTrace();
     }
		
	return baseID;
		
	}
	
	//子项删除业务
	public static void deletechild(final TParam id){

		try{
    		TransManager delete = new TransManager(){
    			public void process() throws Exception {
    				
    			    sess.delete(id);
    			    System.out.println("dsfsdfsdf");
  	   			 
    			}
    		};
    	delete.execute();
    
     }
     catch(Exception e){
  
    	 e.printStackTrace();
     }
		
	}
	public static List serarchid(final Long name){
		ArrayList list=null;
		try{
    		TransManager delete = new TransManager(){
    			public void process() throws Exception {
    				
    				prameter1  =(List)sess.createQuery("from TParam where code="+name).list();
    				
  	   			 
    			}
    		};
    	delete.execute();
    	list=(ArrayList)delete.prameter1;
    
     }
     catch(Exception e){
  
    	 e.printStackTrace();
     }
     return list;
		
	}
	/*基本参数的子项删除之后,返回一个集合*/
	public static  List getBasecontent(final Long id){
		
		ArrayList list=null;
		try{
    		TransManager delete = new TransManager(){
    			public void process() throws Exception {
    				
    				prameter1  =(List)sess.createQuery("from TParam where id.code ="+id).list();
    				
  	   			 
    			}
    		};
    	delete.execute();
    	list=(ArrayList)delete.prameter1;
    
     }
     catch(Exception e){
  
    	 e.printStackTrace();
     }
         
	return list;	
	}
	
	
	
}


利用一个bo来使用
0 请登录后投票
   发表时间:2007-02-23  
嘿嘿.异常处理上有点问题,重构成下面的:

package com.t48.hr.util;   
  
import org.hibernate.Session;   
import org.hibernate.Transaction;   
import com.t48.hr.util.HBSessionFactory;   
  
public abstract class TransManager {   
  public Session sess;     
  public abstract void process() throws Exception;   
  
  public void execute() throws Exception   
  {   
     Transaction trans=null;
     sess=HBSessionFactory.getCurrentSession();   // 移到这里
     try {   
        trans=sess.beginTransaction();   
        process();   
        trans.commit();   
    } catch (Exception e) { 
        if (trans != null) {    // 要判断
            trans.rollback();
        }
        e.printStackTrace();   
        throw e;   
        // TODO: handle exception   
    }   
    finally{
        HBSessionFactory.closeCurrentSession();   
    }   
  }   
  
}  
0 请登录后投票
   发表时间:2007-02-23  
代码要简化,类似demo一样的东西,查询,update只需要一个就可以了!

粗粗看了看,发现有如下代码:

    static  TParamDAO dao = new TParamDAO();//生成一个dao实例   
    static Session session = null;


Session为static不会产生线程安全问题?不过好象这个session从来没有使用。

另外看到楼主有如下代码:
    public static List serarchid(final Long name){   
        ArrayList list=null;   
        try{   
            TransManager delete = new TransManager(){   
                public void process() throws Exception {   
                       
                    prameter1  =(List)sess.createQuery("from TParam where code="+name).list();   
                       
                    
                }   
            };   
        delete.execute();   
        list=(ArrayList)delete.prameter1;   
       
     } 


既然在process里面如此使用session,还是建议将process声明成如下方式:
process(HibernateSession sess)  throws Exception


一点粗浅见解!
0 请登录后投票
   发表时间:2007-02-23  
...学习了
0 请登录后投票
   发表时间:2007-02-23  
   static  TParamDAO dao = new TParamDAO();//生成一个dao实例   
    static Session session = null;   


这个是我没有删掉的代码.忘记看了...

我也不知道,线程怎么处理
0 请登录后投票
   发表时间:2007-03-06  
prameter1沒有聲明啊?


解決辦法
在transManager中加入
public Object prameter1;


0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics