JPA like fuzzy query

Source: Internet
Author: User
Tags trim
  First floor Q:    
Hi,

I ' m trying to use the AS statement with JPA. What I want to does is get all of the items that has a name that matchs the keyword ' og ' For example (' Dog ', ' Big dog ' ...). If I Write this:

query = Em.createquery ("Select I from Item i WHERE i.name like:keyword");
Query.setparameter ("keyword", keyword);

That'll take the items and that has the exact keword (in my example only ' og ' would appear). I ' ve tried to use the wildcards%uh ID doesn ' t work:

query = Em.createquery ("Select I from Item i WHERE i.name Like '%:keyword% ');
Query = Em.createquery ("Select I from Item i WHERE i.name like%:keyword%");

Does Anybody knows how do I use the like statement with wildcards? And by the the-the-there a-ignore the case of my keyword (' og ' or ' og ' should give the same result).

Thanks,

Antonio goncalves

Second floor:

Hello Antonio,

Can Don't use
query = Em.createquery ("Select I from Item i WHERE UPPER (i.name) like UPPER (: keyword)");
Query.setparameter ("keyword", "%" keyword "%");
Query.getresultlist ();

Best regards,
Chris
Third floor:
Hi Chris. It doesn ' t work with the UPPER keyword. Here's the error that I ' ve got:

caused By:exception [TOPLINK-8025] (Oracle TOPLINK Essentials-2006.4 (Build 060412)): oracle.toplink.essentials.except Ions. Ejbqlexception
Exception description:syntax Error parsing the query [SELECT i from Item i WHERE UPPER (i.name) like UPPER (: keyword) ORDER by I.name], unexpected tokens [like].
|#]

Any idea?

Otherwise, the wildcard works fine:

query = Em.createquery ("Select I from Item i WHERE i.name like:keyword");
Query.setparameter ("keyword", "%" keyword "%");
Query.getresultlist ();
 
 

Level four:

 

   
Hello,

Sorry, looks like I w As wrong on the UPPER (: Keword) portion. The spec states:
String_Expression [not] like Pattern_value [ESCAPE Escape_character]
"The Pattern_value is a stri ng literal or a string-valued input parameter "

so you'll have to call toUpperCase () on the string you pass into The query IE:
query.setparameter ("keyword", "%" keyword.touppercase () "%");

Best regards,
Chris

Final wording:

Thanks, it works fine like that

query = Em.createquery ("Select I from Item i WHERE UPPER (i.name) Like:keyword ORDER by I.name");
Query.setparameter ("keyword", "%" + keyword.touppercase () + "%");

Antonio

My error wording: Sql.append ("and s.idcard like%?%"); Paraml.add (Stuff.getidcard (). Trim ());

The right should be: Sql.append ("and s.idcard like?");

Paraml.add ("%" +stuff.getidcard (). Trim () + "%");

Transferred from: http://forums.oracle.com/forums/thread.jspa?threadID=423742

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.