使用'in'而不是'='的例外情况在Spring选择的地方

时间:2015-07-09 08:53:45

标签: java mysql spring

我使用Spring来选择一个值。这是存储库中的查询:

@Query(value = "select * from incidents where descriptioninfo like %:descriptioninfo% and fixed = :fixed and starttimestamp > :startTime and deviceid in (select id from devices) and deviceid in (select deviceid from devicesmaps) and (incidentseverityid in (:incidentseverityid))  order by starttimestamp desc limit :start, :length", nativeQuery = true)
public List<IncidentEntity> getByStarttimestampAndDescriptioninfoAndSeverityAndFixed(@Param("startTime") String startTime, @Param("descriptioninfo") String descriptioninfo, @Param("incidentseverityid") List<Integer> incidentseverityid, @Param("start") int start, @Param("length") int length, @Param("fixed") int fixed);

如果我运行它,我会得到例外:

  

具有该名称[descriptioninfo]的参数不存在

奇怪的是,将选择更改为:

@Query(value = "select * from incidents where descriptioninfo like %:descriptioninfo% and fixed = :fixed and starttimestamp > :startTime and deviceid in (select id from devices) and deviceid in (select deviceid from devicesmaps) and (incidentseverityid = (:incidentseverityid))   order by starttimestamp desc limit :start, :length", nativeQuery = true)
public List<IncidentEntity> getByStarttimestampAndDescriptioninfoAndSeverityAndFixed(@Param("startTime") String startTime, @Param("descriptioninfo") String descriptioninfo, @Param("incidentseverityid") List<Integer> incidentseverityid, @Param("start") int start, @Param("length") int length, @Param("fixed") int fixed);

(仅从

更改
(incidentseverityid in (:incidentseverityid)) 

(incidentseverityid = (:incidentseverityid)) 

修复异常并检索所需的值!

可能的最小变化,从'in'到'='对不同的参数('incidentseverityid'而不是'descriptioninfo'中的参数)修复了问题!

为什么?

  • 带有'in'或'='的select在MySQL Workbench中使用相同的参数。
  • 请不要告诉我改变:param to?1 because I had a problem with ? in java 8
  • 请注意,该表在MySQL中被转储和恢复甚至修复,其引擎在InnoDB和MyISAM之间进行了更改。
  • 另请注意 - 即使创建一个名为“IncidentsServeritiesEntity”的类作为儿子并使用适当的int ID也无济于事 - 只能从'in'切换到'='。

    @Param("incidentseverityid") List<IncidentsServeritiesEntity> incidentseverityid
    
  • 如果我使用以下选项,则选择不起作用:

    identseverityid in :incidentseverityid
    

或:

    (identseverityid in (:incidentseverityid))

(带括号)。

1 个答案:

答案 0 :(得分:0)

解决方案有点难看但有效:定义=而不是in缺少的内容,创建一个长or命令并传递它​​。

String allParams = "";

for (String currParam : param)
{
    allParams += ("param = " + currParam + " or "); 
}

allParams = allParams .substring(5, allParams .length() - 3);
相关问题