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

Java 尝试检索主键时出现SQL语法错误,java,sql,spring,hibernate,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(); Sessio

我正在尝试使用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();
  }
   }
当我尝试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)
尝试使用:

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

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();
  }
DML操作不支持方法list(),在这种情况下必须使用executeUpdate();它将返回更新的行数。
您还必须调用事务处理。提交()以持久化更新。

我不知道hibernate,但是运行更新时,
createQuery()
似乎是错误的选择(错误消息也这么说)。createSQLQuery()会产生更大的错误。如果您对O/R映射不感兴趣,为什么要使用hibernate?任何使用
query
it名称的方法似乎都不是一个好选择。您不是在运行“查询”,而是在运行更新。
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();
  }