Spring custom query escape : - spring

I have a custom query in Spring which look's like this
#Query(value = "select s.* from (select #p1:= :profileId p) parm , v_source_result_average_by_profile s;", nativeQuery = true)
The problem is that spring thinks after the first : there is a parameter but there isn't. Is there a way to escape the character?

Solved by just adding \\ like this
#Query(value = "select s.* from (select #p1\\:= :profileId p) parm , v_source_result_average_by_profile s;", nativeQuery = true)

Related

Optimal way to achieve setmaxresults in springdatajpa with hql query

Since limit is not supported in #query of spring, we need alternative way for this other than pagination query param
#Query(" select something so where so.id>50 order by so.created desc")
#QueryHints(#QueryHint(name = "org.hibernate.annotations.FETCH_SIZE" , value = "1"))
optionam<something> findlatestSomething();
#Query(" select something s where s.id = select max(so.id) something so order by so.id desc")
optionam<something> findlatestSomething();

Spring JPA native subquery is not working , returning zero record

i am trying to execute below sub query in spring jpa, but it is returning 0 records, but same query is returning records in this link fiddle
#Query(value = " SELECT id FROM (SELECT *, SUM(total_quantity) OVER (ORDER BY created_dttm DESC) "
+ "- total_quantity AS tq_sum FROM t) sub_query WHERE type= ?1 and tq_sum < ?2", nativeQuery = true)
public List<LubricantStock> findAllLubeStockForLubeTypeAndTotalQuantity(int lube_type_id, double total_quatity);
is there any limitations using subquery in spring jpa

cant using "LIKE" in native query spring boot

I try to get orders of user by query :
#Query(value = "SELECT * FROM ORDERS WHERE USER_ID = ?1 AND CAST(CREATE_AT AS NVARCHAR(100)) LIKE ?2 OR CAST(GRAND_TOTAL AS NVARCHAR(100)) LIKE ?2 OR CAST(STATUS AS NVARCHAR(100)) LIKE ?2" , nativeQuery = true)
Page<Order> getOrdersByUserSearch(int userID, String searchS, Pageable pageable);
But it always return empty list. i run this code in SQL server and it work (?1 =2. ?2 = '2021-06-26').
If I try to change "NOT LIKE" instead of "LIKE" It run.
I dont want using query ( not native), Named query or specification method because it get more error.
Any advice?.
SQL like query requires % along with the value for a match. In case of ordered parameters in queries we can use:
#Query("SELECT m FROM Movie m WHERE m.rating LIKE ?1%")
List<Movie> searchByRatingStartsWith(String rating);
Click here for more info.
In your case the query string should be like this:
SELECT * FROM ORDERS WHERE USER_ID = ?1 AND CAST(CREATE_AT AS NVARCHAR(100)) LIKE %?2% OR CAST(GRAND_TOTAL AS NVARCHAR(100)) LIKE %?2% OR CAST(STATUS AS NVARCHAR(100)) LIKE %?2%

How to create a query for a known value on a known field, with a known date between 2 unknown dates)

The task is to find a known value in the specified field. Condition: There is a system date or a date that I want to use. This date must be between 2 dates ( the fields where the dates are known), that is, you need to check all the dates in the database, + additional condition:
it is necessary that the found string had in itself a previously known field marked IS NULL
The code written in Sql, below working.
select b.* from nc_proc b where b.code_proc = 'S.2.2' and b.date_on >= '12-AUG-19' and b.d_off IS NULL and b.date_off > '12-AUG-19';
'12-AUG-19' is a variable that is sent to me(more precisely, I form it from the system time)
'S. 2. 2' is a variable that I get and which is the main search.
How to make a request ?
here is an example that tried to make
#Query(value = "SELECT b.* FROM nc_proc b WHERE b.code_proc = ?1 and b.date_on > ?2 and b.d_off IS NULL and b.date_off > ?2 ", nativeQuery = true)
List <NcProc> findByCodeProcList2 (String value, String systemDate);
This don't work.
Please tell me how to fix. And is it possible not to use the query manually, but to do with the built-in keywords spring data ? Then this option would be better.
Update
#Query(value = "SELECT b.* FROM nc_proc b WHERE b.code_proc = ?1 and ?2 b.date_on BETWEEN b.date_off and b.d_off IS NULL", nativeQuery = true)
List <NcProc> findByCodeProcList2 (String value, String systemDate);
Hibernate: SELECT b.* FROM nc_proc b WHERE b.code_proc = ? and ?
b.date_on BETWEEN b.date_off and b.d_off IS NULL
11:02:58:871 WARN http-nio-8080-exec-1
o.h.e.j.s.SqlExceptionHelper:137 - SQL Error: 920, SQLState: 42000
19-сентября-20 11:02:58:871 ERROR http-nio-8080-exec-1
o.h.e.j.s.SqlExceptionHelper:142 - ORA-00920: invalid relational operator
In addition, I was told that BETWEEN includes the left and right restriction, and it is necessary that the desired date was in the specified interval and is not equal to the left and right restriction.
Update
it's work
#Query(value = "select * from nc_proc where code_proc = ?1 " +
"and date_on <= TO_DATE(?2, 'yyyy-mm-dd HH24:MI:SS') " +
"and TO_DATE(?2, 'yyyy-mm-dd HH24:MI:SS ') < date_off " +
"and d_off IS NULL",
nativeQuery = true)
NcProc findByCodeProc(String codeProc, String systemDate);
I wanted not to use the native record in the future.
Who knows how to make an expression from the keywords spring-data ?
I can't help on the Spring side, but for your queries I think you want something like:
SELECT b.* FROM nc_proc b WHERE b.code_proc = ?1 AND b.date_on > ?2 AND b.date_off < ?2 AND b.d_off IS NULL
It's unclear from your question whether the dates go b.date_on > '12-AUG-19' > b.date_off or the other way around, b.date_on < '12-AUG-19' < b.date_off. So you might have to switch the > < operators.

jpql native query not setting parameter

#Repository
public interface GroupRepository extends JpaRepository<Group, String> {
//Other queries....
#Query(value = "with cte(group_id, parent_group_id, group_name) as( "
+ "select group_id, parent_group_id, group_name "
+ "from hea.hea_group "
+ "where group_id = ?1 "
+ "union all "
+ "select g.group_id, g.parent_group_id, g.group_name "
+ "from hea.hea_group g "
+ "inner join cte on cte.group_id = g.parent_group_id "
+ "where g.parent_group_id is not null "
+ ") select * from cte", nativeQuery = true)
List<Object> getChildGroups(String groupId);
}
Above is the query that I have written that should return the parent group and all of its children. The query does what it is suppose to do when I replace the ?1 with a hard coded group id value and change the method to have no parameters, but when I try to run it as above it returns nothing even though I'm passing in the exact same value that I was hard coding.
Below is the sql that is being generated by the query. When I replace the ? with a group id an run it on a test database it returns the results that it should.
with cte(group_id, parent_group_id, group_name) as( select
group_id,
parent_group_id,
group_name
from
hea.hea_group
where
group_id = ?
union
all select
g.group_id,
g.parent_group_id,
g.group_name
from
hea.hea_group g
inner join
cte
on cte.group_id = g.parent_group_id
where
g.parent_group_id is not null ) select
*
from
cte
The variables are zero based so ?0 is what you should use.

Resources