Hibernate user connection condition in association

I would like to associate 2 entities using sleep mode annotations with a user connection suggestion. The proposition relates to the usual equality FK / PK, but also to where FK is zero. In SQL, it will be something like this:

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

From what I read, I have to use the @WhereJoinTable annotation in the owner entity, but I'm puzzled by how I define this condition ... especially the first part - with reference to the connection object identifier.

Does anyone have an example?

+6
java sql annotations hibernate
source share
1 answer

Here is an example of using the standard parent / child paradigm, which 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 PK... modify as needed public B getB() { return b; } } public class B { ... @OneToMany(mappedBy = "b") public List<A> getA() { return a; } } 
+18
source share

All Articles