HQL statement named parameter query example

Source: Internet
Author: User

Session session = This.getsession ();

String hql = "from Tblfwxx FW where fw.title like?";

Query query = session.createquery (HQL);

Query.setstring (0, "% brilliant Son");

Use the above. "As placeholders, and then set the values of the parameters one by one using the SetString method of query. However, this reduces the readability of the code.

So try to use "named parameter query" to solve these problems later.

Change the HQL statement above to: String hql = "from Tblfwxx FW where Fw.tltle like:zt";

Query query = session.createquery (HQL);

Query.setstring ("zt", "Yan Son");

=============================

I've talked about the criteria query (and also the object query).

The criteria object encapsulates the query criteria, and provides restrictions and other categories for assistance. (For specific usage, find network resources)

Example: Public List search (tblfwxx condition) {

Session session = This.getsession ();

Criteria C = Session.createcriteria (Tblfwxx.class);

if (null! = condition) {

if (condition.gettitle () = null &&!condition.gettitle (). Equals ("")) {

C.add (Restrictions.like ("title", Condition.gettitle (), matchmode.anywhere));

}

}

}

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.