Cooper's devlog

6-5. 도메인 클래스에 대한 중복 제거 및 리팩토링 본문

Programming/Spring-boot

6-5. 도메인 클래스에 대한 중복 제거 및 리팩토링

cooper_dev 2020. 8. 3. 20:10

1. 강의 링크

https://www.youtube.com/watch?v=fjyf2XZFtJE&list=PLqaSEyuwXkSppQAjwjXZgKkjWbFoUdNXC&index=52

 


2. 학습 목표

  • User, Question, Answer 클래스에서 발생하는 중복을 제거
  • 모든 데이터의 생성일과 수정일을 효과적으로 처리하는  방법

3. 과정

1. domain class 내에 발생하는 중복 제거

(1) AbstractEntity 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package net.slipp.domain;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
 
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
 
import com.fasterxml.jackson.annotation.JsonProperty;
 
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class AbstractEntity {
    @Id
    @GeneratedValue
    @JsonProperty
    private Long id;
    
    @CreatedDate
    private LocalDateTime createDate;
    
    @LastModifiedDate
    private LocalDateTime modifiedDate;
 
    public String getFormattedCreateDate() {
        return getFormattedDate(createDate, "yyyy.MM.dd HH:mm:ss");
    }
 
    public String getFormattedModifiedDate() {
        return getFormattedDate(modifiedDate, "yyyy.MM.dd HH:mm:ss");
    }
 
    public String getFormattedDate(LocalDateTime dateTime, String format) {
        if(dateTime == null) {
            return "";
        }
        return dateTime.format(DateTimeFormatter.ofPattern(format));
    }
    
    public Long getId() {
        return id;
    }
 
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }
 
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        AbstractEntity other = (AbstractEntity) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
 
    @Override
    public String toString() {
        return "AbstractEntity [id=" + id + ", createDate=" + createDate + ", modifiedDate=" + modifiedDate + "]";
    }
}
 
 
  • 부모 class에 중복이 발생하는 column 작성 및 중복 method 작성한 중복 제거
  • createDate, modifiedDate 리팩토링(중복제거)
  • @createDate / @LastModifiedDate : 작성 및 수정 내용 자동 업로드 annotation
  • @MappedSuperClass : 공통 매핑 정보가 필요할 때, 부모 클래스에 선언하고 속성만 상속 받아서 사용
  • @EntityListeners(AuditingEntityListener.class) : 해당하는 annotatition(@createDate / @LastModifiedDate)와 같은 annotaion을 인식하여 업데이트 되는 값을 인지한다.

 

(2) MySlippAplication 내용 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package net.slipp;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
 
@SpringBootApplication
@EnableJpaAuditing
public class MySlippApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(MySlippApplication.class, args);
    }
 
}
 
 
  • @EnableJpaAuditing : Audit은 주로 DB값이 변경했을 때 누가 값을 변경했고, 언제 변경했는지, Audit(감사)하는 용도로 사용

 

(3) question, answer, user class 수정

-question.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package net.slipp.domain;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
 
import javax.persistence.*;
 
import org.hibernate.type.LocalDateTimeType;
 
import com.fasterxml.jackson.annotation.JsonProperty;
 
@Entity
public class Question extends AbstractEntity{
    
    @ManyToOne
    @JoinColumn(foreignKey = @ForeignKey(name="fk_question_writer"))
    @JsonProperty
    private User writer;
    
    @JsonProperty
    private String title;
    
    @Lob //상당히 긴 내용을 첨가할 경우
    @JsonProperty
    private String contents;
    
    @JsonProperty
    private Integer countOfAnswer = 0;
    
    @OneToMany(mappedBy = "question")
    @OrderBy("id DESC")
    private List<Answer> answers;
    
    public Question() {
    }
 
    public Question(User writer, String title, String contents) {
        super();
        this.writer = writer;
        this.title = title;
        this.contents = contents;
    }
 
    public void update(String title, String contents) {
        this.title = title;
        this.contents = contents;
    }
 
    public boolean isSameWriter(User loginUser) {
        return this.writer.equals(loginUser);
    }
 
    //댓글 수 증가 감소
    public void addAnswer() {
        this.countOfAnswer += 1;
    }
    
    public void deleteAnswer() {
        this.countOfAnswer -= 1;
    }
}
 
 

 

  • AbstractEntity를 상속(extends) 받은 것을 기반으로 재작성
  • 중복되는 column(createDate, id) : AbstractEntity에서 상속

-answer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package net.slipp.domain;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
 
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
 
import com.fasterxml.jackson.annotation.JsonProperty;
 
@Entity
public class Answer extends AbstractEntity{
 
    @ManyToOne
    @JoinColumn(foreignKey = @ForeignKey(name="fk_answer_writer"))
    @JsonProperty
    private User writer;
    
    @ManyToOne
    @JoinColumn(foreignKey = @ForeignKey(name="fk_answer_to_question"))
    @JsonProperty
    private Question question;
    
    @Lob
    @JsonProperty
    private String contents;
    
    public Answer() {
    }
    
    public Answer(User writer,Question question, String contents) {
        this.writer = writer;
        this.question = question;
        this.contents = contents;
    }
 
    public boolean isSameWriter(User loginUser) {
        return loginUser.equals(this.writer);
    }
 
    @Override
    public String toString() {
        return "Answer [" + super.toString() + ", writer=" + writer + ", contents=" + contents + "]";
    }
 
}
 
 

 

-user

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package net.slipp.domain;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
 
@Entity //해당 class를 Entity(table)로 사용하겠다는 의미 
public class User extends AbstractEntity{
 
    //(1) nullable = false : not null 설정 (2) length = 20 : id 문자열 길이
    @Column(nullable = falselength = 20, unique=true
    @JsonProperty
    private String userId;
    
    @JsonIgnore
    private String password;
    
    @JsonProperty
    private String name;
    
    @JsonProperty
    private String email;
    
    public boolean matchId(Long newId) {
        if(newId == null) {
            return false;
        }
        return newId.equals(getId());
    }
    
    public boolean matchPassword(String newPassword) {
        if(newPassword == null) {
            return false;
        }
        return newPassword.equals(password);
    }
 
    public void update(User updateUser) {
        this.userId = updateUser.userId;
        this.password = updateUser.password;
        this.name = updateUser.name;
        this.email = updateUser.email;
    }
 
    public void setUserId(String userId) {
        this.userId = userId;
    }
 
    public String getUserId() {
        return userId;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public void setName(String name) {
        this.name = name;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    
    
    @Override
    public String toString() {
        return "User [" + super.toString() +", userId=" + userId + ", password=" + password + ","
                + " name=" + name + ", email=" + email + "]";
    }
}
 
  • toString : super.toString() 상속받은 내용 첨부
Comments