JPA Two primary key at owning side and One Foreign Key at the Child

13 views Asked by At

I have 3 tables

Table name = TBL_USER —Columns
SSN [PK] Username[PK] Password

Table name = TBL_ROLE —Columns Role_ID[PK] Role_Name Privilege

Table name = TBL_USER_ROLE —Columns Username [PK] [FK to TBL_USER] Role_ID [PK] [FK to TBL_ROLE]

Is there any option to make the mapping :: [TBL_USER]—Username—[TBL_USER_ROLE]—Role_ID[TBL_ROLE]?

I tried to use

  1. @CollectionTable with @ElementCollection along with creating an embeddable for TBL_USER_ROLE where User entity is the owner. However, it didn’t works because TBL_USER has 2 primary keys & TBL_USER_ROLE has only one column that represents the user. Hence, it breaks while using @JoinColumn
  2. Tried to Create an entity for TBL_USER_ROLE with User entity as a variable, annotated with both @id and @ManyToOne annotation. This time it breaks, saying that UserRole entity has broken id. I think it is also due to multiple primary keys (SSN & Username) at the owning side [TBL_USER].
0

There are 0 answers