有人知道如何为以下代码避免下一个警告吗?
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();
答案 0 :(得分:43)
如消息所示,请改用命名参数。
List users = sessionFactory.getCurrentSession()
.createQuery( "from User where username = :username" )
.setString( "username", username )
.list();
答案 1 :(得分:16)
或者你可以使用JPA风格的位置参数:
createQuery("from User where username=?1");
您还需要更改NamedQueries。
答案 2 :(得分:5)
这是一个代码示例,它扫描所有hbm查询文件并将其更改为jpa样式的位置参数。
@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");
}
}