How to avoid old style outer join (+) in Hibernate

2019-04-28 01:35发布

I've wrote HQL query like this:

SELECT a FROM A a LEFT JOIN a.b where ...

Hibernate generate sql query like this:

SELECT a FROM A a LEFT JOIN a.b where a.b_id = b.id(+)

But when I write something like this:

SELECT a FROM where a.b.id > 5

It generate SQL:

SELECT a.* FROM A b cross join B b where b.id > 5

So when I combine this approaches I recieve Oracle ERROR:

SQL Error: 25156, SQLState: 99999
ORA-25156: old style outer join (+) cannot be used with ANSI joins

So is there way to say to Hibernate that I want receive only one type of queries (old style or new) ?

UPDATE: By combining I mean HQL query like this:

SELECT alarm FROM Alarm as a LEFT JOIN alarm.madeBy as user where user.name = 'George' and a.source.name = 'UFO'

So here I specify that Alarm should be connected with User with LEFT JOIN, and not specify how connect Alarm with Source, so Hibernate will connect it with Cross Join.

And SQL Query will be like this:

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'

For more understanding I will add small example of Alarm Entity:

@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;
}

2条回答
干净又极端
2楼-- · 2019-04-28 02:31

I understand that you try to obtain SQL query below by using HQL.

SELECT a.* FROM A ,B where a.b_id = b.id(+) and b.id > 5

Firstly, you should never use code like that a.b.XX at your HQL.
the sample code is

SELECT a FROM A a LEFT JOIN a.b where b.id is null or (b.id is not null and b.id > 5)

then hibernate genarates a SQL query that does not cause an exception.

查看更多
别忘想泡老子
3楼-- · 2019-04-28 02:34

So the solving of this problem is set hibernate's Dialect to org.hibernate.dialect.Oracle9Dialect

<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
查看更多
登录 后发表回答