2012-05-25 3 views
24

¿Alguien sabe cómo evitar la próxima ADVERTENCIA para el siguiente código?¿Cómo arreglar una consulta de Hibernate con advertencias de depreciación?

org.hibernate.hql.internal.ast.HqlSqlWalker [HqlSqlWalker.java:929] [DEPRECATION] Encountered positional parameter near line 1, column 56. Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead. 
Hibernate: select user0_.ID_USER as ID1_0_, user0_.USERNAME as USERNAME0_, user0_.PASSWORD as PASSWORD0_ from USER user0_ where user0_.USERNAME=? 
Hibernate: select authoritie0_.ID_USER as ID1_0_1_, authoritie0_.ID_ROLE as ID2_1_, role1_.ID_ROLE as ID1_2_0_, role1_.NOMBRE as NOMBRE2_0_, role1_.DESCRIPCION as DESCRIPC3_2_0_ from USER_ROLE authoritie0_ inner join ROLE role1_ on authoritie0_.ID_ROLE=role1_.ID_ROLE where authoritie0_.ID_USER=? 

Query query = sessionFactory.getCurrentSession().createQuery("from User where username=?"); 
query.setString(0, username); 
List<User> users = query.list(); 

Respuesta

39

Como dice el mensaje, utilice los parámetros con nombre en su lugar.

List users = sessionFactory.getCurrentSession() 
     .createQuery("from User where username = :username") 
     .setString("username", username) 
     .list(); 
+0

muchas gracias! – webmeiker

+0

Esto es solo en createquery o tenemos que cambiar en namedquery también? –

+0

La advertencia se origina al analizar la consulta, que es la misma para la cadena pasada a createQuery frente a la cadena utilizada en una consulta con nombre. –

16

o puede utilizar al estilo APP parámetros posicionales:

createQuery("from User where username=?1"); 

necesita cambiar NamedQueries también.

5

Aquí hay un ejemplo de código que escanea todos los archivos de consulta hbm y los cambia a parámetros posicionales jpa-style.

@Test 
public void replaceQuestionMark() throws IOException { 

    org.springframework.core.io.Resource[] resources = new org.springframework.core.io.Resource[0]; 
    String queryFilesLocation = "persistence/hibernate/hbm/queries"; 
    resources = (new PathMatchingResourcePatternResolver()).getResources("classpath*:" + queryFilesLocation+ "/**"); 

    for (org.springframework.core.io.Resource resource : resources) { 

     //remove xml description tag because it has also question mark and it can disturb the question mark scanning. 
     // I will put it back at the end 
     String queryFile = new String(IOUtils.toByteArray(resource.getInputStream())).replace("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", ""); 

     //split the queries file by "</named-query>" ending tag 
     Iterable<String> queriesInFile = Splitter.on("</named-query>").split(queryFile); 


     //initialize new list for new fixed queries string 
     List<String> fixedQueryInFile = Lists.newArrayList(); 

     for (String queryString : queriesInFile) { 

      Integer questionIndex = 0; 
      char[] queryChars = queryString.toCharArray(); 
      StringBuffer fixedQueryString = new StringBuffer(); 
      //scan each char in a single query 
      for (int i = 0; i < queryChars.length; i++) { 

       char character = queryChars[i]; 
       //copy the char to the new fixed query 
       fixedQueryString.append(character); 
       if (character == '?') { 
        //add the question mark order number after the question mark 
        fixedQueryString.append(questionIndex); 
        //increase the order for the next question mark in this single query 
        questionIndex++; 
       } 

      } 
      //add the fixed query string to the list 
      fixedQueryInFile.add(fixedQueryString.toString()); 
     } 

     //add the xml description tag that we removed before + the fixed queries. 
     String fixedFile = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + Joiner.on("</named-query>").join(fixedQueryInFile); 
     File file = null; 
     try { 
      file = resource.getFile(); 
      //find the query file in the sources 
      FileOutputStream fileOutputStream = new FileOutputStream(new File(file.getPath().replace("target\\classes", "src\\main\\resources"))); 
      //overwrite the file with the new fixed queries 
      fileOutputStream.write(fixedFile.getBytes()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    if (resources == null) { 
     throw new RuntimeException("resource not found"); 
    } 
} 
Cuestiones relacionadas