How to write join fetch query with jooq? - java

Anyone knows how to write join fetch query with jooq ?
my code :
int oid=5;
Select<?> selectQuery = DSL_CONTEXT.select().from(TABLE_A)
.join(TABLE_B).on(TABLE_A.PID.eq(TABLE_B.ID))
.where(TABLE_A.OID.eq(oid))
.orderBy(UPDATED.asc(), ID.asc())
.seekAfter(val(offsetDateTime), val(id))
.limit(50);
this results in : select * from table_a join table_b type of query. How to make it create a query of following type :
select * from table_a join fetch table b ...
Any help is appreciated.

The closest you can get with jOOQ 3.11 out of the box functionality is using ResultQuery.fetchGroups(). On your query, call:
Map<TableARecord, Result<TableBRecord>> result = select.fetchGroups(TABLE_A, TABLE_B);

Related

pagination and naive query + eclipse link

I am trying to write a native query and pagination in eclipse link + JPA for PostgreSQL
but I'm getting exceptions :
Here is the code what I tried :
#Query(nativeQuery = true, value= "select a.* from rqst a LEFT OUTER JOIN table_b b ON a.id= b.id where a.code='abc' ORDER BY /*#pagable*/")
#Query(nativeQuery = true, value= "select a.* from rqst a LEFT OUTER JOIN table_b b ON a.id= b.id where a.code='abc' ORDER BY ?#{#pageable}")
and I followed this link but no luck :
Spring Data and Native Query with pagination
any help appreciated
when I use second query I got this exception :
Can't infer the SQL type to use for an instance of org.springframework.data.domain.PageRequest. Use setObject() with an explicit Types value to specify the type to use.
at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:1039) ~[postgresql-9.4.1208.jar:9.4.1208]

Convert sql query to hql

SELECT recipe.name,SUM(salesdetails.quantity::integer),recipe.price As Quantity
FROM (salesinfo JOIN salesdetails ON salesinfo.sessionid=salesdetails.salesinfo_sessionid)
JOIN recipe ON salesdetails.recipe_id=recipe.id group by salesdetails.recipe_id,recipe.name,recipe.price
ORDER BY SUM(salesdetails.quantity::integer) DESC;
Can anyone give me the hql query for this?
If you are not acquainted with the HQL and want to use the same query ,then you can do it using the native query feature of the Hibernate like this :
#QUERY(value="SELECT recipe.name, SUM(salesdetails.quantity::
INTEGER),recipe.price AS Quantity
FROM (salesinfo
JOIN salesdetails ON salesinfo.sessionid=salesdetails.salesinfo_sessionid)
JOIN recipe ON salesdetails.recipe_id=recipe.id
GROUP BY salesdetails.recipe_id,recipe.name,recipe.price
ORDER BY SUM(salesdetails.quantity:: INTEGER) DESC", nativeQuery = TRUE)
But I would recommend that you first try to convert the same in into HQL query and then if any issue then you should ask it here instead of directly asking for the converted query. Meanwhile this can help.

Convert SQL to hibernate query using criteria and/or projection (subqueries)

Could anyone please help to convert this SQL Query to HQL (using criteria and projection) to have in result Set<topic> topics.
SELECT t.id, t.title, COUNT(p.id) as count, MAX(p.created_on) as `latest`
FROM topics t
JOIN posts p
ON t.id = p.topic_id
GROUP BY t.id
ORDER BY count ASC;
My problem is that Hibernate is fetching all data connected to one of columns of the topic table (connected table is users) - and query takes to much time and unnecessary selects and joins.
Tried to use projections but couldn't.
Thanks in advance for any help!
From your code,
SELECT t.id, t.title, COUNT(p.id) as count, MAX(p.created_on) as `latest`
FROM topics t
JOIN posts p
ON t.id = p.topic_id
GROUP BY t.id
ORDER BY count ASC;
You can remove JOIN and convert it in WHERE condition. Hope it will save you.
Use this following code
SELECT t.id, t.title, COUNT(p.id) as count, MAX(p.created_on) as `latest`
FROM topics t, posts p
WHERE t.id = p.topic_id
GROUP BY t.id
ORDER BY count ASC;

Hibernate takes more time for sql query to execute than in oracle sql developer

I have query that takes few milliseconds to execute in oracle sql developer and same query takes around 2 mins through hibernate. I set show_sql = true but did not find any clue.
Below are the details -
Query -
select field1 from table1 t1
join table2 t2 on t1.id = t2.id
where t2.status = 'SomeValue';
Hibernate query -
Criteria criteria = getSession().createCriteria(Table1ClassName.class);
criteria.createAlias("table2", "ts", CriteriaSpecification.INNER_JOIN);
criteria.add(Restrictions.eq("ts.status", "SomeValue"));
criteria.setProjection(Property.forName("field1"));
return criteria.list();
Generated sql -
select
this_.field1 as y0_
from
table1 this_
inner join
table2 ts1_
on this_.ID=ts1_.ID
where
hs1_.STATUS='SomeValue';
The query returns approximately 5000 records. Is it the case that converting data into java objects takes time.
Any help is appreciated..

SQL subquery in Hibernate Query Language

I am new to HQL and I am working on subqueries.
I have the following SQL subquery:
select * from (
select * from table order by columnname
) as subquery
where columnvalue = 'somevalue';
I want to fire the query in HQL. I wrote the below code :
Result = session.createQuery("from (from table order by columnname) as subquery where columnvalue = :somevalue")
.setParameter(/*setting all parameters*/)
.list();
I am getting this exception:
QuerySyntaxException : unexpedted token :( line 1, column 10 [from (from ...)]
My SQL query is giving me correct results. How do I write it in HQL ?
I did not think HQL could do subqueries in the from clause
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries
note the sentence:
Note that HQL subqueries can occur only in the select or where clauses.
It will be better if you excute native SQL.

Categories