How can I specify a case insensitive search in Java - java

I currently have this code in place:
query = "select ID_OBJECT from "+Config.props.getProperty("Ob.SchemaName.Objects")+".sdm_doc_info where name like ? and typefileverscurr = 'DWG'";
However this only returns results that match the upper case DWG.
I would like my search to be case insensitive.
How can I use ?i in this case?
I tried the following but my Eclipse does not like it:
query = "select ID_OBJECT from "+Config.props.getProperty("Ob.SchemaName.Objects")+".sdm_doc_info where name like ? and typefileverscurr.matches('(?i:dwg)')";
Thanks for your help.

Usually I use to do these kind of jobs in two ways, One is by using the SQL upper() or lower() functions, like this:
"select ID_OBJECT from "+something+".sdme_doc_info where lower(typefileverscurr) = 'dwg'";
OR
"select ID_OBJECT from "+something+".sdme_doc_info where upper(typefileverscurr) = 'DWG'";
And another way is to use like in the query and mix above functions as well to make more insensitive
"select ID_OBJECT from "+something+".sdme_doc_info where lower(typefileverscurr) like '%dwg%'";
OR
"select ID_OBJECT from "+something+".sdme_doc_info where upper(typefileverscurr) like '%DWG%'";

Related

How to convert a select statement in mysql into a query in java?

I have a select statement inside MySQL.
select * from tbl_user where path like '%,id,%'
How can I convert it to a query in Java? What I want to pass id.
maybe
#Query(value = "select * from tbl_user where path like CONCAT('%,',?1,',%')", nativeQuery = true)
Iterable<Tbl_user> findAllChildUsers(int id);
will help
It depends of what framework/library are you using.
in hibernate you can use :id, but you have to also check that it is native query.
In some libraries you just pass a String as a query.

Is there any way to do this query in HQL?

I'm new to Hibernate and I'm creating a simple movie finder app in Java. I need to make a query in my dao to search movies by a list of keywords. I know how to do it in SQL, but I was wondering if there is any way to make an HQL query to do this. The Java Method in the dao receives a List which is a list of keyowrds and the SQL statement is like this:
SELECT *
FROM movies
WHERE name LIKE '%keyword1%' OR name LIKE 'keyword2' ...
So to achieve this is necessary to iterate trough all the list and concatenate the Query for each keyword.
It's possible to make this query more simple in HQL, so you can pass a list of keywords to que query so it can use it with the LIKE statement?
Thanks.
SELECT *
FROM movies
WHERE name LIKE '%keyword1%' OR name LIKE 'keyword2'
Would translate to HQL as
from movies m where m.name like :keyword1 or m.name like :keyword2
You will need to pass the named parameters keyword1 and keyword2 when querying.
And if you insist of using the like matchers, you will need to loop over the list and dynamically generate the query.
The other option is to actually use the IN clause with the HQL, that would however make wildcard matches impossible.
from movies m where m.name in (:keywords)
Query qry = session.createQuery("From RegistrationBean as rb where rb."+searchCriteria+" like :sf");
qry.setString("sf",'%'+searchField+'%');
Not only did I need to do this, but I needed to do it in a #Query annotation in Spring Data. This is the only way I got it to work. (Keep in mind that this is in an interface.)
#Query(value="from Movie h where lower(h.name) like concat('%',lower(:term),'%'")
List<Movie> findLike(#Param("term") String term);
I put the calls to lower() to make it case-insensitive, and I used the concat() function to add the % characters. It didn't work when I wrote it like this:
#Query(value="from Movie h where lower(h.name) like lower(%:term%)")
List<Movie> findLike(#Param("term") String term); // Query doesn't work! Use concat()
It didn't even work like this, without case sensitivity:
#Query(value="from Movie h where h.name like %:term%")
List<Movie> findLike(#Param("term") String term); // Query doesn't work! Use concat()

How to use multiple values in WHERE using JPA CriteriaBuilder

I have an example MyTable with 3 columns - id, common_id, creation_date, where common_id groups entries.
Now I would like to select using CriteriaBuilder all newest entries from each group (that is for each common_id get me latest creation_date).
In SQL the query would look like this:
select * from MyTable where (common_id, creation_date) in (select common_id, max(creation_date) from MyTable group by common_id)
Now I have tried to create the where predicate by writing something like (cb is CriteriaBuilder, root is a Root):
cb.array(root.get('common_id'), cb.max(root.get('creation_date')))
.in(
query.subquery(MyTable.class)
.select(cb.array(root.get('common_id'), cb.max(root.get('creation_date'))))
.groupBy(root.get('common_id')))
But unfortunately cb.array is not an Expression (it's a CompoundSelect), so I cannot use .in() on it.
Thanks for pointers!
could you create it using JPQL? As far that I know, that is not possible.
I looked at the Spect (4.6.16 Subqueries) and it talk about "simples select expression":
simple_select_clause ::= SELECT [DISTINCT] simple_select_expression
I believe that only one return is possible, if you look at the examples there you will not find anything like it.
You will need to use NativeQuery for it.

SQL rawQuery if contains

A quick question, I can't find it on the internet, probably I am looking at the wrong "term".
But how do you make a SQL query statement with "if contains"
"SELECT something FROM " + TABLE_NAME + " WHERE name **contains** '*String*'", null);
I know you got these statements: = > < >= <= but what to use for if contains?
You want the LIKE keyword
Depending on the variety of SQL for the wildcard.
...Where name like '%string%'
SQLite uses % for a sequence of 0 or more unspecified characters (like a *), and an underscore _ for any single character
SELECT something FROM table WHERE name LIKE '%spam%'
(percent signs act like asterisks in a "conventional" search, give or take.)
You can get much more complicated with dbms specific functions (eg, here's Oracle's) that can, situationally, add regular expressions and other ways of searching.
As always, watch out for collation issues and case.
you can use LIKE
"SELECT something FROM " + TABLE_NAME + " WHERE name like '%yourstring%'";
refer to: http://www.sqlite.org/lang_expr.html
Instead of "if contains" you can use the "like" statement...
the query will be...
"SELECT something FROM tablename where name like %'string'%"
This will return what u want,,,
For Example:
Your table name "abc" contains Jarry,Jack,John,Josh,.....
if u run the query
select * from abc where name like %Ja%
it will return
Jarry,Jack..

Hibernate Detached Criteria

I have a DetachedCriteria which I am using to search a table based on a name field. I want to make the search case-insensitive, and am wondering if there is a way to do this without using HQL. Something like:
private void searchByFullName(DetachedCriteria criteria, String searchCriteria) {
criteria.add(Restrictions.like("fullName", "%" + searchCriteria.toUpperCase() + "%"));
criteria.addOrder(Order.asc("fullName"));
}
But I want to make sure that it will ignore the case when it does the search, so the SQL it generates should look something like:
SELECT * FROM PEOPLE WHERE ? LIKE toUpper(FULL_NAME);
there is ilike
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/criterion/Restrictions.html#ilike(java.lang.String,%20java.lang.Object)

Categories