论坛首页 入门讨论版 DAO

动态代理 session 困惑

浏览 366 次
该帖已经被评为新手帖
作者 正文
最后更新时间:2008-07-13
//接口实现类   
public class BranchgoodsGroupImpl implements BranchGoodsGroupDao {   
       
    private  Session session;   
  
    public Session getSession() {   
        return session;   
    }   
  
    public void setSession(Session session) {   
        this.session = session;   
    }   
  
    public Iterator getBranchGoodsGroupList(int branchid, String groupclass) {   
        return session.createQuery("from SBranchgoodsGroup where SBranchgoodsGroup.id.branch.id=? and SBranchgoodsGroup.id.groupclass=?")   
                        .setInteger(0, branchid)   
                        .setString(1, groupclass).list().iterator();   
    }   
  
    public Iterator getGoodsGroupListbyBranchid(int branchid) {   
        return session.createQuery("select SBranchgoodsGroup.id.groupclass from SBranchgoodsGroup where SBranchgoodsGroup.id.branch.id=? group by SBranchgoodsGroup.id.groupclass")   
                .setInteger(0, branchid).list().iterator();   
    }   
  
    public int create(Object o) {   
        session.saveOrUpdate(o);   
        return 0;   
    }   
  
    public int delete(Object o) {   
        session.delete(o);   
        return 0;   
    }   
  
    public int update(Object o) {   
        session.update(o);   
        return 0;   
    }   
       
       
}   
//代理   
public class DynaProxyGoodsGroup implements InvocationHandler {   
        /**   
         * 要处理的对象(也就是要在方法的前后加上业务逻辑的对象)  
         */  
        private Object delegate;   
       
        /**  
         * 动态生成方法被处理过后的对象   
         * @param delegate  
    1    * @param proxy  
         * @return  
         */  
        public Object bind(Object delegate) {   
            this.delegate = delegate;   
            return  Proxy.newProxyInstance(this.delegate.getClass().getClassLoader(),   
                                          this.delegate.getClass().getInterfaces(),    
                                          this);   
        }   
        public Object invoke(Object proxy, Method method, Object[] args)   
            throws Throwable {   
            Object result = null;    
            Session session=null;   
            Transaction tx = null;   
            try{   
                 session= SessionFactory.currentSession();   
                 tx = session.beginTransaction();                  
                            result= method.invoke(this.delegate, args);   
                session.flush();   
                tx.commit();   
            } catch (org.hibernate.exception.DataException de) {   
                de.printStackTrace();   
                if (tx != null)   
                    tx.rollback();   
                throw de;   
            } catch (org.hibernate.exception.GenericJDBCException gg) {   
                gg.printStackTrace();   
                if (tx != null)   
                    tx.rollback();   
                throw gg;   
            } catch (Exception ex) {   
                ex.printStackTrace();   
                if (tx != null)   
                    tx.rollback();   
                throw new RuntimeException(ex.getMessage());   
            } finally {   
                if (session != null) {   
                    try {   
                        SessionFactory.closeSession();   
                    } catch (HibernateException e) {   
                        throw new RuntimeException(e);   
                    }   
  
                }   
            }   
        return result;   
    }   
  
}   
  
//testcase   
  
public class Test {   
    public static void main(String[] args) {   
        BranchGoodsGroupDao hello = (BranchGoodsGroupDao)new DynaProxyGoodsGroup ().bind(new BranchgoodsGroupImpl());   
        hello.getGoodsGroupListbyBranchid(1);           
    }   
}  

问题出来了 当我调用 hello.getGoodsGroupListbyBranchid(1); 时候 发现session是空 困惑我的是 session应该如何引入到DAO里面去..
   
最后更新时间:2008-07-13
确实该拍
renco 写道

                 session= SessionFactory.currentSession();     
                 tx = session.beginTransaction();                    
                 result= method.invoke(this.delegate, args);     
                 session.flush();     
                 tx.commit();     
 



你自己没有把Session注入呀,增加一句:

                 session= SessionFactory.currentSession();     
                 tx = session.beginTransaction();
                 // 应该抽象一个注入接口,或者干脆反射来处理比较好
                 ((BranchgoodsGroupImpl)this.delegate).setSession(session);
                 result= method.invoke(this.delegate, args);     
                 session.flush();     
                 tx.commit();     
 
   
0 请登录后投票
最后更新时间:2008-07-21
什么不通过动态的ID来处理,在说用session 会影响性能。
   
0 请登录后投票
论坛首页 入门讨论版 DAO

跳转论坛:
JavaEye推荐