1和0都是什么概念?假设说1是成功,0是失败。那么: public Integer addRe(Re re) {
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.saveOrUpdate(re);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
                                return 1;
} else {
                                return 0;
                        }
} finally {
if (session != null) {
session.close();
}
}
}你在每次调用这个方法的时候都用一个Integer类型的对象接一下,看看能不能接到值。

解决方案 »

  1.   

    错了。应该是这样: public Integer addRe(Re re) {
    Integer num = 0;
    try {
    session = HibernateSessionFactory.currentSession();
    tx = session.beginTransaction();
    session.saveOrUpdate(re);
    tx.commit();
    num = 1;
    } catch (Exception e) {
    e.printStackTrace();
    if (tx != null) {
    tx.rollback();
    }
    } finally {
    if (session != null) {
    session.close();
    }
    return num;
    }
    }
      

  2.   

    回2楼的朋友:
       方法为什么一定要写成Integer ? int 不行么?  我是这样写的
    public int register(TblUser item){
    Session session = HibernateSessionFactory.getSession() ;
    Transaction tx = null ;
    try {
    tx = session.beginTransaction();
    session.save(item);
    tx.commit();
    session.createQuery("select user.uid from TblUser user where user.uname = '"+item.getUname()+"'");
    } catch (HibernateException e) {
    // TODO Auto-generated catch block
    if(tx != null){
    tx.rollback();
    return 0 ;
    }
    e.printStackTrace();
    }finally{
    session.close();
    }
    return 1 ;
    }
      

  3.   

    得得到 , 但是对于 ”tx != null“ 这个条件是不是能够包括添加失败的这个异常 存有疑问, 我也是刚学Hibernate。
      

  4.   

    除了这种验证的话 是不是还可以使用Struts的错误处理机制呢?  那个要怎么验证的?
      

  5.   

    理论上来说应该能得到的,至于struts错误处理机制,我不太了解,无能为力。不好意思了~
      

  6.   

    如果实在不放心,那个return就写在finally里面。这样不管有没有异常都会return到值的。
      

  7.   

    象这样的DAOImpl login 代码 , 该怎么很好的使用Struts的错误处理机制呢 ?
    public int register(TblUser item){
            Session session = HibernateSessionFactory.getSession() ;
            Transaction tx = null ;
            try {
                tx = session.beginTransaction();
                session.save(item);            
                tx.commit();
                session.createQuery("select user.uid from TblUser user where user.uname = '"+item.getUname()+"'");
            } catch (HibernateException e) {
                // TODO Auto-generated catch block
                if(tx != null){
                    tx.rollback();
                    return 0 ;
                }
                e.printStackTrace();
            }finally{
                session.close();
            }
            return 1 ;
        }
      

  8.   

    用hibernate配置dao用点麻烦的,因为要在dao层不段的实现类似这样的索取数据:
    Session session = HibernateSessionFactory.getSession() ;
            Transaction tx = null ;
            try {
                tx = session.beginTransaction();
                session.save(item);            
                tx.commit();
                session.createQuery("");我希望你能在Spring中进行dao层的配置.
      

  9.   

    save方法会返回实体对象的id的  如果id不为空就可以判断了.我简直是个天才...
      

  10.   

    save方法会返回实体对象的id的  如果id不为空就可以判断了.我简直是个天才...
      

  11.   

    是在写还是在做JAVA,
    这里的做法我认为是直接抛一个自定义的注册失败异常,在外面在捕获,凯不是更好一点