开发者

Hibernate Join with OR clause

开发者 https://www.devze.com 2022-12-11 02:49 出处:网络
I\'m trying to figure out how to generate a simple join that uses an \'or\' clause contained within it, using HQL or Hibernate annotations. Here is an example of what I want the SQL to look like:

I'm trying to figure out how to generate a simple join that uses an 'or' clause contained within it, using HQL or Hibernate annotations. Here is an example of what I want the SQL to look like:

select *
from tableA
left outer join tableB
    on tableA.id1 = tableB.id1
    or tableA.id2 = tableB.id2
where ...

I know I can do straight SQL code for this, however, I want to take advantage of the Hibernate abstraction level. I saw @JoinColumns however this seems to be an 'and' in the join. I'm taking advantage of this join with a typed HQL query, so I also have the ability to deviate a little from the annotations and use it in there... any i开发者_开发技巧deas?


I've got to say that this is a bit esoteric; if it were possible to map your association thusly it would most likely perform rather abysmally (unless both tables involved are pretty small).

But, as far as I know, it's not possible:

  1. You can't map an association over OR'ed columns directly
  2. You can't use any join type other than cross join over entities not connected by association.
  3. You can't specify custom loader for anything other than collection - does your tableB hold collection elements for tableA rows? Even if that's the case, you would only be able to use this Loader for straight-up collection retrieval; it won't work as part of more complex HQL query.
0

精彩评论

暂无评论...
验证码 换一张
取 消