I'm trying to do something like this using native query:
#Query(value = "select * from systems where UPPER(system_name) like UPPER('%?1%') ORDER BY system_name LIMIT '?2'", nativeQuery = true)
List<System> findAllBySystemNameWithLimit(String systemName, int limit);
But it doesn't work. Is it possible to use limit as parameter? Thanks for answer.
When you use ?1 or :paramname you don't have to use them between two quotes like you do '%?1%' and '?2' so instead your query should look like :
select * from systems
where UPPER(system_name) like UPPER(CONCAT('%', ?1, '%')) ORDER BY system_name LIMIT ?2
//------------------------------^-----^-----^----^---^-------------------------------^^
Related
I have situation in which I have to compose a JPQL query in some method, then pass this query to spring data query method to be used as the query in #Query annotation.
#Query(value = ":DyanamicQuery")
List<PrizeInsuranceConfiguration> filterConfigurPrizeInsurance(String DyanamicQuery);
This is not possible. As it throws compile error. Can you give me an alternat way to achieve this?
#Query(value = ":DyanamicQuery")
List<PrizeInsuranceConfiguration> filterConfigurPrizeInsurance(String DyanamicQuery);
expected to get Query that I made will go and sit at value = ":DyanamicQuery" I also tried giving #Query("#{#entityName}").
You can create a JPQL query as a string, but you cannot pass it as a parameter to the value attribute of the #Query annotation.
You can use below example -
String dyanamicQuery= "SELECT * FROM table_name WHERE condition= :condition";
#Query(value = dyanamicQuery)
List<Entity> findByField(#Param("condition") String condition);
Or you can use below example also -
#Query(value = "SELECT * FROM table_name WHERE condition= :condition")
List filterConfigurPrizeInsuranceByField(#Param("condition") String condition);
I've read the documentation on inserting method parameters into queries and other questions however they all suggest that this should work.
#Query(value = "SELECT * FROM period WHERE time LIKE \":day%\" AND id IN (SELECT id FROM booking)", nativeQuery = true)
List<Booking> findAllBookingsOnDay(#Param("day") String day);
Results in:
Hibernate: SELECT * FROM period WHERE time LIKE ":day%" AND id IN (SELECT id FROM booking)
I've tried removing quotations and percentage but that just results in.
Hibernate: SELECT * FROM period WHERE time LIKE ? AND id IN (SELECT id FROM booking)
The correct way to do this is to just use an unescaped placeholder in the query, and the bind the LIKE wildcard expression to it from the Java side.
#Query(value = "SELECT * FROM period WHERE time LIKE :day AND id IN (SELECT id FROM booking)", nativeQuery =true)
List<Booking> findAllBookingsOnDay(#Param("day") String day);
Usage:
String day = "%friday%"; // or whatever value would make sense here
findAllBookingsOnDay(day);
I am trying to do a simple search function by multiple words, like this in SQL query
SELECT * FROM faqs WHERE title REGEXP 'préférée|Changer|endommagé' or question REGEXP 'préférée|Changer|endommagé'
but when I tried to implement this in #Query in my JpaRepository like this
#Query(value = "SELECT f FROM Faq f WHERE f.title REGEXP :term" )
Page<Faq> searchByRegExp(#Param("term") String term,Pageable pageable);
But it seems that REGEXP isnt supported because of this error:
<expression>, <operator>, GROUP, HAVING or ORDER expected, got 'REGEXP'
Note: I tried adding nativeQuery = true - same issue:
#Query(value = "SELECT * FROM faqs WHERE title REGEXP :term ", nativeQuery = true)
Page<Faq> searchByRegExp(#Param("term") String term,Pageable pageable);
Unfortunately jpql does not support regular expressions. You would have to use like and write all the possibilities. But in your case you can simply use in:
#Query(value = "SELECT f FROM Faq f WHERE f.title in (:terms)" )
Page<Faq> searchByRegExp(#Param("terms") List<String> terms, Pageable pageable);
And as the first parameter pass the possibilities: List.of("préférée", "Changer", "endommagé"). For more complex regular expressions this list would grow considerably, but in this case only 3 values are possible.
The other way is using nativeQuery and sql:
#Query(value = "SELECT f.* FROM faqs f WHERE (f.title REGEXP :term)", nativeQuery = true)
Page<Faq> searchByRegExp(#Param("term") String term,Pageable pageable);
I know that I can write so:
Query query = session.createSQLQuery(
"select s.stock_code from stock s where s.stock_code = :stockCode")
.setParameter("stockCode", "7277");
List result = query.list();
How I must do if I use list values
select count(*) from skill where skill.id in (1,2,4)
I want replace hardcode values.
Maybe:
Query query = session.createSQLQuery("select count(*) from skill where skill.id in :ids")
.setParameter("ids", Arrays.asList(1,2,4));
Query interface have setParameterList(List<any>) function to set the value in IN Clause in HQL. But in HQL IN Clause have a limit to set the element. If the limit is exceed, memory overflow exception occur.
Have you tried something like this?
Query query = session.createSQLQuery(
"select s.stock_code from stock s where s.stock_code in (:stockCodes)")
.setParameter("stockCodes", "1,2,4");
Does it work for you?
I wonder if there is a good solution to build a JPQL query (my query is too "expressive" and i cannot use Criteria) based on a filter.
Something like:
query = "Select from Ent"
if(parameter!=null){
query += "WHERE field=:parameter"
}
if(parameter2!=null) {
query += "WHERE field2=:parameter2"
}
But i would write WHERE twice!! and the casuistic explodes as the number of parameter increases. Because none or all could be null eventually.
Any hint to build these queries based on filters on a proper way?
select * from Ent
where (field1 = :parameter1 or :parameter1 is null)
and (field2 = :parameter2 or :parameter2 is null)
Why can't you use a criteria, like this.
Other options (less good imho):
Create two named queries one for each condition, then call the respective query.
Or build up a string and use a native query.
Oh, do you just mean the string formation(?) :
query = "Select from Ent where 1=1 "
if(parameter!=null){
query += " and field=:parameter"
}
if(parameter2!=null) {
query += " and field2=:parameter2"
}
(I think that string formation is ugly, but it seemed to be what was asked for)