我寫的HQL這樣的查詢:如何在Hibernate中避免舊式外連接(+)?
SELECT a FROM A a LEFT JOIN a.b where ...
休眠生成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,我想只接收一種類型的查詢(舊式或新的)?
UPDATE: 通過結合我的意思是HQL這樣的查詢:
SELECT alarm FROM Alarm as a LEFT JOIN alarm.madeBy as user where user.name = 'George' and a.source.name = 'UFO'
所以在這裏我指定報警應用戶進行連接,LEFT JOIN,並沒有規定如何連接報警與來源,所以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;
}
你是什麼意思結合的方法? –
請爲「組合」方法顯示一些代碼。 –