How to update with inner join in HQL - java

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

Related

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.

Spring data jpa left join fetch and where clause

I have the following query in my repository:
SELECT p FROM Project p JOIN p.users u WHERE u.login =:login
There is a Many To Many relationshio between user and project.
Everything works fine and it returns the user's projects, but I want it for each project to return the corresponding set of users.
UPDATE: Tried the following as mateuszlo suggested:
SELECT p FROM Project p JOIN FECTH p.users JOIN p.users u WHERE u.login =:login
But now i got the following exception:
nested exception is java.lang.IllegalArgumentException: Count query validation failed for method public abstract org.springframework.data.domain.Page com.example.app.repository.ProjectRepository.findAllByUserLo‌​gin(java.lang.String‌​,org.springframework‌​.data.domain.Pageabl‌​e)! org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
It is impossible to create a named alias for a FETCH JOIN query to use it in a WHERE statement. JPA doesn't allow this on purpose, because it could easly lead to unwanted situations.
Consider a Project X, which has 3 Users: John, Tom and Mike. Query with FETCH JOIN for projects, that have user John would return Project X with only one User - John. That would produce an incomplete Project entity, which is inconsistent with current database state.
So what you have to do is to join twice. First time with normal JOIN, to identify proper Projet records, and then second time with FETCH JOIN to fetch correspoding Users:
SELECT p FROM Project p
JOIN FETCH p.users
JOIN p.users u WHERE u.login =:login
First off I'm assuming you're using pagination? That is you have a Pagable parameter on your JPA query method? If so, there are two ways to solve this.
Count query
#Query(value = "SELECT p FROM Project p " +
"LEFT JOIN FETCH p.users u WHERE u.login = :login",
countQuery = "SELECT COUNT(u) FROM User u WHERE u.login = :login")
Page<Project> fetchProjectsByUserLogin(String login, Pageable pageable);
Entity Graph
#Query(value = "SELECT p FROM Project p " +
"LEFT JOIN p.users u WHERE u.login = :login")
#EntityGraph(attributePaths = {"users"}, type = EntityGraph.EntityGraphType.FETCH)
Page<Project> fetchProjectsByUserLogin(String login, Pageable pageable);

HQL: how to find item in a subcollection by id

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

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