package com.toy.board.domain;
import lombok.Data;
import javax.persistence.*;
import java.util.List;
@Data
@Entity
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany(mappedBy = "roles")
private List<User> users;
}
package com.toy.board.domain;
import lombok.Data;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Data
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private Boolean enabled;
@ManyToMany
@JoinTable(
name = "user_role",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private List<Role> roles = new ArrayList<>();
}
User_Role 엔티티 생성
package com.toy.board.domain;
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
public class UserRole {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@ManyToOne
@JoinColumn(name = "role_id")
private Role role;
}
'JPA' 카테고리의 다른 글
JPA - Jpa로 조회방법(FetchType) 설정하기 (0) | 2022.09.12 |
---|---|
JPA - Jpa를 이용하여 @OneToMany 관계 설정하기 (0) | 2022.09.11 |
JPA - Spring Security를 이용한 로그인 처리 (0) | 2022.09.10 |
JPA - Jpa를 이용한 페이지 처리 및 검색 (0) | 2022.09.09 |
JPA - Jpa를 이용한 RESTful API 만들기 (0) | 2022.09.08 |