HQL with subquery with entity path at least two levels deep produces wrong SQL
Description
If we have for example these entities:
@Entity
class A implements Serializable {
private static final long serialVersionUID = -5412503864464287451L;
@Id @GeneratedValue long id;
@ManyToOne B b;
}
@Entity
class B implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue long id;
@ManyToOne C c;
}
@Entity
class C implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue long id;
}
HQL like this:
select a.id from A a where exists (select b.id from B b where a.b.c.id = 5) will produce SQL:
select a1_0.id from A a1_0 where exists(select b1_0.id from B b1_0,B b2_0 where b2_0.c_id=5)
This is obviously wrong as b2_0 should be in correlation with a1_0 . Exists is not a factor here as this does not work with in either. Attached test contains more examples, see console output.
If we have for example these entities:
@Entity class A implements Serializable { private static final long serialVersionUID = -5412503864464287451L; @Id @GeneratedValue long id; @ManyToOne B b; } @Entity class B implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue long id; @ManyToOne C c; } @Entity class C implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue long id; }
HQL like this:
select a.id from A a where exists (select b.id from B b where a.b.c.id = 5)
will produce SQL:select a1_0.id from A a1_0 where exists(select b1_0.id from B b1_0,B b2_0 where b2_0.c_id=5)
This is obviously wrong as b2_0 should be in correlation with a1_0 .
Exists
is not a factor here as this does not work within
either. Attached test contains more examples, see console output.