Saturday, 2 March 2013

Update table using HQL

Update table using HQL

This example of code is using the previous hibernate configuration ,orm file and Java class file


  try {
           Session session=HibernateSessionFactory.getSessionFactory().openSession();
           Transaction tx=session.beginTransaction();
            String hql = "UPDATE StudentClass s SET s.studentName= :newName WHERE s.studentId= :studentID";
           // String hql="UPDATE StudentClass  SET studentName= :newName WHERE //studentId= :studentID";
            Query query = session.createQuery(hql);
            query.setParameter("studentID", new Long(1));
            query.setParameter("newName "Nitesh sahay");
            int rowCount = query.executeUpate();
            System.out.println("Rows affected: " + rowCount);
          
        } catch (java.lang.HibernateException ex) {
            ex.printStackTrace();
        }