Reputation: 4004
There is a requirement which i should use the jpa native query (because jpa doesn't support timestampdiff function).
Also i should select same table twice, such as: I have table: Individual, Task, and etc
The native query i used is: “select emp.name, tsk.name, app.name, from Individual emp, Task tsk, Individual app where ................”
The expected data i want is: "Tom, task1, Jack", however the result data is "Jack, task1, Jack" given this native sql query. Which means the app.name overrides emp.name.
If i want to get the correct result i have to use the query like: "select emp.name, tsk.name, (select app.name from Individual app where xx.id=xx.id), from Individual emp, Task tsk, Individual app where ................"
code for native query (get wrong data):
String nativeSql = "select con.first_name, app.first_name from Individual con, Task tsk, TimeBlock tb, Timesheet ts, Individual app where con.id=ts.owner_id and tb.timesheet_id=ts.id and tb.task_id=tsk.id and tsk.approver_id=app.id";
Query query = entityManager.createNativeQuery(nativeSql);
code for native query (can get correct data):
String nativeSql = "select con.first_name, (select app.first_name from Individual app where tsk.approver_id=app.id) from Individual con, Task tsk, TimeBlock tb, Timesheet ts, Individual app where con.id=ts.owner_id and tb.timesheet_id=ts.id and tb.task_id=tsk.id and tsk.approver_id=app.id";
Query query = entityManager.createNativeQuery(nativeSql);
code for jpql query:
String jpql = "select con.firstName, app.firstName from Individual con, Task tsk, TimeBlock tb, Timesheet ts, Individual app where con.id=ts.owner.id and tb.timesheet.id=ts.id and tb.task.id=tsk.id and tsk.approver.id=app.id";
Query query = entityManager.createQuery(jpql);
But the interesting is:
I used this native sql query to search from mysql db (using command line, workbench whatever), the result data is the correct one "Tom, task1, Jack"
If i used the jpql for this requirement without timestampdiff feature, the result data is also the correct one.
Just tried the jdbc, if i used the native sql query in jdbc, I can also get the correct data.
It seems some problem for jpa....
So anyone who met this kind of problem before and know the essential of that.
Thanks for your help.
Upvotes: 3
Views: 2028
Reputation: 2698
Got the same problem, just found out that you got to alias the columns to fix the problem.
This gave me wrong result :
SELECT i.number, taux5_50.vatAmount, taux19_60.vatAmount
FROM Invoice i
LEFT JOIN InvoiceVATLine taux5_50 ON taux5_50.invoice_id=i.id AND taux5_50.rate=5.50
LEFT JOIN InvoiceVATLine taux19_60 ON taux19_60.invoice_id=i.id AND taux19_60.rate=19.60
WHERE ...
This gave me correct result :
SELECT i.number, taux5_50.vatAmount AS taux5_50_vatAmount, taux19_60.vatAmount AS taux19_60_vatAmount
FROM Invoice i
LEFT JOIN InvoiceVATLine taux5_50 ON taux5_50.invoice_id=i.id AND taux5_50.rate=5.50
LEFT JOIN InvoiceVATLine taux19_60 ON taux19_60.invoice_id=i.id AND taux19_60.rate=19.60
WHERE ...
Upvotes: 3