Convert SQL to JPA 2 Criteria queries - java

I've successfully created some JPA 2 criteria queries, but im now converting a query that is too complex for me to be able to convert it. Anyone up for a challenge? I will be eternally thankful for any response! :)
This is the query in plain SQL (mysql):
SELECT o.orderId, oi.insertDate
FROM AIDA_ORDER o
LEFT OUTER JOIN
(SELECT orderId, MIN(insertDate) AS insertDate FROM AIDA_ORDER_INSERT
WHERE insertDate > CURDATE() GROUP BY orderId) AS oi
ON oi.orderId = o.orderId
ORDER BY oi.insertDate, o.orderId;
The AIDA_ORDER table corresponds to a OrderBean, and AIDA_ORDER_INSERT to a OrderInsertBean.
A AIDA_ORDER can have multiple AIDA_ORDER_INSERTS.

Related

Write hibernate criteria predicate with where clause?

I have a field in SSMS which is of type nvarchar. It has date and time stored in it . I want to retrieve records which is before a date using criteria_builder. I am able to retrieve it using SQL. The Sql is given below:
SELECT CONVERT(DateTime,f.field_values,103) as DueDate
FROM TableA a
JOIN TableB p ON a.id = p.activity_id
JOIN TableC s ON p.id = s.package_id
JOIN TableD f ON s.id = f.package_section_id
WHERE a.code like '%sass%' and f.field_name='Due_Date'
However when i try to use Hibernate criteria:
I get
Conversion failed when converting date and/or time from character string.
This occurs as I m not using the SQL Convert function. I have searched a lot online but unable to find how can i use CONVERT in Hibernate criteria. Hibernate predicate I am using is:
Predicate fieldPredicates = cb.and(cb.equal(packageFieldJoin.get(TableB_.fieldName), "Due_Date"),
cb.lessThan(packageFieldJoin.get(TableB_.fieldValuesData)), sdf1.parse(CLOSE_DUE_BEFORE)))

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.

Convert sql query to JPA. Join 3 tables

I have a database that currently got 3 tables that i want to join together.
products
product_category
category
I have an sql join that does what i need it to do, which is taking category_id and returning all products in that category.
SELECT products.name FROM products
JOIN product_category ON products.id = product_category.product_id
JOIN categories ON categories.id = product_category.category_id
WHERE categories.id = ?
I have tried to google for a while now without success.
How can i make this happen?

Order by, group by, first result in HQL (convert from Oracle SQL)

How would represent the following Oracle SQL in Hibernate HQL.
select table_num
, room_id
, min(event_type) keep(dense_rank first order by changed_on desc)
from room_history
group by table_num, room_id;
The idea behind the query is to order the table "room_history" by "changed_on" datetime column and then group it by "table_num" and "room_id" pairs whilest keeping the first "event_type" for each group. The mentioned query works for Oracle but I have trouble converting it into HQL.
Purpose is to get the latest "event_type" for "table_num" and "room_id" pair.
It seems this is not achievable
I ended up converting the following SQL query instead. It is not perfect but it does the job for my purposes.
select table_num, event_type et from room_history where id in (select max(id) from privacy_history group by msisdn);
I was able to do this assumptions since when for this table always a.id > b.id then also a.changed_on> b.changed_on.

(JPQL) - Query for getting user with highest number of records

Excuse me for anking again about this issue but I need to have a JPA query for this:
select username, count(*)
from Records
group by username
order by count(*) desc
limit 1
I thought about smth like:
select r.username,count(*) from Records r order by r.username desc
and then to call
getResultList().get(0)
but I am allowed to write only:
select r from Records r order by r.username desc
and in this case I do not know how to get what I need.
Does anyone have any idea?
The SQL query has a group by, and orders by count. The JPA query doesn't have any group by and orders by user name. So I don't see how they could return the same thing.
The equivalent JPQL query is
select r.username, count(r.id)
from Record r
group by r.username
order by count(r.id) desc
If you call setMaxResults(1) on the Query object, the limit clause will be added to the generated SQL query, making it completely equivalent.

Categories

Resources