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)
Related
i'm trying to develop a webapp where users post their ads, I'm currently working on the research an ad section, it's just a form that sends a search model (it has a string which is the text written on the search bar and some non-required filters) to the backend (spring boot), the backend gets the string and gives it to a JpaRepository method that should do a query on my oracle database and give me back the result set as a list of AdEntity, only problem is the list is always empty, this is the method:
#Query(value = "select distinct a.ann_id, a.nome, a.autore, a.descrizione, a.prodotto, a.prezzo "
+ "from annunci a, prodotti p "
+ "where a.prodotto = p.prod_id "
+ "and (a.nome like '%:main%' or p.nome like '%:main%')", nativeQuery = true)
public List<AnnuncioEntity> searchWithoutFilters(String main);
and this is where I call it:
#PostMapping("/")
public List<FullAnnuncio> searchAnnunci(#RequestBody SearchEntity search) {
List<AnnuncioEntity> li = annuncioDAO.searchWithoutFilters(search.getMain());
return null;
}
The li list is always empty (checked with debugger) and I have no idea how to fix it, I'm sure the query works and produces the expected resultSet as I tried it on sqlDeveloper first, and no the string I get from search isn't null
Help pls I'm new to these things as I'm not even a junior programmer lmao
Done, the Like clause on jpa repository kinda works like dodoo so I had to write it like this:
LIKE CONCAT(CONCAT('%', :main), '%')
(the sintax is correct only with oracle databases tho so be careful, for mySql and others write it like this:
LIKE CONCAT('%', :param, '%')
thanks #Arnaud
Try changing the query ":main" to "?1" in both occurrences
I have a list of different ids and their names. For every id[0] we have name[0] that needs to be matched.
list of ids, l{1,2,3,4};
list of names, n{a,b,c,d};
Now suppose if I want to get an exact match for both above combination, is there any way in HQL to get the result?
I am looking to find a replacement for a query like:
select any_column
from table_name
where (id[0]=1 and name[0]=a) or (id[1]=2 and name[1]=b and so on...);
The HQL query should be something like below:
select any_column
from table_name
where (id,name) IN {(id[0],name[0]), (id[1], name[1]),...};
Any suggestions?
I am not hql/sql guy, however one way I can think of is, in your hql, you concatenate the id and name with a space (or other special char) then with in sub-clause. something like:
select * from table where concat(id, ' ', name) in (:pairList)
The pairList parameter is a java collection, you should prepare before the hql query call, which has element id[x] + " " + name[x].
I think this should work.
If you are using hibernate, another possible solution is, make use of the hibernate's #Formular annotation on your table entity, to create a calculated column, such as:
#Formula(value = " concat(id, ' ', name) ")
private String idNamePair;
Then you can in hql use it as a normal field. like ... from table where idNamePair in (:paramList)
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()
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%'";
I'm having some small trouble trying to dynamically generate an SQL SELECT statement generated from an entry from a webpage.
Basically i have a small search engine in the website, it accepts three parameters (price,city,brand), so i have a JavaBean built in the same way with only 3 attributes, the same ones.
When performing a SELECT on the database i could make it this way using PreparedStatement
String sql=select * from product where price<=? and city=? and brand=?;
prep=conn.prepareStatement(sql);
prep.setDouble(1,price);
prep.setString(2,city);
prep.setString(3,brand);
where prep is of course a PreparedStatement obj
My problem comes when the user does not insert a parameter in the field of the web page, because apparently he is not interested on a limitation.
I need to find a way to "cut" the entry at runtime, like:
city field=""? then take out city from the search criteria:
String sql=select * from product where price<=? and brand=?;
One way i could do that is filling in the code in the servlet with else-if but thats not the only solution i'm sure.
Another potential solution would be to use IFNULL(expr1,expr2) function of SQL but in case is null, then i should just remove the null-field from the query.
Do you have any advice?
i hope its clear enough the way i explained it.
You can use OR in your WHERE clause to do something like:
WHERE (Price<=? OR ? IS NULL)
AND (City = ? OR ? IS NULL)
AND (Brand = ? OR ? IS NULL)
Try to replace condition "=" with Like '%[input_value]%'. In this way if uset left blank field you don't have any restriction.
You can use like for strings.
String sql = "SELECT * FROM product WHERE price <= ? AND city like '?' AND brand like '?'";
prep = conn.prepareStatement(sql);
prep.setDouble(1, price);
prep.setString(2, city);
prep.setString(3, brand);
See Exact match with sql like and the bind
And in case city/brand is not selected, then set it
prep.setString(2, "%");
prep.setString(3, "%");
If price is not selected, then use some very big number.