Hibernate: How to Join three 3 tables in one join table in Annotation?

后端 未结 1 1460
悲&欢浪女
悲&欢浪女 2020-12-09 05:31

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

相关标签:
1条回答
  • 2020-12-09 06:23

    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 {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题