> For the complete documentation index, see [llms.txt](https://flamme1004.gitbook.io/flamme-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://flamme1004.gitbook.io/flamme-dev/info-1/framework/spring/jpa/compositekey-manytoone-joincolumn.md).

# @CompositeKey에 ManyToOne JoinColumn이 있을 경우

\[2020-01-08]

![](https://15316191-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M7_opr2RFh9Acog0_8H%2F-MQX-igfnI5XXWJHfMGv%2F-MQX0Q0LYzHys8wciHO7%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202021-01-08%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%209.39.22.png?alt=media\&token=b553df22-5007-445c-a7df-44de76bab0e4)

&#x20; Group과 GroupMember의 관계는 @ManyToOne 양방향 연관관계이다.

&#x20;연관관계의 주인은 @OneToMany인 GroupMember에 설정하였다.

&#x20;여기서 GroupMember의 Entity Class 는 아래와 같이 되어있다.

```
public class GroupMember {

    @EmbeddedId
    private GroupMemberId groupMemberId
    
    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("gid")
    @JoinColumn("id")
    private Group group
    
    @OneToOne
    @JoinColumn(name="g_id", updateable = false, insertable = false)
    private UserProfile userProfile
    
    @OneToOne
    @JoinColumn(name="g_id", updateable = false, insertable = false)
    private UserInfo userInfo

}

@Embeddedable
public class GroupMemberId implements Serializable{

    @Column(name = "g_id")
    private Long gId
    
    @Column(name = "u_id")
    private Long uId
}
```

Composite key에 만약 @ManyToOne이 들어가야 된다면 IdClass를 사용하거나 이렇게 MapsId를 사용하여야 한다.

만약 Insert를 하려고 한다면, GroupMemberId에서 uId와 Group Entity만 넣어주면 된다.

&#x20;즉 gId는 생략해도 된다는 것이다.
