Note that there are some explanatory texts on larger screens.

plurals
  1. POthrowing meaningful exceptions from hibernate DAO implementation
    primarykey
    data
    text
    <p>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</p> <p>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.</p> <p>I tried it this way..I don't know if this is the correct way to do it..I would welcome your opinion/suggestions</p> <p>sincerely,</p> <p>Jim</p> <pre><code>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(); } } } </code></pre> <p>The domain specific dao is</p> <pre><code>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()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload