从 hibernate DAO 实现中抛出有意义的异常

发布于 2024-11-25 09:43:48 字数 2125 浏览 4 评论 0原文

在我的Web应用程序(tomcat上的jsp+hibernate+hsqldb)代码中,我使用了几个Dao实现。基类Dao实现包含所有会话打开、关闭逻辑。许多特定于域的Dao类扩展了这个基类以提供具体的 find()、delete() 方法

我想在发生错误时向用户提供有意义的消息,而不是 error500 消息。 由于基类方法使用 hibernate.Session 类来执行 get()、saveOrUpdate() 方法,因此它们会抛出 HibernateException。特定于域的子类需要捕获此异常并将其包装在一些自定义异常中并重新抛出它。

我尝试了这种方式..我不知道这是否是正确的方法..我

真诚地欢迎您的意见/建议,

吉姆

abstract class BaseDao{
   private Class persistentClass;
   public BaseDao(Class persistentClass) {
        super();
        this.persistentClass = persistentClass;
    }
   public Object findById(Long id) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();
        Object object = null;
        try {
            object = (Object) session.get(persistentClass, id);
            return object;
        }
        finally {
            session.close();
        }
    }

    @Override
    public void saveOrUpdate(Object obj) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();
        Transaction tx = null;
        try {
        tx = session.beginTransaction();
        session.saveOrUpdate(obj);          
        tx.commit();
        }catch(HibernateException e){
            if (tx != null) {
                       tx.rollback();   

             }
             throw e;
        }finally {
            session.close();
        }

    }
}

领域特定的 dao 是

class SaleOrderDao extends BaseDao{
    public SaleOrderDao() {
        super(SaleOrder.class);
    }
    @Override
   public SaleOrder findSaleOrderById(Long saleOrderId){            
            SaleOrder so =  (SaleOrder)findById(saleOrderId);
            return  so;

    }
    @Override
    public void saveOrUpdateSaleOrder(SaleOrder so){
         try{
                saveOrUpdate( so);
          }catch(HibernateException e){
              String msg = "could not insert/update saleorder"+so.getSONumber();
               throw new SaleOrderDaoException(msg+"/ "+e.getMessgae());
           }
        }

     }

In my web application(jsp+hibernate+hsqldb on tomcat) code, I am using couple of Dao implementations.The base class Dao implementation contains all the session open,close logic.A number of domain specific Dao classes extend this base class to provide specific find(),delete() methods

I wanted to give the user meaningful messages when an error occurs ,instead of a error500 message .
Since,the base class method uses a hibernate.Session class for get(),saveOrUpdate() methods ,they throw HibernateException.The domain specific subclasses need to catch this an wrap it in some Custom Exception and rethrow it.

I tried it this way..I don't know if this is the correct way to do it..I would welcome your opinion/suggestions

sincerely,

Jim

abstract class BaseDao{
   private Class persistentClass;
   public BaseDao(Class persistentClass) {
        super();
        this.persistentClass = persistentClass;
    }
   public Object findById(Long id) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();
        Object object = null;
        try {
            object = (Object) session.get(persistentClass, id);
            return object;
        }
        finally {
            session.close();
        }
    }

    @Override
    public void saveOrUpdate(Object obj) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();
        Transaction tx = null;
        try {
        tx = session.beginTransaction();
        session.saveOrUpdate(obj);          
        tx.commit();
        }catch(HibernateException e){
            if (tx != null) {
                       tx.rollback();   

             }
             throw e;
        }finally {
            session.close();
        }

    }
}

The domain specific dao is

class SaleOrderDao extends BaseDao{
    public SaleOrderDao() {
        super(SaleOrder.class);
    }
    @Override
   public SaleOrder findSaleOrderById(Long saleOrderId){            
            SaleOrder so =  (SaleOrder)findById(saleOrderId);
            return  so;

    }
    @Override
    public void saveOrUpdateSaleOrder(SaleOrder so){
         try{
                saveOrUpdate( so);
          }catch(HibernateException e){
              String msg = "could not insert/update saleorder"+so.getSONumber();
               throw new SaleOrderDaoException(msg+"/ "+e.getMessgae());
           }
        }

     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2024-12-02 09:43:48

您确定客户想要有意义的信息吗?我认为只有在出现业务错误时才应该出现有意义的错误。对于技术(读取、意外)错误,客户应该只看到通用错误页面,可能带有错误参考代码,但仅此而已。

您的代码的另一个问题是您要将 e.getMessage 包含到错误消息中。这不好,因为该消息可能包含一些技术信息,这可能有助于闯入您的系统。但是,话虽如此,日志必须包含尽可能多的有关错误的信息(在合理的范围内,不应有密码、卡详细信息)。

因此,基本规则是尽可能少地向客户展示技术错误。业务错误是另一回事,在这里你应该尽可能清楚。

Are you sure that customer want to have meaningful message? I believe that meaningful error should appear just in case of business errors. For technical (read, unexpected) errors customer should see just generic error page, probably with error reference code, but no more that that.

Another problem with your code is you are going to include e.getMessage into error message. It is not good, because, potentially, that message can have some technical information, which may help to break into your system. But, saying that, logs have to have as much information as possible (within sensible limits, there shouldn't be passwords, card details) about the error.

So the basic rule - for technical errors show to customer as least as you can. Business errors are the other story, here you should be as clear as possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文