I'm trying to find the number of Events that have a given Cluster as member. I have the cluster id. (Event to Cluster is a ManyToMany.)
I have an HQL query like this:
select count(*) from Event e join e.clusters c where :id in (c.id)
I've also tried to put a subselect in the "in" part, but no luck.
Is there a way to do this in HQL?
select count(distinct e.id)
from Event e join e.clusters c
where c.id = :id
Related
How to update with inner join in HQL ?
My query in SQL like this
update u
set u.name = t.name
from User u
inner join Temp t on t.id = u.id
and I try in HQL like
#Query("
update User u
set u.name = ..?..
where u.id in (select id from Temp)
")
How can I get name in Temp to set in this query?
thank you.
HQL as in Hive QL? No, Hibernate QL.
Hibernate provide a createSQLQuery method to let you call your native SQL statement directly. Native SQL Query approach based on ORACLE SQL:
update u
set u.name = (select t.name
from t
where t.id = u.id)
where exists (select 'X'
from t
where t.id = u.id)
and then apply the Hibernate stuff as per those object guidelines on parameters and bind variables. Not convinced an INNER JOIN is the way to go, but may be Hibernate is different for some approaches.
The JPQL syntax not supports the JOIN keyword.
But, if you have an entity relationship between User and Temp, you can try something like this:
UPDATE User u
SET u.name = u.temp.name
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;
I have 2 entites , each stored in mysql table.
1. productA : {productId(pk) , desc , date}
2. productB : {productId(pk) , quantity,type,date}
I want to run this SQL query:
select a.*
from productA a left join productB b using(productId)
where b.productId is null
(return all the products from a that not exists in b)
Is it possible to write this query in Hibernate?
Thank you!!
Is it possible to write this query in Hibernate?
Yes, of course. From JPA specification 2.1 (4.4.5.2 Left Outer Joins):
LEFT JOIN and LEFT OUTER JOIN are synonymous. They enable the
retrieval of a set of entities where matching values in the join
condition may be absent. The syntax for a left outer join is
LEFT [OUTER] JOIN join_association_path_expression [AS] identification_variable
[join_condition]
An outer join without a specified join condition has an implicit join
condition over the foreign key relationship corresponding to the
join_association_path_expression. It would typically be mapped to a
SQL outer join with an ON condition on the foreign key relationship as
in the queries below: Java Persistence query language:
SELECT s.name, COUNT(p)
FROM Suppliers s LEFT JOIN s.products p
GROUP BY s.name
SQL:
SELECT s.name, COUNT(p.id)
FROM Suppliers s LEFT JOIN Products p
ON s.id = p.supplierId
GROUP By s.name
An outer join with an explicit ON condition would cause an additional
specified join condition to be added to the generated SQL: Java
Persistence query language:
SELECT s.name, COUNT(p)
FROM Suppliers s LEFT JOIN s.products p
ON p.status = 'inStock'
GROUP BY s.name
SQL:
SELECT s.name, COUNT(p.id)
FROM Suppliers s LEFT JOIN Products p
ON s.id = p.supplierId AND p.status = 'inStock'
GROUP BY s.name
Note that the result of this query will be different from that of the
following query:
SELECT s.name, COUNT(p)
FROM Suppliers s LEFT JOIN s.products p
WHERE p.status = 'inStock'
GROUP BY s.name
The result of the latter query will exclude suppliers who have no
products in stock whereas the former query will include them.
An important use case for LEFT JOIN is in enabling the prefetching of
related data items as a side effect of a query. This is accomplished
by specifying the LEFT JOIN as a FETCH JOIN as described below.
Good day everyone,
I'm currently struggeling with converting the following SQL to HQL:
SELECT
e.ID,
p.ID,
i.ID
FROM
ENTRY e
JOIN PERSON p ON e.FK_PERSON = p.ID
RIGHT JOIN IDENTITY i ON i.FK_PERSON = p.ID
WHERE
i.IS_MAIN_IDENTITY = 1
;
The way the DB is structured is:
One person may have one or more identities
One person has one main identity. The main identity is an identity with IS_MAIN_IDENTITY set to 1 (true)
One person may have zero or more entries
The purpose of this query is to build a screen collection which should display all entries, their owner (person) and their main identity (because only the identity contains a persons name, etc.)
My attempt so far to perform this query in HQL was:
select
entr,
pers,
iden
from
MEntry entr
join entr.entrPerson pers
right join iden.idenPerson
but this gave me the following exception:
java.lang.IllegalStateException: No data type for node:
org.hibernate.hql.internal.ast.tree.IdentNode
\-[IDENT] IdentNode: 'iden' {originalText=iden}
What am I doing wrong here? Or is there a even better way to write this query?
Thank you for your time in advance.
You need to write your SQL query like this:
SELECT
e.ID,
p.ID,
i.ID
FROM
PERSON p
INNER JOIN ENTRY e ON e.FK_PERSON = p.ID
INNER JOIN IDENTITY i ON i.FK_PERSON = p.ID
WHERE
i.IS_MAIN_IDENTITY = 1
and the HQL equivalent is:
select
p
from
Person p
join fetch p.entries e
join fetch p.identity
So you select only Persons but each Person also contains as Identity and a list of Entry entities. You can then access all the entries for each person on your UI.
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
)