简体   繁体   English

关联时的Hibernate自定义连接子句

[英]Hibernate custom join clause on association

I would like to associate 2 entities using hibernate annotations with a custom join clause. 我想将使用hibernate注释的2个实体与自定义连接子句相关联。 The clause is on the usual FK/PK equality, but also where the FK is null. 该子句与通常的FK / PK相等,但也是FK为空的。 In SQL this would be something like: 在SQL中,这将是这样的:

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. 根据我的阅读,我应该在所有者实体上使用@WhereJoinTable注释,但我很困惑我如何指定这个条件......特别是它的第一部分 - 指的是加入实体的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. 这是一个使用标准父/子范例的例子,我认为应该使用基本的@Where注释。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM