I am trying to delete ROW in SQL DB by session.delete() on calling a RemoveProject Servlet where getting Parameters for which I want to delete Object from db.
SessionFactory sf=DataConnect.getConnect();
Session sess=sf.openSession();
Transaction tr=sess.beginTransaction();
try{
Query qur=sess.createQuery("from Project");
org.hibernate.Criteria cr=sess.createCriteria(Project.class);
//cr.add(Restrictions.eq("name",request.getParameter("name")));
cr.add(Restrictions.idEq(Integer.parseInt(request.getParameter("id"))));
List obj=cr.list();
for (Iterator iterator = obj.iterator(); iterator.hasNext();)
{
osl.pojo.Project devobj = (osl.pojo.Project) iterator.next();
System.out.print("DELETING"+devobj.getName()+"from Project");
sess.delete(devobj);
tr.commit();System.out.println("ROW DELETED");
}
}catch(HibernateException he){ he.printStackTrace(); ;tr.rollback(); response.sendRedirect(request.getRequestURI());}
But it is not working I also Tried Flushing Session.
I am getting this Message in Glassfish Console.
***Info: DELETING DATA_To_BE_DELETED from Project
WARN: SQL Error: 1064, SQLState: 42000
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index=1' at line 1
Info: HHH000010: On release of batch it still contained JDBC statements
Severe: org.hibernate.exception.SQLGrammarException: could not execute statement***
The problem is because you are trying commit inside the for
loop, so change it to outside the loop:
for (Iterator iterator = obj.iterator(); iterator.hasNext();) {
osl.pojo.Project devobj = (osl.pojo.Project) iterator.next();
System.out.print("DELETING"+devobj.getName()+"from Project");
sess.delete(devobj);
}
tr.commit();
所述问题的数据库列名称与数据库属性“索引”冲突,因此在将对象数据更新回Sql时出错,并且在SQL语法中导致错误。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.