尝试检索主键时SQL语法错误

时间:2014-09-15 09:55:53

标签: java sql spring hibernate

我正在尝试使用JSON调用更新表。

@Override
public List<GroupsDetails> editGroupList(String Name, String startDate, String endDate, Integer Status_GroupID, Integer GroupsID) {
  SessionFactory sessionFactory=HibernateSessionManager.getSessionFactory();
  Session session=sessionFactory.getCurrentSession();
  Transaction transaction=session.beginTransaction();
  try{
    transaction.begin();
    @SuppressWarnings("unchecked")
    List<GroupsDetails> groupList=session.createQuery("UPDATE GroupsDetails SET Name='" + Name + "', StartDate='" + startDate + "', EndDate='" + endDate + "', Status_GroupID=" + Status_GroupID + " WHERE GroupsID=" + GroupsID).list();
    System.out.println(startDate);
    return groupList;
  }finally{
    session.close();
  }
   }

但是我一直收到错误消息,表示不支持DML操作。 错误是:

    Request processing failed; nested exception is org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations [UPDATE com.nebuilder.ats.pojo.GroupsDetails SET Name='Alpha', StartDate='21-06-2014', EndDate='23-08-2014', Status_GroupID=1 WHERE GroupsID=1]

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations [UPDATE com.nebuilder.ats.pojo.GroupsDetails SET Name='Alpha', StartDate='21-06-2014', EndDate='23-08-2014', Status_GroupID=1 WHERE GroupsID=1]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

当我尝试createSQLQuery

Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

2 个答案:

答案 0 :(得分:0)

尝试使用query.executeUpdate()

public int executeUpdate()
                  throws HibernateException
Execute the update or delete statement.

The semantics are compliant with the ejb3 Query.executeUpdate() method.

Returns:
    The number of entities updated or deleted.

所以你的代码应该是这样的:

try{
    transaction.begin();
    int count = session.createQuery("UPDATE GroupsDetails SET Name='" + Name + "', StartDate='" + startDate + "', EndDate='" + endDate + "', Status_GroupID=" + Status_GroupID + " WHERE GroupsID=" + GroupsID).executeUpdate();
    System.out.println("Number of records updated = " + count);
    transaction.comit();
  } catch(Exception ex){
    transaction.rollback();
    ex.printStacktrace();
}finally{
    session.close();
  }

答案 1 :(得分:0)

DML操作不支持方法list(),在这种情况下你必须使用executeUpdate();这将返回更新的行数。 此外,您还必须致电交易。 commit()来保持更新。