一尘不染

如何避免Hibernate中的旧样式外部联接(+)?

hibernate

我已经这样写了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;
}

阅读 621

收藏
2020-06-20

共1个答案

一尘不染

因此,解决此问题的方法是将hibernate的方言设置为org.hibernate.dialect.Oracle9Dialect

<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
2020-06-20