Session.delete()不起作用

时间:2017-03-19 21:02:21

标签: java hibernate

我试图通过session.delete()在调用RemoveProject Servlet时删除SQL DB中的ROW,其中获取我想从db中删除Object的参数。

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());}

但它不起作用我也尝试了法拉盛会议。

我在Glassfish控制台收到此消息。

***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***

2 个答案:

答案 0 :(得分:0)

问题是因为你在for循环中尝试提交,所以将它改为循环外部:

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();

答案 1 :(得分:0)

上述问题的数据库列名与数据库属性“index”冲突,因此将对象数据更新回Sql时出错,并且在SQL语法中导致错误。