JOIN with multiple OR in HQL - java

Below is a SQL query with multiple joins and conditions which gives the desired output, I want to convert the below query to HQL
select * from customer c
join customer_geo_rel cg on c.id=cg.customer_id
join geography g on cg.geo_id=g.id
join geo_geo_hierarchy gghCluster on g.id = gghCluster.geo_id
join geo_geo_hierarchy gghDivision on gghCluster.geo_id = gghDivision.parent_geo_id
join role_data_rel rdr on gghCluster.geo_id = rdr.permission_data_id OR
gghDivision.parent_geo_id = rdr.permission_data_id OR
g.id=rdr.permission_data_id

What's the problem? HQL supports the ON clause for joins and if you use a very old version of Hibernate, you can use the WITH clause like e.g. .. join g.roleDataRelList rds on ...

Related

Hibernate Criteria: hibernate left join without association

I am a Hibernate newbie and i have this below query. It is working as i expected. These two tables are not associated. Is there a way to get the same result by using Criteria API or how can i run this query via Hibernate ? Any help would be appreciated.
SELECT p.title, c.content
FROM posts p
LEFT JOIN comments c ON p.id = c.post_id
WHERE p.status = 'A' AND (p.title iLIKE '%r%' OR c.content iLIKE '%r%');
Criteria API needs a path between entities, so I'm not sure this join could be done using Criteria API. Better do it with HQL if you have Hibernate >= 5.1:
select p.title, c.content
from org.example.Posts p
left outer join org.example.Comments c
on p.id = c.id
where p.status = 'A' AND (lower(p.title) LIKE '%r%' OR lower(c.content) LIKE '%r%');
Still, you could stick to using SQL queries with Hibernate or better still, create association between Posts and Comments.

How to rewrite sql statement to work with hibernate?

I'm using PostgreSQL in my java application without ORM. I want to go further and add Hibernate to my project. I have this sql query which I add to PreparedStatement() and it returns a number.
SELECT COUNT(pr.id) FROM prisoner pr
JOIN cell c ON c.id = pr.cell_id
JOIN prison p ON p.id = c.prison_id
WHERE p.id = ?
I'm new to Hibernate. How would you suggest me to rewrite this statement to work with Hibernate? Should I use HSQL, or criteria or query or something different ?
You can do it Either of following way.
1) Keep you query as it and use nativeSQL for hibernate.
hibernate native query, count
2) make model of all your join table and put hibernate query.

Is there any approach to make Join with JPA2 criteria API like this?

I'm in trouble with JPA2 criteria API. Is there any approach to make smth like this with JPA2 criteria API:
SELECT t1.id FROM Table1 t1 INNER JOIN Table2 t2 ON (t1.id = t2.id)
With these rules:
table1 hasn't table2 reference
table2 has table1 reference
the result should be table1 ids
I'm consfused with JPA2 criteria API Root. How to create join in this situation? Should I use two ROOT's : one for JOIN, another for SELECT? please response
Using where
You could do a usual JPQL cross-product join:
SELECT t1.id FROM Table1 t1, Table2 t2 WHERE t2.t1_id = t1.id
JPA support for right joins
JPA does not support right join queries. From here:
Right outer joins and right outer fetch joins are not required to be
supported in Java Persistence 2.0. Applications that use RIGHT join
types will not be portable.
Native SQL query
So another option would be using a native SQL query.
SELECT Table1.id FROM Table1, Table2 WHERE Table2.t1_id = Table1.id
The JPQL query would simply be
select e1.id from Entity2 e2 join e2.entity1 e1
With the criteria API, it would look like this (untested. I hate this API):
CriteriaQuery<String> cq = cb.createQuery(Long.class);
Root<Entity2> e2 = cq.from(Entity2.class);
cq.select(e2.get(Entity2_.entity1).get(Entity1_.id));

Join and Group using DynamicQuery

I have an SQL query:
Select t1.*
From tracking As t1
Inner Join (
Select Max(trackingid) As trackingid, shipid
From tracking
Group By shipid
) As t2 On t1.trackingid = t2.trackingid
How can I execute this query using DynamicQuery?
Aggreed with Kumar. You cannot use JOINS in dynamic query.
Its better to use custom-sql or finder query.

HQL ERROR: Path expected for join

I keep trying variations of this query and can't seem to make this happen. I've also referenced this post: Path Expected for Join! Nhibernate Error and can't seem to apply the same logic to my query. My User object has a UserGroup collection.
I understand that the query needs to reference entities within the object, but from what I'm seeing I am...
#NamedQuery(
name = "User.findByGroupId",
query =
"SELECT u FROM UserGroup ug " +
"INNER JOIN User u WHERE ug.group_id = :groupId ORDER BY u.lastname"
)
select u from UserGroup ug inner join ug.user u
where ug.group_id = :groupId
order by u.lastname
As a named query:
#NamedQuery(
name = "User.findByGroupId",
query =
"SELECT u FROM UserGroup ug " +
"INNER JOIN ug.user u WHERE ug.group_id = :groupId ORDER BY u.lastname"
)
Use paths in the HQL statement, from one entity to the other. See the Hibernate documentation on HQL and joins for details.
You need to name the entity that holds the association to User. For example,
... INNER JOIN ug.user u ...
That's the "path" the error message is complaining about -- path from UserGroup to User entity.
Hibernate relies on declarative JOINs, for which the join condition is declared in the mapping metadata. This is why it is impossible to construct the native SQL query without having the path.
You'll be better off using where clauses. Hibernate does not accept inner joins for tables where the PK/FK relationship is not there between Entities
do
SELECT s.first_name, s.surname, sd.telephone_number FROM Student s, StudentDetails sd WHERE s.id = sd.student_id
instead of
SELECT s.first_name, s.surname, sd.telephone_number FROM Student s INNER JOIN StudentDetails sd on s.id = sd.student_id
The latter will only work if Student's id (s.id) is referenced as FK on StudentDetails (sd.student_id)) table design / erd

Categories

Resources