java - Transaction Management in Hibernate with DAO Design Pattern -


i have many tables, every table,we have dao interface , daoimplementation class.

example dao interface

public interface cancelpolicydao {  public cancelpolicy insertcancelpolicy(cancelpolicy cpdao)throws channeldispatcherexception;  public cancelpolicy updatecancelpolicy(cancelpolicy cpdao)throws channeldispatcherexception;  public void deletecancelpolicy(cancelpolicy cpdao)throws channeldispatcherexception;  public cancelpolicy findbycancelpolicydata(integer id, integer offsetum, integer nights, float poram, byte ispercent)throws channeldispatcherexception;  public cancelpolicy findbycancelpolicyid(integer id)throws channeldispatcherexception; } 

example daoimplementation class

public class cancelpolicydaoimpl implements cancelpolicydao {  @override public cancelpolicy insertcancelpolicy(cancelpolicy bean) throws channeldispatcherexception {      session ses = null;     try {          ses = hibernateconnector.getinstance().getsession();         ses.save(bean);         ses.flush();         return bean;     } catch (exception e) {         e.printstacktrace();         throw new channeldispatcherexception(dbutil.getstacktracemessage(e));     } {         if (ses != null) {             try {                 ses.close();             } catch (exception er) {                 er.printstacktrace();             }         }     }  }  @override public cancelpolicy updatecancelpolicy(cancelpolicy bean) throws channeldispatcherexception {     session sess = null;      try {          sess = hibernateconnector.getinstance().getsession();         sess.update(bean);         sess.flush();         return bean;     } catch (exception e) {       e.printstacktrace();         throw new channeldispatcherexception(dbutil.getstacktracemessage(e));     }  }  @override public void deletecancelpolicy(cancelpolicy bean) throws channeldispatcherexception {     session sess = null;      try {          sess = hibernateconnector.getinstance().getsession();         sess.delete(bean);         sess.flush();     } catch (exception e) {      e.printstacktrace();         throw new channeldispatcherexception(dbutil.getstacktracemessage(e));     }  }  @override public cancelpolicy findbycancelpolicydata(integer id, integer offsetum, integer nights, float poram, byte ispercent) throws channeldispatcherexception {      session ses = null;     try {         ses = hibernateconnector.getinstance().getsession();         query query = ses.createquery("from cancelpolicy "                 + " a.cancelpolicytypeid =:cancelpolicytypeid  "                 + " ,   a.offsetunitmultiplier =:offsetunitmultiplier  "                 + " ,   a.nights =:nights  "                 + " ,   a.percentoramount =:percentoramount "                 + " ,   a.ispercent =:ispercent");          query.setparameter("cancelpolicytypeid", id);         query.setparameter("offsetunitmultiplier", (offsetum));         query.setparameter("nights", (nights));         query.setparameter("percentoramount", poram);         query.setparameter("ispercent", ispercent);          list querylist = query.list();         if (querylist != null && querylist.isempty()) {             return null;         } else {             return (cancelpolicy) querylist.get(0);         }     } catch (exception e) {        e.printstacktrace();         throw new channeldispatcherexception(dbutil.getstacktracemessage(e));     } {         if (ses != null) {             try {                 ses.close();             } catch (exception e) {                 e.printstacktrace();             }         }     }  }  public cancelpolicy findbycancelpolicyid(integer id) throws channeldispatcherexception {      session ses = null;     try {         ses = hibernateconnector.getinstance().getsession();         query query = ses.createquery("from cancelpolicy "                 + " a.id =:id  ");          query.setparameter("id", id);          list querylist = query.list();         if (querylist != null && querylist.isempty()) {             return null;         } else {             return (cancelpolicy) querylist.get(0);         }     } catch ( exception e) {        e.printstacktrace();         throw new channeldispatcherexception(dbutil.getstacktracemessage(e));     } {         if (ses != null) {             try {                 ses.close();             } catch (exception e) {                 e.printstacktrace();             }         }     }  }   

example main method

 public static void main(string[] args)  {         // how handel transaction in hibernate ?      cancelpolicydao cancelpolicydao = hibernatedaofactory.getinstance().getcancelpolicydao();        cancelpolicy insertcancelpolicy = cancelpolicydao.findbycancelpolicydata(2, 76, 25, 25.36f, 3);     if(insertcancelpolicy==null){         cancelpolicy cancelpolicy = new cancelpolicy();     cancelpolicy.setcancelpolicytypeid(1);     cancelpolicy.setnights(2);      insertcancelpolicy = cancelpolicydao.insertcancelpolicy(cancelpolicy);     }     integer autoincrementid = insertcancelpolicy.getid();      availabilitydao availabilitydao =  hibernatedaofactory.getinstance().getavailabilitydao();     availability availability = new availability(); //        using  cancelpolicy autoincrementid      availability.setid(autoincrementid);     availability.setcount(2);     availability.setmaxlos(5);     availabilitydao.insertavailability(availability);     .     .     .     .     .    } 

now question how handle transaction in daoimpl's ? should pass session object parameter every daoimpl's or there better approach

i'd recommend not reinvent wheel; use existing, robust , tested code.

the comments mentioned aop , spring framework. imho way go. spring framework has subproject called spring data allows define finder methods (like findbycancelpolicydata) in declarative way.

this save lot of work.

if reason don't want / may not use spring, can still read amazingly documentation of base framework , mentioned spring data in order gain lots of excellent ideas, regarding transaction (via aop), code reuse (via generic daos) or api design. don't miss read.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -