1

How to update with inner join in HQL ?

My query in SQL like this

update u
set u.name = t.name
from User u
inner join Temp t on t.id = u.id

and I try in HQL like

@Query("
update User u
set u.name = ..?..
where u.id in (select id from Temp)
")

How can I get name in Temp to set in this query?
thank you.

methodplayer
  • 43
  • 1
  • 8

2 Answers2

0

HQL as in Hive QL? No, Hibernate QL.

Hibernate provide a createSQLQuery method to let you call your native SQL statement directly. Native SQL Query approach based on ORACLE SQL:

update u
   set u.name = (select t.name  
                   from t
                  where t.id = u.id)
 where exists (select 'X' 
                 from t 
                where t.id = u.id)

and then apply the Hibernate stuff as per those object guidelines on parameters and bind variables. Not convinced an INNER JOIN is the way to go, but may be Hibernate is different for some approaches.

thebluephantom
  • 11,806
  • 6
  • 26
  • 54
0

The JPQL syntax not supports the JOIN keyword.

But, if you have an entity relationship between User and Temp, you can try something like this:

UPDATE User u
SET u.name = u.temp.name
Dherik
  • 13,091
  • 10
  • 86
  • 132