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, "% Yan er%");

Used above. " As a placeholder, and then set the value of the parameter one by one, using the SetString method of query. But this reduces the readability of the code.

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

Replace the HQL statement above with the following: String hql = "from Tblfwxx FW where Fw.tltle like:zt";

Query query = session.createquery (HQL);

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

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

I've also talked about criteria queries (also object queries)

The criteria object is mainly to encapsulate the query conditions and to provide restrictions and other categories for assistance. (For specific use, 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.