Spring DATA JPA support subquery in from ?
like
select * from (
select unique_id
from table_a
) as from_a
inner join table_b
on from_a.unique_id = table_b.unique_id
querydsl JPASubquery or Spring DATA criteria impossible?
I think it is noe possible.
in other way. you use JPAExpressions. it will be serching for subquery in query dsl.
But i aleardy user that subQuery that are result only one table.
Related
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
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.
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.
I need to implement sql query like:
SELECT * FROM (SELECT a FROM b WHERE a.z = 1) WHERE rownum <=1;
How can I write such statement with QueryDSL (I am not using JPA and JDO - only clean sql)?
Querydsl SQL emulates paging of all the supports databases, so you can write directly
query.from(a)
.where(a.z.eq(1))
.limit(1)
.list(a);
If you need to write this via a subquery then like this
query.from(
new SQLSubQuery().from(a).where(a.z.eq(1)).list(a).as(a))
.where(rownum.loe(1))
.list(a);
I wrote the following query for MySQL:
SELECT subquery.t1_column1,
subquery.t2_id,
MAX(subquery.val)
FROM (
SELECT t1.column1 as t1_column1,
t1.id_t2 AS t2_id,
count(1) AS val
FROM table1 t1
INNER JOIN table2 t2
ON t2.id = t1.id_t2
GROUP BY t1.id_t2
) subquery
GROUP BY t1_column1
And I'd like to translate it into JPA (JPQL or criteria query).
I don't know how to make this max(count) thing, and JPA doesn't seem to like the SELECT FROM SELECT...
If anyone has an idea other than native queries (I'll do it for now), it would be great.
I haven't checked tha JPA specification, but given that the Hibernate documentation says
Note that HQL subqueries can occur only in the select or where
clauses.
I very much doubt that your query can be transformed in a valid JPQL query.
You'll have to keep using this native SQL query.
JPA 2.0 JPQL does not support sub-selects in the from clause. You may want to try to rewrite your query, or use a native SQL query.
EclipseLink 2.4 will support sub-selects in the FROM clause,
see,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause