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.
Related
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.findAllByUserLogin(java.lang.String,org.springframework.data.domain.Pageable)! 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);
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
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 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
I have a following entity structure.
Venue -- n:1 -- Schedule -- 1:n -- Event
I have a query to get counts of all schedule in Venues:
SELECT v, count(s.event) FROM Venue v
LEFT JOIN i.schedule s
GROUP BY i.id ORDER BY count(s.event) asc;
The problem is that this query will never output venues, that have zero events. The problem is with Hibernate, which generates following:
select ..., count(schedule4_.event_id) as col_7_0_
from Venue venue0_
left outer join event_schedule schedule4_ on venue0_.id=schedule4_.venue_id,
Event event5_
where schedule4_.event_id=event5_.id and ...
You can see, that Hibernate tries to join Event on Schedule even though I did not request that. Obviously if the Schedule does not exist, joining Event does not make any sense.
(I simpified the query, if there is some typo, it should not have any impact on the problem).
Try with the following query:
select v.id, count(e.id) from Venue v
left join v.schedule s
left join s.events e
group by v.id order by count(e.id) asc;