Hibernate error: unexpected AST node - java

I need to sort topics by latest post. Could someone please help me with this hibernate query:
unexpected AST node: query
[SELECT t
FROM Topic t
ORDER BY
(SELECT MAX(p.createdOn) FROM Post p WHERE p.topic.id = t.id)
DESC]
What is the issue here?

SELECT topic FROM Post ORDER BY createdOn

I think that this JPQL query should work
select t.id, t.description, max(p.createdOn) as maxCreationTime
from Topic t inner join t.posts p
group by t.id, t.description
order by maxCreationTime
If you want to include topics that have not related posts.
select t.id, t.description, max(p.createdOn) as maxCreationTime
from Topic t left join t.posts p
group by t.id, t.description
order by maxCreationTime
You have to specify the selected fields from Topic in order to make the aggregation function max work. Hope this helps.

Related

HQL - COALESCE or CASE on LEFT JOIN

I have 4 tables:
job
client
order
client
client
state
state
name
I need to get a client's state name from a job OR order. If job is not null get from job, else get from order.
I tried something like this:
LEFT JOIN job.client jc
LEFT JOIN order.client oc
LEFT JOIN COALESCE(jc.state, oc.state) clientState
but I get an unexpected token: COALESCE exception.
I also tried:
LEFT JOIN job.client jc
LEFT JOIN order.client oc
LEFT JOIN CASE WHEN job IS NOT NULL THEN jc.state ELSE oc.state END clientState
but I get an unexpected token: CASE exception.
Any idea on how to solve this? Should I try multiple JOINS with state table (jc.state and oc.state) and use the CASE in the projection? Isn't there an easier way?
Thanks in advance
Extra info:
This query could be solved like the example below. My main question is if there is a better way of doing this:
SELECT CASE WHEN jcClientState IS NOT NULL THEN jcClientState.code ELSE ocClientState.code END
FROM job job
LEFT JOIN anotherTable anotherTable
LEFT JOIN job.client jc
LEFT JOIN anotherTable.order oc
LEFT JOIN jc.state jcClientState
LEFT JOIN oc.state ocClientState
Assuming HQL supports coalesce (appears as such when doing a quick search), then you can use coalesce like this:
select coalesce(table1.state, table2.state, 'unknown') as state
from table1
left join table 2
on table2.id = table1.id
The coalesce will grab the first non-null value.

Query dsl - How to write having clause

I am new to Java and Querydsl. I have searched so much to get how to write the below query in query dsl. end up with nothing.Can any one please help.
SELECT * FROM `library_user_product` GROUP by user_id HAVING max(product_id)
Thanks in advance
Use this link it has examples for the group by, having and other function in respect to JPA
sample:
SELECT c.currency, SUM(c.population) FROM Country c
WHERE 'Europe' MEMBER OF c.continents
GROUP BY c.currency
HAVING COUNT(c) > 1

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 select count(*) from many to many in spring data jpa?

I have two tables (for example posts:tags) with M:N relationship with standard middle table.
I want to select all post ids with the count of tags per each post with spring data jpa.
Here is what i did:
SELECT p.id, count(t) as total FROM post p join p.tags t;
However this is not returning the correct result.
Fixed it, adding group by did the trick.
SELECT p.id, count(t) as total
FROM post p join p.tags t
GROUP BY p.id;

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