Can you please help me how to join three tables with one common join table? I have USER, APPLICATION, and ROLE tables. And I want thier IDs to be joined in a table named USE
The problem is that you have defined a relationship to roles and a separate relationship to applications. There's no declaration that any specific user is a join of both one role and one application.
When you try to associate with a role, the application is null and visa-versa.
Here's an example of mapping a 3-way join between classes A, B and C using JPA. Note, no entity is required for the join table.
@Entity
public class A {
@JoinTable(name = "a_b_c",
joinColumns = @JoinColumn(name = "a_id"),
inverseJoinColumns = @JoinColumn(name = "c_id"))
@MapKeyJoinColumn(name = "b_id")
@ElementCollection
private Map<B, C> cByB = new HashMap<>();
}
@Entity
public class B {
...
}
@Entity
public class C {
...
}