How to rewrite sql statement to work with hibernate? - java

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.

Related

How can I write a JPA query without native query that includes join operation?

So I have a SQL query like this,
SELECT *
FROM USERS.NAMES UN
JOIN USERS.ADDRESS UA
ON ADDRESS_TYPE IN ('{user_supplied_value_1}', '{user_supplied_value_2}')
AND UN.USERS_ID = UA.USERS_ID
WHERE UN.NAMES_ID = '{user_supplied_value}'
How can I write a derived JPA query for this without having to use a native query?
I was thinking of something along the lines of this,
List<Names> findAllByNamesIdAndAddressTypeIn

Query DSL left join query

I am a newbie in Query DSL. Now i need to write this simple query in Query DSL:
select * from parent_msq pm left join child_msg cm on (cm.id = pm.id)
I try do like this:
QParentMsg qParentMsg = QParentMsg.parentMsg;
QChildMsg qChildMsg = QChildMsg.childMsg;
JPAQuery q = new JPAQuery(em);
q.from(qParentMsg).leftJoin(qParentMsg.id, qChildMsg).on(qParentMsg.id.eq(qChildMsg.id));
What am I doing wrong?
If the relationship between ParentMsg and ChildMsg entities isn't defined, Hibernate Version Prior to Hibernate 5.1 will not allow the join.
if you are using hibernate 5.1 or later, the following will work.
q.from(qParentMsg).leftJoin(qChildMsg).on(qParentMsg.id.eq(qChildMsg.id));

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.

QuerySyntaxException : Hibernate not recognizing the postgres query syntax in java

I am facing problem of executing the following query in java using hibernate for postgres tables.
The query is made up to retrive the data from 3 tables using Inner Joins.
Query :
QryJourney = "SELECT journey.id , journey.operatingday, journey.linename, journey.scheduledeparture, journey.scheduledeparturestopname, journeydetail.stopname , journeydetail.latitude, journeydetail.longitude FROM journey left join journey_journeydetail ON journey.id = journey_journeydetail.journey_id left JOIN journeydetail ON journey_journeydetail.journeydetails_id = journeydetail.id WHERE journey.id = '155815228' ORDER BY journeydetail.schedulearrival";
as soon as it executes, following exception occured.
Exception :
Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ON near line 1, column 268 [SELECT journey.id , journey.operatingday, journey.linename, journey.scheduledeparture, journey.scheduledeparturestopname, journeydetail.stopname , journeydetail.latitude, journeydetail.longitude FROM de.db.journeyTracker.model.journey left join journey_journeydetail ON journey.id = journey_journeydetail.journey_id left JOIN journeydetail ON journey_journeydetail.journeydetails_id = journeydetail.id WHERE journey.id = '155815228' ORDER BY journeydetail.schedulearrival]
Tis query works 100% fine at postgres while executing on its SQL Pane.
Anybody having any idea?
Regards
Usman
Hibernate queries are written in Hibernate Query Language (HQL) not in native SQL. Rephrase your query in HQL or use a native query to use SQL with Hibernate.
Hibernate is an object-relational mapper. It won't just give you a result set. If you want that, use JDBC directly, using PgJDBC.
If you want native domain objects as query results, use Hibernate with HQL or via a native query mapping. Native queries are fiddlier becuse you have to explicitly tell Hibernate how all the result columns map to your result objects.

"DISTINCT ON" with HQL?

I am trying to get distinct values from table application based on the column entry_id. I've managed to get this working with plain SQL:
SELECT DISTINCT ON (a.entry_id) a.* FROM application AS a
JOIN entry AS e ON e.id = a.entry_id
WHERE e.valid_until BETWEEN ? AND ?;
The problem is, I have to translate it to HQL. Is there any way to resolve it in HQL without Criteria API?
It looks like this feature has been requested a long time ago but is still unresolved.

Categories

Resources