Update query wih join in HQL - java

I have two tables RootOprs and ChildOprs, where RootOprs can have multiple ChildOprs
I want to update 2 column in RootOprs table and one column in ChildOprs with bulk data
Update ChildOpr c
Left join Fetch c.rootOpr
set c.status=:status,
r.status=:staus
r.oprtimestamp=:timestamp
where c.id IN :childOperIDList;
This query is not working. Does anyone have better approach than this? Please help.

Related

#Query for joining two tables

I am trying to join two entities in a third one using #Query method.
#Query("SELECT new com.concretepage.entity.DeptEmpDto(d.departmentId,d.departmentName,d.managerId,d.locationId,e.employeeId,e.firstName,e.lastName,e.phoneNumber,e.hireDate,e.jobId,e.salary,e.commissionPct) FROM Employee e INNER JOIN Department d")
List <DeptEmpDto> fetchEmpDeptDataInnerJoin();
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1.
I cannot understand where is my mistake.Any help will be appreciated :).
You missed the joining condition after joining tables using ON clause. So just change your query with this:
#Query("SELECT new com.concretepage.entity.DeptEmpDto(d.departmentId,d.departmentName,d.managerId,d.locationId,e.employeeId,e.firstName,e.lastName,e.phoneNumber,e.hireDate,e.jobId,e.salary,e.commissionPct) FROM Employee e INNER JOIN Department d on e.joining_column_from_table1=d.joining_column_from_table2")
Make sure to replace joining_column_from_table1 and
joining_column_from_table2 with your column names from table
Employee and Department respectively

How to write this query with hibernate using mysql DB

I am trying to select one row from all 3 tables. The tables have the same structure, 4 columns, "code", "airportname", "latitude", "longitude".
The query I am trying to write is:
select
finnairroute.code,finnairroute.airportname,finnairroute.latitude,finnairroute.longitude,lhroute.airportname,lhroute.latitude,lhroute.longitude,sasroute.code,sasroute.airportname,sasroute.latitude,sasroute.longitude
FROM frs.sasroute, frs.finnairroute,frs.lhroute where
sasroute.code="BER" or finnairroute.code ="BER" or lhroute.code
="BER";
Tested with Mysql workbench and I got this result:
How should I write the query in order to get one single row that has all the info for "BER"?
Many thanks!
You can try something like this:
SELECT finnairroute.*,lhroute.*,sasroute.* FROM DUAL
LEFT OUTER JOIN finnairroute ON (finnairroute.code='BER')
LEFT OUTER JOIN lhroute ON (lhroute.code='BER')
LEFT OUTER JOIN sasroute ON (sasroute.code='BER')
The thing is that you will always get one row, even if the code is in none of the table. You need to test whether finnairroute.code, lhroute.code and sasroute.code are null or not.
Another way to approach the problem:
SELECT 'finnair' as company,code,airportname,latitude,longitude FROM finnairroute WHERE code='BER'
UNION SELECT 'lh' as company,code,airportname,latitude,longitude FROM lhroute WHERE code='BER'
UNION SELECT 'sas' as company,code,airportname,latitude,longitude FROM sasroute WHERE code='BER'
This gives you between zero and three rows, depending on what tables contain the code 'BER'. An additional column, 'company' tels you which table contains each row.

How to select count(*) from many to many in spring data jpa?

I have two tables (for example posts:tags) with M:N relationship with standard middle table.
I want to select all post ids with the count of tags per each post with spring data jpa.
Here is what i did:
SELECT p.id, count(t) as total FROM post p join p.tags t;
However this is not returning the correct result.
Fixed it, adding group by did the trick.
SELECT p.id, count(t) as total
FROM post p join p.tags t
GROUP BY p.id;

How to count all groups returned by a group by query in JPQL?

If I have a group by query
Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...
How can I count the number of rows not using NativeQuery?
I need to do something like this:
select count(*) from
(Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...)
Is there any way to do with JPQL?
in oracle you can this hql
Select sum(count(*) - count(*) + 1)
from Entity e
group by e.field1, e.field2...
but dose not work in postgres
Derived tables are not supported in JPQL, so, the best way to do it is to run a native SQL query.
After all, there is a very good reason why both JPA and Hibernate offer support for SQL queries, don't you agree?

Combine two resultset from different table

My Requirement is to display some of the columns in one table and some of the columns in another table in an html table. Though it has same coloumn id , value will be different. So, I cannot match this two. My query is as follows:
SELECT time_stamp,queryresultset FROM table1 d WHERE dID = 'CP009'
AND d.time_stamp >'2011-05-01 00:00:00' AND d.time_stamp < '2011-05-01 05:00:00'
order by time_stamp
UNION ALL
SELECT time_stamp,cpuutil FROM table2 h WHERE hID='HS002'
AND h.time_stamp >'2011-05-01 00:00:00' AND h.time_stamp < '2011-05-01 05:00:00'
order by time_stamp
So, the time_stampe here I'm getting will vary just in milliseconds for both the table. But, I want it in one resultset. Though the time value varies in millisecones between the table, the number of rows will be equal. So, I have to bring this in a single resultset. I don't know whether it is possible to handle in sql query. Or I may have to try in java coding? Please guide me. Following is my sample html table.
----------------------------------------------------
Time_stamp Cpuutil Queryresultset
----------------------------------------------------
2011-03-09 12:00:00 2.3 9.8
2011-03-09 12:15:00 5.3 4.5
2011-03-09 12:30:00 4.3 9.3
2011-03-09 12:45:00 2.3 9.2
I am afraid, I have difficulties with understanding your question, but it seems to me, you are looking for something as:
SELECT table1.time_stamp t1, table1.queryresultset, table2.time_stamp t2, table2.cpuutil
FROM table1 , table2
WHERE ABS(t1-t2)<100
AND t1 >'2011-05-01 00:00:00'
AND t1 < '2011-05-01 05:00:00'
ORDER by t1
Another posibility:
`SELECT column list
FROM table1
INNER JOIN table2
ON table1.col1=table2.col2
WHERE criteria
ORDER BY column list `
Have a nice day.
This sounds like a database conception mistake. If your two tables are linked logically, they should have a physical connector. If you can refactor your database model, the best solution is to add a table table0 holding the common factors between the two tables (or just an autoincrement id), and then to add an external key to table1 and table2. You need to insert a row into table0 first, then to insert a row into table1 and one into table2 using table0's key as an external key.
If you can't refactor the tables, that's too bad. Anyway, the easiest way to do what you want is to write java code:
Open resultset1 from table 1
Open resultset2 from table 2
While resultset1 is not empty
fetch row1 from resultset1
fetch row2 from resultset2
generate html
Close resultsets
Done!
But this way is wrong and you will get problems as you advance further in your project.
Thanks for your responses. So I found out the query. It's working now.
SELECT a.cpuutil,a.hostid,a.time_stamp, b.queryresultset, b.time_stamp AS tm
FROM table1 a, table2 b
WHERE a.hID = 'hs002' AND b.dID='cp011'
AND SUBSTR(a.time_Stamp,1,15) = SUBSTR(b.time_stamp,1,15)
AND a.time_stamp > '2011-05-10 00:00:00'
AND a.time_stamp < '2011-05-10 14:00:00'

Categories