I have to create this N1QL couchbase query in Spring data
select... LIKE "TASK:100:%"
where 100 is a parameter, but I don't know if it is possible
#Query("#{#n1ql.selectEntity} where META().id like \"TASK:$1%:\" ")
List<Task> findTasks(String taskId);
The correct syntax would be the following:
#Query("#{#n1ql.selectEntity} where META().id like ('TASK:' || $1 || '%') ")
List<Task> findTasks(String taskId);
Although I think you should concatenate the id on the backend instead of inside the query.
Related
I am trying to use postgresql jsonb operators with spring data jpa query as:
#Query(value="SELECT * from Employee e WHERE e.details #> '{\"province\":{\"city\":{\"town\": \":town\"}}, \"hobbies\": [\":hobby\"]}'",nativeQuery = true)
town and hobby are inputs.
There is no error but no result is returned, though records are there which meets the criteria
It seems parameter binding is not working.
What can be the solution?
Here, :town and :hobby is inside ''(single quote) means string literal, so parameter can't be replaced. You can use || to concat as string so that parameters are not inside in '' and can be replaced.
#Query(value="SELECT * from Employee e WHERE e.details #> ''||'{\"province\":{\"city\":{\"town\": \"' || :town || '\"}}, \"hobbies\": [\"' || :hobby || '\"]}'||'' ", nativeQuery = true)
This question already has an answer here:
Externalize query in spring boot application
(1 answer)
Closed 2 years ago.
I am using spring-data JpaRepository.
I have the following native query :
#Query(value = "SELECT SUBSTRING_INDEX(u.email, '#', -1) as domain, COUNT(*) as domainCount, r.invite_organization_id"
+ " FROM srs_users as u,srs_user_registrations as r where u.user_id=r.user_id and r.invite_organization_id=:orgId"
+ " GROUP BY "
+ "SUBSTRING_INDEX(u.email, '#', -1) ORDER BY domainCount DESC", nativeQuery = true)
List<Object[]> countTopDomain(#Param("orgId") String orgId );
Can we externalize the above native query in jpa-named-queries.properties just like other named queries.
You can use JPA Named queries. But I'm afraid that is not exactly what you are looking for. Other than that there is no support of externalizing SQL statements.
At resources create a folder META-INF.
At META-INF create a default file jpa-named-queries.properties
In this file put your query at any unique key. Say,
chk.test1=SELECT SUBSTRING_INDEX(u.email, '#', -1) as domain, COUNT(*) as domainCount, r.invite_organization_id FROM srs_users as u, srs_user_registrations as r where u.user_id=r.user_id and r.invite_organization_id=:orgId GROUP BY SUBSTRING_INDEX(u.email, '#', -1) ORDER BY domainCount DESC
To use this use below code:
#Query(name = "chk.test1", nativeQuery = true)
List<Object[]> countTopDomain(#Param("orgId") String orgId );
I have a table with an integer column. It has 12 records numbered 1000 to 1012. Remember, these are ints.
This query returns, as expected, 12 results:
select count(*) from proposals where qd_number::text like '%10%'
as does this:
SELECT COUNT(*) FROM "proposals" WHERE (lower(first_name) LIKE '%10%' OR qd_number::text LIKE '%10%' )
but this query returns 2 records:
SELECT COUNT(*) FROM "proposals" WHERE (lower(first_name) || ' ' || qd_number::text LIKE '%10%' )
which implies using || in concatenated where expressions is not equivalent to using OR. Is that correct or am I missing something else here?
You probably have nulls in first_name. For these records (lower(first_name) || ' ' || qd_number::text results in null, so you don't find the numbers any longer.
using || in concatenated where expressions is not equivalent to using ORIs that correct or am I missing something else here?
That is correct.
|| is the string concatenation operator in SQL, not the OR operator.
So the query below is probably not the most efficient, buy still, I am wondering why it is returning no result, even though the SQL counterpart does. There is no error, I am just getting no result. Is it maybe not the correct equivalent for the query I wrote in MySQL?
This is the JPA JPQL.
Query query = em.createQuery("SELECT sub FROM Subscription sub WHERE "
+ "sub.isSuspended = 0 AND "
+ "(SELECT i FROM Invoice i WHERE i.dateDue < CURRENT_DATE AND i.datePaid IS NULL "
+ "GROUP BY i HAVING COUNT(i.idInvoice) > 2) MEMBER OF sub.invoices");
And this is the SQL from MySQL.
SELECT * from subscription
WHERE subscription.is_suspended = 0 AND id_subscription IN
(SELECT id_subscription FROM invoice
WHERE date_due < CURDATE() AND date_paid IS NULL
GROUP BY id_subscription
HAVING COUNT(*) > 2)
The two queries are not the same. To use the actual query use the NativeQuery createNativeQuery() instead of Query.
In your case the JPA version seems to have syntax errors.
After the AND you are missing the IN operator.
In the nested query you are selecting i instead of something like i.idInvoice
The JPA query should look like
SELECT sub FROM Subscription sub
WHERE sub.isSuspended = 0
AND sub.idSubscription IN
(SELECT i.idInvoice
FROM Invoice i
WHERE i.dateDue < CURRENT_DATE AND i.datePaid IS NULL
GROUP BY i.idInvoice
HAVING COUNT(i.idInvoice) > 2);
how can I solve the following problem. At the moment I implement an query, which search in db for string in rows. It works great, I can pageing the result in frontend but I can't sort. If I call the following url and add the sort params as url parameter It no sort my result.
How I can solve the problem
web/api/orders/search/findSearch?page=0&sort=number,asc&searchText=hello&size=25
#Query("SELECT o FROM Order o where"+
"(lower(o.number) like('%' || lower(:searchText) || '%') " +
"or lower(o.name) like('%' || lower(:searchText) || '%') ) " +
"or lower(o.created) like('%' || lower(:searchText) || '%') " +)
Page<Order> findSearch(#Param("searchText") String searchText, Pageable pageable);