我写了这样的 HQL 查询:
SELECT a FROM A a LEFT JOIN a.b where ...
Hibernate 生成这样的 sql 查询:
SELECT a FROM A a LEFT JOIN a.b where a.b_id = b.id(+)
但是当我写这样的东西时:
SELECT a FROM where a.b.id > 5
它生成 SQL:
SELECT a.* FROM A b cross join B b where b.id > 5
因此,当我结合这些方法时,我会收到 Oracle 错误:
SQL Error: 25156, SQLState: 99999
ORA-25156: old style outer join (+) cannot be used with ANSI joins
那么有没有办法对 Hibernate 说我只想接收一种类型的查询(旧式或新式)?
更新: 通过组合我的意思是像这样的 HQL 查询:
SELECT alarm FROM Alarm as a LEFT JOIN alarm.madeBy as user where user.name = 'George' and a.source.name = 'UFO'
所以这里我指定Alarm应该使用 LEFT JOIN与User连接,而不是指定如何将Alarm与Source连接,因此 Hibernate 将其与 Cross Join 连接。
SQL 查询将是这样的:
FROM Alarms a, Users u cross join Sources s where a.user_id = u.user_id(+) and a.source_id = s.source_id and u.name = 'George' and s.name = 'UFO'
为了更多的理解,我将添加警报实体的小例子:
@Entity
@Table(name = 'Alarms')
public class Alarm {
@Id
private BigDecimial id;
@ManyToOne
@JoinColumn(name = "user_id")
private User madeBy;
@ManyToOne
@JoinColumn(name = "source_id")
private Source source;
}