Querydsl limit record example - spring-data-jpa

I need to select limit record by querydsl, my query is like:
select *from <table> where <column_id> = 5 limit 2
How can I do a query in my repository interface method to the given above query?

Use QueryDslPredicateExecutor.findAll(Predicate predicate, Pageable pageable) and then get the actual results back using PageRequest.of(0, limit).

Related

JPA - fetch data from db adding text[] column in where condition and checking against an input array

I have a spring boot java application which uses postgresql has a table with text[] column 'groups'. Let the values of the column
Groups(text[])
Group1,Group2
Group2
Group1
Group3
My input value will be an arraylist of Group1,Group2 and I need to add this to the where condition in query and fetch the data. The return data must be having the first 3 rows.
Please help in writing a query using JPA criteria and Predicate and all.
I am known about query but here I need it be created using CriteriaBuilder and Predicate
the query I found is like this
select * from table where exists (select * from unnest(groups) n where n like any (array['Group1','Group2']));
I need to change this in to java code which uses criteria and predicate
Thanks in Advance

How to do 'select count(distinct column) from table' with jpa query method?

Is there a way to do select count(distinct column1) from table with jpa query method not with query annotation?
I know I can do select count(distinct column1) from table where column2 = :column2 by long countDistinctColumn1ByColumn2(:column2).
I've searched official documents and tried some tests but couldn't find a way, thanks in advance.
This is not supported by Spring Data JPA.
The documentation can be found here https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods

NativeSql query into JPQL query convertion

I have native query like this
SELECT sample.api_name,
sample.hitcount,
r.unit_rate*sample.hitcount AS amnt
FROM
(SELECT u.api_name AS api_name,u.tenant_id,
u.count AS hitcount
FROM tableA u
WHERE u.tenant_id = :tenant
AND u.time_stamp BETWEEN :dateFrom AND :dateTo
GROUP BY u.api_name,
u.tenant_id) AS sample
LEFT JOIN tableB r ON sample.api_name = r.api_name
AND sample.tenant_id =r.tenant_id
The values "tenant,dateFrom,dateTo" values are providing by using #Param tag.
I have tried to convert to the jpql query in spring data jpa.
But by seeing in some reference documents i came to know that the
Jpql inner queries are not supported in the FROM clause.
is that my point correct or not. If not Please help me to write in JPQL if my point is correct please help to write in Criteria api.

Subquery in FROM clause using CriteriaQuery

I need to implement select from a nested query, for the sake of simplicity let it be:
select * from (select * from city) c
How can this be done using CriteriaQuery and Subquery?
You can't because (select * from city) is not an entity and CritieriaAPI queries on entities not on tables.
The only way would be to create a view for (select * from city) and map that view to an entity.
First, your syntax is not JPA compliant as mentioned previously, secondly, you must know that subqueries in JPQL are allowed only in WHERE and HAVING clauses. The same applies to Criteria queries. If you need the more powerful SQL subquery features then with JPA you have to use JPA provisions for native queries, or otherwise, use another type of JDBC library.

Why my select query returns "more than 5000 records"?

I have t_users class with the following schema:
name (string)
uid (long)
I have around 100K records for that class and when I want to get a specific user using its id, Orientdb tells me that my query returns more thant 5K records, so it returns nothing.
Here is my query:
SELECT FROM t_users where uid = 123456
You can try and return the data by using limits and inspect the data returned to try and conclude an answer.
Documentation - Limiting
SELECT FROM <target> [WHERE ...] SKIP <records-to-skip> LIMIT <max-records>