post entity

This commit is contained in:
jinho jeong
2022-04-20 15:59:55 +09:00
parent 0ba0e948db
commit 1a18b8d90a
2 changed files with 83 additions and 7 deletions

View File

@@ -2,22 +2,93 @@ package com.example.oneul.model;
import java.time.LocalDateTime;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.springframework.data.annotation.CreatedDate;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
@Entity
public class Post {
@Id @GeneratedValue
private Long id;
@CreatedDate
private LocalDateTime createdAt;
@Column(nullable = false)
private String content;
@Access(AccessType.PROPERTY)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private UserEntity writer;
public Post(Long id, LocalDateTime createdAt, String content, UserEntity writer){
this.id = id;
this.createdAt = createdAt;
this.content = content;
this.writer = writer;
}
public Long getId(){
return this.id;
}
public LocalDateTime getCreatedAt(){
return this.createdAt;
}
public String getContent(){
return this.content;
}
public UserEntity getWriter(){
return this.writer;
}
@Override
public String toString(){
return "Post["
+ "id: " + this.id
+ ", createdAt: " + this.createdAt
+ ", content: " + this.content
+ "writer: " + this.writer.getId()
+ "]";
}
public static class Builder {
private Long id;
private String content;
private LocalDateTime createdAt;
private UserEntity writer;
public Post build(){
return new Post(
id,
createdAt,
content,
writer);
}
public Builder id(Long id){
this.id = id;
return this;
}
public Builder createdAt(LocalDateTime createdAt){
this.createdAt = createdAt;
return this;
}
public Builder content(String content){
this.content = content;
return this;
}
public Builder writer(UserEntity writer){
this.writer = writer;
return this;
}
}
}

View File

@@ -1,11 +1,14 @@
package com.example.oneul.model;
import java.time.LocalDateTime;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.springframework.data.annotation.CreatedDate;
@@ -13,13 +16,15 @@ import org.springframework.data.annotation.CreatedDate;
public class UserEntity {
@Id @GeneratedValue
private Long id;
@Column(nullable = false)
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
@CreatedDate
private LocalDateTime createdAt;
@OneToMany(orphanRemoval = true, cascade = CascadeType.REMOVE)
private Set<Post> posts;
public Long getId(){
return this.id;
}