Sql HQL查询不工作,靠近';的语法不正确';错误。使用Spring和Hibernate

Sql HQL查询不工作,靠近';的语法不正确';错误。使用Spring和Hibernate,sql,hibernate,spring,spring-mvc,hql,Sql,Hibernate,Spring,Spring Mvc,Hql,我正在尝试执行以下更新查询并得到错误 疑问是= @Transactional public List<Order> getClosedOrders(String userID) throws DataAccessException { try { String SQL_SELECT_QUERY = "from Order as o where o.orderStatus='closed' and o.account.profile.userId='"+userID+"'"; S

我正在尝试执行以下更新查询并得到错误

疑问是=

@Transactional
public List<Order> getClosedOrders(String userID) throws DataAccessException {
try { 

String SQL_SELECT_QUERY = "from Order as o where o.orderStatus='closed' and o.account.profile.userId='"+userID+"'";

String SQL_UPDATE_QUERY = "update Order set orderStatus=completed where orderStatus=closed and account.profile.userId='"+userID+"'";

List<Order> orderList = (List<Order>) list(SQL_SELECT_QUERY); 

if(!orderList.isEmpty()) {

batchUpdate(SQL_UPDATE_QUERY);
return orderList;
}
return null;
} catch(Exception ex) {

ex.printStackTrace();
throw new DataAccessException(errorMessage);
} 
}
@Transactional
公共列表getClosedOrders(字符串用户ID)引发DataAccessException{
试试{
字符串SQL_SELECT_QUERY=“from Order as o,其中o.orderStatus='closed'和o.account.profile.userId=”“+userId+”;

String SQL_UPDATE_QUERY=“更新订单集orderStatus=completed,其中orderStatus=closed,account.profile.userId=”“+userId+””; List orderList=(List)List(SQL\u SELECT\u查询); 如果(!orderList.isEmpty()){ batchUpdate(SQL\u更新\u查询); 返回订单列表; } 返回null; }捕获(例外情况除外){ 例如printStackTrace(); 抛出新的DataAccessException(errorMessage); } }
但是,选择查询正在工作,但是 对于它提供的更新查询 以下错误:

警告[http-8080-2] (jdbceptionReporter.java:71)-SQL 错误:102,SQLState:S0001

错误[http-8080-2] (jdbceptionReporter.java:72)- “,”附近的语法不正确

org.hibernate.exception.sqlgrammareexception: 无法执行更新查询

在 org.hibernate.exception.sqlstatecoverter.convert(sqlstatecoverter.java:67)

在 org.hibernate.exception.jdbceptionhelper.convert(jdbceptionhelper.java:43)

在 org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)

在 org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:334)

在 org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:209)

我不明白为什么会这样。我没有在查询中的任何地方使用“,”但它仍然表示“,”附近的语法不正确 为什么会这样?如何解决这个问题? 提前感谢您。

首先:

<property name="hibernate.show.sql" value="true"></property>
和使用

addString("userId",userId);

可能这些更改将帮助您消除问题。

我不确定,但尝试通过反勾号(对于MySQL)或双引号(对于PostgreSQL)或类似方式来取消顺序。如果您的查询使用原始SQL,则数据库可能会将其识别为保留关键字(如
orderby
)。

您缺少此关键字中的引号

String SQL_UPDATE_QUERY = "update Order set orderStatus=completed where orderStatus=closed and account.profile.userId='"+userID+"'";
String SQL_UPDATE_QUERY=“更新订单集orderStatus=completed,其中orderStatus=closed,account.profile.userId=”“+userId+””; 难道不是吗

String SQL_UPDATE_QUERY = "update Order set orderStatus='completed' where orderStatus='closed' and account.profile.userId='"+userID+"'"; 字符串SQL\u UPDATE\u QUERY=“更新订单集orderStatus='completed'其中orderStatus='closed'和account.profile.userId='”+userId+“”;
所有orderStatus表达式的引号。

这确实很奇怪。我想到的唯一一件事是userID包含一个逗号,但我相信您已经检查过了。尝试记录Generated SQL查询。添加了该属性,但不知道为什么它不显示查询。“更新订单集orderStatus=completed,其中orderStatus=closed,account.profile.userID=”+userID+“”;我想执行这个查询。@Sugar,这是HQL查询,SQL查询呢?您应该启用hibernate.show.sql,然后在控制台中查看原始sql查询。 String SQL_UPDATE_QUERY = "update Order set orderStatus='completed' where orderStatus='closed' and account.profile.userId='"+userID+"'";