Spring Dao说错误的SQL语法 - 语法错误

时间:2014-11-11 07:39:21

标签: mysql spring

  

org.springframework.jdbc.BadSqlGrammarException:   PreparedStatementCallback;糟糕的SQL语法[删除   model_state_history FROM model_state_history INNER JOIN模型ON   model_state_history.model_id = model.id WHERE model.package_id =?];   嵌套异常是org.h2.jdbc.JdbcSQLException:SQL中的语法错误   语句“DELETE model_state_history FROM [*] model_state_history   INNER JOIN模型ON model_state_history.model_ID = model.ID WHERE   model.PACKAGE_ID =? “; SQL声明:

它会在输出中抛出上述内容。对于我的眼睛,我没有看到语法错误。我正在使用MySQL数据库。上面的SQL语法中有什么明显错误的吗?

使用DELETE FROM语法也会出现语法错误。

  

org.springframework.jdbc.BadSqlGrammarException:   PreparedStatementCallback;糟糕的SQL语法[DELETE FROM   model_state_history INNER JOIN模型ON model_state_history.model_id =   model.id WHERE model.package_id =?];嵌套异常是   org.h2.jdbc.JdbcSQLException:SQL语句“DELETE中的语法错误   FROM model_STATE_HISTORY INNER [*] JOIN模型ON   model_STATE_HISTORY.model_ID = model.ID WHERE model.PACKAGE_ID =? “;   SQL语句:DELETE FROM model_state_history INNER JOIN模型ON   model_state_history.model_id = model.id WHERE model.package_id =?   [42000-176]

这是正在运行的代码

  @Transactional
  private int deleteAllModels(Long packageId) throws DataAccessException {
    return new NamedParameterJdbcTemplate(dataSource).update(
        "DELETE FROM model_state_history INNER JOIN model ON model_state_history.version_id = model.id WHERE model.package_id = :package_id",
        ImmutableMap.of("package_id", packageId));

1 个答案:

答案 0 :(得分:0)

第二个想法,我认为你可能有JOIN语法的问题。你正在写

  

DELETE FROM ... INNER JOIN ... ON msh.version_id = m.id WHERE m.package_id =:package_id

我认为WHERE应该是AND,所以查询会变成:

"DELETE FROM model_state_history INNER JOIN model 
 ON model_state_history.version_id = model.id AND model.package_id = :package_id"

因为WHERE引用了DELETE语句,该语句中没有model表格"范围"。

干杯,