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
Related
I have been trying to get Hibernate to generate me a query with a subquery in its where clause. I've used this answer as a base to help me going, but this question mentioned only one table.
However, this is what I would need (in SQL):
SELECT [...]
FROM a
LEFT OUTER JOIN b on a.idb = b.idb
LEFT OUTER JOIN c on b.idc = c.idc
[...]
LEFT OUTER JOIN k out on j.idk = k.idk
WHERE k.date = (SELECT max(date) from k in where in.idk = out.idk) OR k.date is null
As I am not very used to using Hibernate, I'm having trouble specifying these inner joins while navigating in the inner constraints.
I was able to re-create the initial criteria as in the linked answer, but I can't seem to join the criteria and the rootCriteria.
If the entities are properly joined with #ManyToOne annotations, simply joining the criteria to the previous table will be enough to propagate the criteria to the whole query.
The following code seems to work properly to add the WHERE clause I'm looking for.
DetachedCriteria kSubquery = DetachedCriteria.forClass(TableJPE.class,"j2");
kSubQuery = kSubQuery.createAlias("k","k2");
kSubQuery.setProjection(Projections.max("k2.date"));
kSubQuery = kSubQuery.add(Restrictions.eqProperty("j.id", "j2.id"));
rootCriteria.add(Restrictions.disjunction()
.add(Subqueries.propertyEq("k.date",kSubQuery))
.add(Restrictions.isNull("k.date")));
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.
Is there a way to further restrict a join by adding some expressions? With plain sql i write:
SELECT c.*, COUNT(i.id) invoice_count
FROM customers c
LEFT JOIN invoices i ON i.customer_id = c.id
AND i.creation_time >= '2012-01-01' -- <= extra restriction
AND i.creation_time < '2013-01-01' -- <= extra restriction
GROUP BY c.id
I haven't found a way to implement this with JPA 2.0 CriteriaQuery.
Update: As requested my (simplified) code so far (without the extra restriction):
CriteriaQuery<CustomerAndInvoiceCount> criteriaQuery = criteriaBuilder.createQuery(CustomerAndInvoiceCount.class);
Root<Customer> customer = criteriaQuery.from(Customer.class);
ListJoin<Customer, Invoice> invoices = customer.join(Customer_.invoices, JoinType.LEFT);
criteriaQuery.select(criteriaBuilder.construct(
CustomerAndInvoiceCount.class,
customer,
criteriaBuilder.count(invoices)));
criteriaQuery.groupBy(customer);
You should just be able to add the criteria predicates without worrying if they are part of the ON clause or the WHERE clause.
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
)