2 Entities, both with a primary key named id as
{{ class Key implements Serializable { String pre long id }
Entity 1: IT
class IT extends AbstractPersistable<Key> { // etc. }
Entity 2: PT with ManyToOne ForeignKey To IT class PT extends AbstractPersistable<Key> { @ManyToOne(optional = false) IT it } }}
The DAO method with a @Query for deletion generates the wrong SQL because the join at the end of the statement does not contain the alias or table name of the target table.
Example:
{ { noformat} interface ITDao extends JpaRepository<IT, Key> { @Modifying @Query("delete from IT i where not exists (select p from PT p where p.it = i)") int deleteUnreferenced() } {noformat } }
SQL generated by Hibernate is: delete from IT where not (exists (select (pt1_.id, pt1_.pre) from PT pt1_ where (pt1_.it_id, pt1_.it_pre)=(id, pre))
because "(id, pre)" does not have any alias or table name, the statement does not join wiht IT but with PT which is wrong.
SQL generated should be: delete from IT where not (exists (select (pt1_.id, pt1_.pre) from PT pt1_ where (pt1_.it_id, pt1_.it_pre)=(IT.id, IT.pre))
or it could be: delete from IT i where not (exists (select (pt1_.id, pt1_.pre) from PT pt1_ where (pt1_.it_id, pt1_.it_pre)=(i.id, i.pre))
Or am I missing something?
|
|