HQL with parameter query

Source: Internet
Author: User

When I recently debugged a Web site with Eclipse, I found an old warning in the console:

WARN [org.hibernate.hql.internal.ast.hqlsqlwalker]–[deprecation] encountered positional parameter near line 1, column 9 5. Positional parameter is considered de precated; Use named parameters or Jpa-style positional parameters instead.

Search on the Internet to understand is the HQL statement in the query parameter placeholder use method obsolete.

It is recommended to replace the old placeholder Query method with either named parameters or JPA placeholders, and now compare the following:


Old:

HQL statement notation: string hql = "from user as u where U.username =?" (just use a hello placeholder)

Parameter settings: Query.setparameter (0, "Jason"), or query.setstring (0, "Jason"); (The first parameter is the position of the placeholder, int type)

New:

HQL statement notation: string hql = "from user as U where U.username =: Name" (colon + parameter name placeholder mode)

Parameter settings: Query.setparameter ("name", "Jason") (the first parameter is the parameter name, String type)


HQL statement notation: string hql = "from user as U where U.username =" (JPA placeholder, question mark + arbitrary digit placeholder)

Parameter settings: Query.setparameter ("Ten", "Jason") (the first parameter is a placeholder number, String type!!)


Use either of the latter two to make the warning disappear.

With the API, the framework of the update, many places will be changed, and even refactoring. But in general, it is to make the design more reasonable, controllable, reduce ambiguity.

So in the process of use sometimes have to pay extra attention to version changes. Continuous learning, continuous improvement, this is also a charm of programming, Ha, said much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.