지난 게시글의 설계된 ER-D를 활용해 Entity를 구현했다.
Post와 PostFile은 1:N 관계이므로 각각 OneToMany
, ManyToOne
매핑을 해주었다.
@Entity
@NoArgsConstructor
@Getter
@SequenceGenerator(
name = "POST_SEQ_GEN", // 시퀀스 제너레이터 이름
sequenceName = "POST_SEQ", // 시퀀스 이름
initialValue = 1,
allocationSize = 1
)
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "POST_SEQ_GEN")
private Integer id;
@Column(length = 100, nullable = false)
private String title;
@Column(columnDefinition = "LONGTEXT", nullable = false)
private String content;
@Column(nullable = false)
private String previewContent;
@Column
private String thumbnail;
@Column(nullable = false)
@CreationTimestamp
private LocalDateTime writingTime;
@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<PostFile> fileList = new ArrayList<>();
@Column(nullable = false)
private Boolean visible;
@Builder
public Post(Integer id, String title, String content, String previewContent, String thumbnail, LocalDateTime writingTime, List<PostFile> fileList, Boolean visible) {
this.id = id;
this.title = title;
this.content = content;
this.previewContent = previewContent;
this.thumbnail = thumbnail;
this.writingTime = writingTime;
this.fileList = fileList;
this.visible = visible;
}
@Entity
@Getter
@NoArgsConstructor
public class PostFile {
@Id @Column(length = 50)
private String uuid;
@Column(length = 100, nullable = false)
private String mimeType;
@Column(length = 500, nullable = false)
private String originalFileName;
@Column(length = 100, nullable = false)
private String fileName;
@Column
private Long fileSize;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
@Builder
public PostFile(String uuid, String mimeType, String originalFileName, String fileName, Long fileSize, Post post) {
this.uuid = uuid;
this.mimeType = mimeType;
this.originalFileName = originalFileName;
this.fileName = fileName;
this.fileSize = fileSize;
this.post = post;
}
}
Project와 ProjectFile도 마찬가지로 1:N 관계이므로 각각 OneToMany
, ManyToOne
으로 매핑해주었다.
@Entity
@SequenceGenerator(
name = "PROJECT_SEQ_GEN", // 시퀀스 제너레이터 이름
sequenceName = "PROJECT_SEQ", // 시퀀스 이름
initialValue = 1,
allocationSize = 1
)
@NoArgsConstructor
@Getter
public class Project {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PROJECT_SEQ_GEN")
private Integer id;
@Column(length = 100, nullable = false)
private String title;
@Column(columnDefinition = "LONGTEXT", nullable = false)
private String content;
private String thumbnail;
@Column(length = 500, nullable = false)
private String summary;
@Column(length = 100, nullable = false)
private String duration;
@Column(length = 500, nullable = false)
private String skills;
@Column(length = 500, nullable = false)
private String learning;
@Column(nullable = false)
private LocalDateTime writingTime;
@OneToMany(mappedBy = "project", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ProjectFile> fileList = new ArrayList<>();
@Column(nullable = false)
private Boolean visible;
@Builder
public Project(Integer id, String title, String content, String thumbnail, String summary, String duration, String skills, String learning, LocalDateTime writingTime, List<ProjectFile> fileList, Boolean visible) {
this.id = id;
this.title = title;
this.content = content;
this.thumbnail = thumbnail;
this.summary = summary;
this.duration = duration;
this.skills = skills;
this.learning = learning;
this.writingTime = writingTime;
this.fileList = fileList;
this.visible = visible;
}
}
@Entity
public class ProjectFile {
@Id @Column(length = 50)
private String uuid;
@Column(length = 100, nullable = false)
private String mimeType;
@Column(length = 500, nullable = false)
private String originalFileName;
@Column(length = 100, nullable = false)
private String fileName;
@Column
private Long fileSize;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "project_id")
private Project project;
}
Admin Entity에서는 Spring Security
를 사용할 때 필요한 UserDetails 클래스의 함수를 오버라이딩하여 구현하였다.
@Entity
@Getter
public class Admin implements UserDetails {
@Id @Column(length = 100)
private String id;
@Column(length = 200, nullable = false)
private String password;
@ElementCollection(fetch = FetchType.EAGER)
@Builder.Default
private List<String> roles = new ArrayList<>();
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.roles.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return id;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}