Convert sql query to hql - java

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.

Related

How to write join fetch query with jooq?

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);

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;

How to count all groups returned by a group by query in JPQL?

If I have a group by query
Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...
How can I count the number of rows not using NativeQuery?
I need to do something like this:
select count(*) from
(Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...)
Is there any way to do with JPQL?
in oracle you can this hql
Select sum(count(*) - count(*) + 1)
from Entity e
group by e.field1, e.field2...
but dose not work in postgres
Derived tables are not supported in JPQL, so, the best way to do it is to run a native SQL query.
After all, there is a very good reason why both JPA and Hibernate offer support for SQL queries, don't you agree?

JPA2 and criteria queries: subqueries with group functions

I have a native SQL query I would like to express with a JPA2 criteria query.
This query already works, but I haven't been able to get it using just JPA2.
I think mostly because I lack experience on this.
I paste the SQL here, maybe you experts can help me to figure it out:
SELECT * FROM
OWNING_POINTS o1
JOIN
(SELECT conversationId, count(*) as nOfMsg
FROM OWNING_POINTS
GROUP BY conversationId) as o2
ON o1.conversationId = o2.conversationId
LEFT JOIN
(SELECT id, created as newerUnreadMessage
FROM OWNING_POINTS
WHERE `read`=0) as o3
ON o1.id = o3.id " +
WHERE o1.owner_user_id=?
ORDER BY newerUnreadMessage DESC, nOfMsg DESC
Thank you very much.

HQL/SQL/Criteria to join-match all records in a given list while selecting all fields

I'm trying to write a HQL/Criteria/Native SQL query that will return all Employees that are assigned to a list of Projects. They must be assigned to all Projects in order to be selected.
An acceptable way of achieving this with native SQL can be found in the answer to this question: T-SQL - How to write query to get records that match ALL records in a many to many join:
SELECT e.id
FROM employee e
INNER JOIN proj_assignment a
ON e.id = a.emp_id and a.proj_id IN ([list of project ids])
GROUP BY e.id
HAVING COUNT(*) = [size of list of project ids]
However, I want to select all fields of Employee (e.*). It's not possible to define SQL grouping by all the columns(GROUP BY e.*), DISTINCT should be used instead. Is there a way to use DISTINCT altogether with COUNT(*) to achieve what I want?
I've also tried using HQL to perform this query. The Employee and ProjectAssignment classes don't have an association, so it's not possible to use Criteria to join them. I use a cross join because it's the way to perform a Join without association in HQL. So, my HQL looks like
select emp from Employee emp, ProjectAssignment pa
where emp.id = pa.empId and pa.paId IN :list
group by emp having count(*) = :listSize
However, due to a bug in Hibernate, GROUP BY entity does not work. The SQL it outputs is something like group by (emptable.id).
Subquerying the assignment table for each project (dynamically adding and exists (select 1 from proj_assignment pa where pa.emp_id=e.id and pa.proj_id = [anId]) for each project in the list) is not an acceptable option.
Is there a way to write this query properly, preferrably in HQL (in the end I want a List<Employee>), without modifying mappings and without explicitly selecting all columns in the native SQL ?
EDIT: I'm using Oracle 10g and hibernate-annotations-3.3.1.GA
How about:
select * from employee x where x.id in(
SELECT e.id
FROM employee e
INNER JOIN proj_assignment a
ON e.id = a.emp_id and a.proj_id IN ([list of project ids])
GROUP BY e.id
HAVING COUNT(*) = [size of list of project ids]
)
I've found an alternative way to achieve this in HQL, it's far more inefficient than what I'd like, (and than what is really possible without that nasty bug) but at least it works. It's better than repeating subselects for each project like
and exists (select 1 from project_assignment pa where pa.id = someId and pa.emp_id = e.id)
It consists of performing a self-join subquery in order to find out, for each of the Employees, how many of the projects in the list they are assigned to, and restrict results to only those that are in all of them.
select e
from Employee
where :listSize =
(select distinct count(*)
from Employee e2, ProjectAssignment pa
where
e2.id = pa.id_emp and
e.id = e2.id
and pa.proj_id IN :projectIdList
)

Categories