Thursday, 25 April 2013

Update Query in Hibernate HQL




  Session session=HibernateUtil.getSessionFactory.openSession();
  String hql = null;
    try {
     hql="update Email e set e.status=:e_stat , e.email=:e_email where e.id='"+id+"'"; // Email is bean name representing corresponding table. 
    


     Query q=session.createQuery(hql);
     q.setParameter("e_stat", true);
     q.setParameter("e_email", "test@mail.com");
     int i= q.executeUpdate(); 
     System.out.print(i); // i return 1 mean's query has successfully executed
     } catch (HibernateException ex) {
     ex.printStackTrace();
 }