运行以下代码时出现错误“JDBCExceptionReporter - 组合不兼容的数据类型”:
public Collection<SensorReading> getLastReadings(int[] sensorIds)
throws DataAccessException {
StringBuilder sb = new StringBuilder();
sb.append("SELECT r ");
sb.append("FROM Sensor AS s, SensorReading AS r ");
sb.append("WHERE s.id IN (");
for(int sensId:sensorIds){
sb.append("'");
sb.append(sensId);
sb.append("',");
}
//strip off the last comma
sb.setLength(sb.length() -1);
//build the rest of the query
sb.append(") AND s.id = r.sensorId ");
sb.append(" AND r.readingTimestampUtc >= s.lastBeaconUtc ");
List<SensorReading> readings = getHibernateTemplate().find(sb.toString());
//map to hold only the latest result
Map<Integer, SensorReading> readingsMap = new HashMap<Integer, SensorReading>();
//flash through the readings and make sure there's only one per sensor ID
for(SensorReading rdg:readings){
Integer sensorId = rdg.getSensorId();
SensorReading reading = readingsMap.get(sensorId);
if(reading==null){
readingsMap.put(sensorId, rdg);
}
else{
//replace if the new reading is later
if(reading.getReadingTimestampUtc().after(rdg.getReadingTimestampUtc())){
readingsMap.put(sensorId, reading);
}
}
}
return readingsMap.values();
}
传感器ID和传感器读取传感器都是整数,其他连接是日期。任何想法,为什么我可能会得到这个?
答案 0 :(得分:2)
如果sensorId
是整数,则HQL应该如下
for(int sensId:sensorIds){
// sb.append("'");
sb.append(sensId);
// sb.append("',");
sb.append(",");
}
在SQL or HQL
的任何查询中,整数不应位于quotes(')
in (1,2,3) -- For the integers
in ('a','b','c') -- For the char types
答案 1 :(得分:1)
糟糕!问题是列表中ID的引号。
为什么hibernate没有正确处理这个问题?