Hibernate fuzzy search

Source: Internet
Author: User

Like SQL queries, Hibernate and HQL use the like keyword for Fuzzy queries. Fuzzy query can compare whether a string matches the specified string mode. The wildcard character is used as follows: % (percent): matches strings of any type and length. For Chinese characters, two percent signs "%" _ (underline) are required: matches a single character, it is generally used to limit the length of a string expression. Example 1. search for students whose names start with "M": String queryString = "from studentInfo s where s. sname like's % '"; 2. retrieve the student object whose name contains the String "abc": String queryString = "from studentInfo s where s. sname like '% abc %' "; 3. search student objects starting with S and with a String length of 5: String queryString = "from studentInfo s where s. sname like's ____ '"; four underscores" _ "4. example: retrieve all students whose names contain "Wang": String queryString = "from StudentInfo s where s. sname like '% "+ sname +" %' "; note that the HQL statement cannot be spliced. Wrong! DAO: public List findBySname (Object sname) {log. debug ("finding all StudentInfo instances"); try {// String queryString = "from StudentInfo s where s. name like '% "+ sname +" %' "; String queryString =" from StudentInfo s where s. sname like '% "+ sname +" %' "; Query queryObject = getSession (). createQuery (queryString); return queryObject. list ();} catch (RuntimeException re) {log. error ("find all failed ", Re); throw re;} page to output this List set.

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.