java - throwing meaningful exceptions from hibernate DAO implementation -


in web application(jsp+hibernate+hsqldb on tomcat) code, using couple of dao implementations.the base class dao implementation contains session open,close logic.a number of domain specific dao classes extend base class provide specific find(),delete() methods

i wanted give user meaningful messages when error occurs ,instead of error500 message . since,the base class method uses hibernate.session class get(),saveorupdate() methods ,they throw hibernateexception.the domain specific subclasses need catch wrap in custom exception , rethrow it.

i tried way..i don't know if correct way it..i welcome 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;         }         {             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 =  (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());            }         }       } 

are sure customer want have meaningful message? believe meaningful error should appear in case of business errors. technical (read, unexpected) errors customer should see generic error page, error reference code, no more that.

another problem code going include e.getmessage error message. not good, because, potentially, message can have technical information, may break system. but, saying that, logs have have information possible (within sensible limits, there shouldn't passwords, card details) error.

so basic rule - technical errors show customer least can. business errors other story, here should clear possible.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -