Hibernate like use

Source: Internet
Author: User
string hql =  "from user as user where user.name like ";       Query query=querysession.createquery (HQL);         Query.setparameter (0, "Javaeye");       string hql =  "From user  as user where user.name like  ";   Query query=querysession.createquery (HQL);      query.setparameter (0, "Javaeye");   The query has no results. or only a user named "Javaeye" is found.     I would like to support the Chinese query according to the user's name, nickname and other multiple conditions.       Problem resolution A:    understanding like matching mode     like  ' mc% '      will search with the letter   All strings (such as  mcbadden) that begin with the mc .     like  '%inger '   will search for all strings (such as  ringer, Stringer) ending with the letter  inger .     like  '%en% '     will search all strings containing letter  en  in any position of the                       (e.g.  bennet, Green, McBadden).     like  ' _heryl '   will search all six-letter names at the end of the letter  heryl                        (such as  cheryl, Sheryl).     like  ' [ck]ars[eo]n '   will search the following strings: Carsen, Karsen, carson                    and  karson (e.g.  carson).     like  ' [M-z]inger '   will search with string  inger  end,                        all names that begin with any single letter from M to Z, such as  .     like  ' m[^c]% '   will search to start with the letter  M , and the second letter is not  c  all                       name (e.g.  macfeather).     Analysis:    The use of position placeholders can solve the problem of querying Chinese;    But if you have too many like queries, consider using the name placeholder;  The   example to read:   &NBSP string hql =  "From user as user where user.name like :name";                    + "  And user.alias like :alias ";      query query=querysession.createquery ( HQL);             query.setparameter (name, "%javaeye%");            Query.setparameter (alias, "%eye");       string hql =& nbsp; " From user as user where user.name like :name ";                + " and  User.alias like :alias ";   Query query=querysession.createquery (HQL);       Query.setparameter (name, "%javaeye%");     Query.setparameter (alias, "%eye");     Such a way is to support a multi-conditional Chinese query.     &NBSP If you want to find out the result of the mismatch. Use not like  keywords     For example:    string hql =  "from user as  User where user.name not like :name ";                          query query= Querysession.createquery (HQL);             query.setparameter (name, "% Javaeye% ");       string hql = " From user as user where  user.name not like :name ";                   query query= Querysession.createquery (HQL);       Query.setparameter (name, "%javaeye%");         NOTE: If you are using a HQL or SQL statement, consider the client's possible          when you use like.    use special character search. For example "&" &nbsp, "%",  "_", "_s_"      &NBSp;    to prevent such vulnerabilities to check the parameter values.            If the hibernate placeholder is used, this issue is not considered.

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.