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

吃可爱长大的小学妹 提交于 2019-12-22 05:31:53

问题


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

回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/12674565/how-to-avoid-old-style-outer-join-in-hibernate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!