# Dao function using hibernate

I'm just wondering if the following is the best way to write a dao function. Should I get the entity manager before the transaction and close it after the transaction every time? Should I write transactions inside a dao?

public void sendBack(Long requestId,String comments){
EntityManager em = getEntityManager();
em.getTransaction().begin();
String update = "update CsRequestReceivers set activeInd = :activeInd,sendBackComments=:comments where requestId = :requestId and activeInd = :oldActiveInd";
em.createQuery(update).setParameter("activeInd", 0l)
.setParameter("requestId", requestId)
.setParameter("oldActiveInd", 1l)
.executeUpdate();
em.getTransaction().commit();
em.close();
}

• I think you refer to this thread of stackoverflow. Link Feb 28, 2011 at 8:10