[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里面去..