renco 2008-07-12 20:36
浏览 139
已采纳

请教 动态代理 session 什么时候注入

[code="java"]
//接口实现类
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 DynaProxyHello().bind(new BranchgoodsGroupImpl());
hello.getGoodsGroupListbyBranchid(1);

}
}
[/code]
问题出来了 当我调用 hello.getGoodsGroupListbyBranchid(1); 时候 发现session是空 困惑我的是 session应该如何引入到DAO里面去..

  • 写回答

2条回答 默认 最新

  • lggegegmail 2008-07-13 15:02
    关注

    [code="java"]public class BranchgoodsGroupImpl implements BranchGoodsGroupDao {
    public Session getSession() {

    return session;

    } [/code]

    改成

    [code="java"] public Session getSession() {

    return SessionFactory.currentSession();

    } [/code]

    应该就好了吧?
    没必要注入的,既然都可以直接拿到. 而且,session是和当前状态完全相关的,使用注入,很别扭.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了