Reputation: 606
I have 2 entities A
& B
. Both are related via new entity AB
.
public class A {
// other fields
@OneToOne(mappedBy = "a")
private AB ab;
}
public class B {
// other fields
@OneToOne(mappedBy = "b")
private AB ab;
}
public class AB{
// more fields
@ManyToOne
@JoinColumn(name = "a_id")
private A a;
@ManyToOne
@JoinColumn(name = "b_id")
private B b;
}
Now above code works, but when I try to add a relationship directly from A
to B
, my results are always empty.
public class A {
// other fields
@OneToOne(mappedBy = "a")
private AB ab;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<B> b;
}
Upvotes: 0
Views: 406
Reputation: 3423
The problem is that you use @OneToOne
from one side and @ManyToOne
from the other side.
You should use @OneToMany
in the class A and B. What is quite logical: If multiple AB can be connected to A, then there should be a collection of ABs in class A.
And after you change those annotations, you can immediately see that the relation between A and B is @ManyToMany
.
Upvotes: 1