2011-11-11 3 views
1

je reçois l'erreur « JDBCExceptionReporter - types de données incompatibles en combinaison » lors de l'exécution du code suivant:JDBCExceptionReporter - types de données incompatibles en combinaison

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 capteur

et sensorid sensorreading sont tous les entiers et l'autre des jointures dates . Des idées pour lesquelles je pourrais obtenir ça?

+0

Que faites-vous référence quand vous dites "l'erreur ci-dessus"? –

Répondre

1

Yikes! Le problème était les citations autour des identifiants dans la liste.

Pourquoi ne pas hiberner correctement?

2

Si le sensorId est entier le HQL doit être comme ci-dessous

for(int sensId:sensorIds){ 
     // sb.append("'"); 
     sb.append(sensId); 
     // sb.append("',"); 
     sb.append(","); 
} 

Dans une requête être SQL or HQL, les entiers ne doivent pas être à l'intérieur du quotes(')

in (1,2,3) -- For the integers 

in ('a','b','c') -- For the char types