【问题标题】:Hibernate custom join clause on association关于关联的 Hibernate 自定义连接子句
【发布时间】:2011-02-17 19:39:10
【问题描述】:

我想将使用休眠注释的 2 个实体与自定义连接子句相关联。该子句适用于通常的 FK/PK 相等性,但也适用于 FK 为空的情况。在 SQL 中,这将类似于:

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

根据我的阅读,我应该在所有者实体上使用 @WhereJoinTable 注释,但我对如何指定此条件感到困惑……尤其是它的第一部分 - 指的是加入实体的 id。

有人有例子吗?

【问题讨论】:

  • 如果 b.a_id 为 null 那么它属于每个 a 对象吗?
  • 是的,“a”的所有对象都将具有“b”,其中 b.a_id 为空

标签: java sql hibernate annotations


【解决方案1】:

这是一个使用标准父/子范例的示例,我认为应该使用基本的 @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; }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
相关资源
最近更新 更多