我使用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'中的参数)修复了问题!
为什么?
另请注意 - 即使创建一个名为“IncidentsServeritiesEntity”的类作为儿子并使用适当的int ID也无济于事 - 只能从'in'切换到'='。
@Param("incidentseverityid") List<IncidentsServeritiesEntity> incidentseverityid
如果我使用以下选项,则选择不起作用:
identseverityid in :incidentseverityid
或:
(identseverityid in (:incidentseverityid))
(带括号)。
答案 0 :(得分:0)
解决方案有点难看但有效:定义=
而不是in
缺少的内容,创建一个长or
命令并传递它。
String allParams = "";
for (String currParam : param)
{
allParams += ("param = " + currParam + " or ");
}
allParams = allParams .substring(5, allParams .length() - 3);