Hibernate的结社自加盟条款 [英] Hibernate custom join clause on association

查看:187
本文介绍了Hibernate的结社自加盟条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Hibernate使用自定义的注解加入第2条的实体相关联。该条款是平时的FK / PK的平等,而且还当FK为空。在SQL中,这将是这样的:

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 something like:

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

从我读,我应该使用@WhereJoinTable注解放在主人的实体,但我很困惑,我怎么指定这个条件......尤其是它的第一部分 - 指的是加盟实体的id

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?

推荐答案

下面是一个使用标准的父/子的范例,我认为使用基本@Where注解应该工作的例子。

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

这篇关于Hibernate的结社自加盟条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆