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条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型