Please update the above code............,
December 20, 2008 at 9:09 AM
Need to add Transaction statement and use createSQLQuery() instead of createQuery() method. ----------------------------- import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration;
public class DeleteHQLExample {
public static void main(String[] args) { // TODO Auto-generated method stub Session sess = null; try{ SessionFactory fact = new Configuration().configure().buildSessionFactory(); sess = fact.openSession(); String hql = "DELETE FROM INSURANCE WHERE ID = 2"; Transaction tx = sess.beginTransaction(); Query query = sess.createSQLQuery(hql); int row = query.executeUpdate(); tx.commit(); if(row == 0){ System.out.println(" Doesn't delete any row"); }else{ System.out.println(" Deleted row " + row); } }catch(Exception e){ System.out.println(e.getMessage()); }finally{ sess.close(); } }