HQL: Combine Left and Right Join - java

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.

Related

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

Left Join in HQL - Hibernate query

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.

HSQL. How to exclude duplicate elements using Distinct

I have duplicate in result in hibernate query, such as:
select new ValueObject(h.id, c.firstName, c.lastName)
from HistoryTable as h left join CustomerTable as c
where h.customerId = c.id and c.notDeleted
order by c.firstName, c.lastName
But, when i used DISTINCT, duplicate in result continue to appear
select distinct new ValueObject(h.id, c.firstName, c.lastName)
from HistoryTable as h left join CustomerTable as c
where h.customerId = c.id and c.notDeleted
order by c.firstName, c.lastName
But my question is, if there is any possibility to using DISTINCT for excluding duplicates for creating new ValueObject in HSQLDB query?
Hibernate does not return distinct results for a query with left or right join.
You can use Hiberante setResultTransformer for your purposes. For more detail's explanations, why and how it resolve, look:
https://developer.jboss.org/wiki/HibernateFAQ-AdvancedProblems#jive_content_id_Hibernate_does_not_return_distinct_results_for_a_query_with_outer_join_fetching_enabled_for_a_collection_even_if_I_use_the_distinct_keyword
and
How do you create a Distinct query in HQL

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