开发者

Hibernate custom join clause on association

开发者 https://www.devze.com 2022-12-30 00:23 出处:网络
I would like to associate 2 entities using hibernate annotations with a custom join clause. The clause is on the usual FK/PK equality, but also where the FK is null. In SQL this would be somet开发者_S

I would like to associate 2 entities using hibernate annotations with a custom join clause. The clause is on the usual FK/PK equality, but also where the FK is null. In SQL this would be somet开发者_StackOverflow社区hing like:

join b on a.id = b.a_id or b.a_id is null

From what I have read I should use the @WhereJoinTable annotation on the owner entity, but I'm puzzled about how I specify this condition...especially the first part of it - referring to the joining entity's id.

Does anyone have an example?


Here's an example using the standard parent/child paradigm that I think should work using the basic @Where annotation.

public class A {
  ...
  @ManyToOne(fetch = FetchType.EAGER) // EAGER forces outer join
  @JoinColumn(name = "a_id")
  @Where(clause = "a_id = id or a_id is null") // "id" is A's PK... modify as needed
  public B getB() { return b; }

}

public class B {
  ...
  @OneToMany(mappedBy = "b")
  public List<A> getA() { return a; }
}
0

精彩评论

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