关于关联的 Hibernate 自定义连接子句 [英] Hibernate custom join clause on association
问题描述
我想使用休眠注释将 2 个实体与自定义连接子句相关联.该子句涉及通常的 FK/PK 相等,但也涉及 FK 为空的情况.在 SQL 中,这将类似于:
join b on a.id = b.a_id 或 b.a_id 为空
从我读到的内容来看,我应该在所有者实体上使用 @WhereJoinTable 注释,但我对如何指定这个条件感到困惑……尤其是它的第一部分 - 指的是加入实体的 id.>
有人举个例子吗?
这是一个使用标准父/子范式的示例,我认为应该使用基本的 @Where 注释.
公共类 A {...@ManyToOne(fetch = FetchType.EAGER)//EAGER 强制外连接@JoinColumn(name = "a_id")@Where(clause = "a_id = id or a_id is null")//id"是A的PK...根据需要修改公共 B getB() { 返回 b;}}公共类 B {...@OneToMany(mappedBy = "b")公共列表<A>getA() { 返回一个;}}
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
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; }
}
这篇关于关于关联的 Hibernate 自定义连接子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!