hibernatespring-data-jpahql

HQL select vs delete. Why delete with cross join doesn't work?


Why this is working:

@Query("from Ban b where b.banned.uuid = :uuid")

but this cause SQL syntax error:

@Query("delete Ban b where b.banned.uuid = :uuid")

Error message is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cross join user user1_ where uuid=x'53......'' at line 1


Solution

  • Neither HQL nor, usually—at least as far as I know—SQL, supports joins in delete statements. However, you can emulate it with a subquery, something like:

    delete Ban b where b.bannedid in (select bb.id from Banned bb where bb.uuid = :uuid)