-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Step2] 연관 관계 매핑 #58
[Step2] 연관 관계 매핑 #58
Changes from 5 commits
1a030a2
22ea250
039b854
2bf1701
4e370b6
bf70361
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,26 +2,45 @@ | |
|
||
import java.util.Objects; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.ForeignKey; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.Lob; | ||
import javax.persistence.ManyToOne; | ||
|
||
import org.springframework.lang.NonNull; | ||
|
||
import qna.NotFoundException; | ||
import qna.UnAuthorizedException; | ||
|
||
@Entity | ||
public class Answer extends BaseEntity { | ||
|
||
public static final Answer NONE = new Answer(); | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private Long writerId; | ||
private Long questionId; | ||
|
||
@JoinColumn(name = "writer_id", | ||
referencedColumnName = "ID", | ||
foreignKey = @ForeignKey(name = "fk_answer_writer")) | ||
@ManyToOne | ||
private User writer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 연관관계 설정 잘해주셨네요 👍 기본 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 조금더 명시적이면 좋겠다라고 판단해서 작성했습니다만, 혹시 잘못된걸까요!? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
그리고 보통 아래와 같은 형태로 작성을 합니다.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 답글 주셨군요 ㅎㅎ 감사합니다. referencedColumnName 이라는게 있어서 사용해본 거라 제거하도록 하겠습니다 : ) |
||
|
||
@JoinColumn(name = "question_id", | ||
referencedColumnName = "ID", | ||
foreignKey = @ForeignKey(name = "fk_answer_to_question")) | ||
@ManyToOne | ||
private Question question; | ||
@Lob | ||
private String contents; | ||
|
||
@NonNull | ||
@Column(nullable = false) | ||
private boolean deleted = false; | ||
|
||
public Answer(User writer, Question question, String contents) { | ||
|
@@ -39,19 +58,19 @@ public Answer(Long id, User writer, Question question, String contents) { | |
throw new NotFoundException(); | ||
} | ||
|
||
this.writerId = writer.getId(); | ||
this.questionId = question.getId(); | ||
this.writer = writer; | ||
this.question = question; | ||
this.contents = contents; | ||
} | ||
// for jpa | ||
protected Answer() {} | ||
|
||
public boolean isOwner(User writer) { | ||
return this.writerId.equals(writer.getId()); | ||
return this.writer.equals(writer); | ||
} | ||
|
||
public void toQuestion(Question question) { | ||
this.questionId = question.getId(); | ||
this.question = question; | ||
} | ||
|
||
public Long getId() { | ||
|
@@ -62,20 +81,20 @@ public void setId(Long id) { | |
this.id = id; | ||
} | ||
|
||
public Long getWriterId() { | ||
return writerId; | ||
public User getWriter() { | ||
return writer; | ||
} | ||
|
||
public void setWriterId(Long writerId) { | ||
this.writerId = writerId; | ||
public void setWriter(User writerId) { | ||
this.writer = writerId; | ||
} | ||
LenKIM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public Long getQuestionId() { | ||
return questionId; | ||
public Question getQuestion() { | ||
return question; | ||
} | ||
|
||
public void setQuestionId(Long questionId) { | ||
this.questionId = questionId; | ||
public void setQuestion(Question question) { | ||
this.question = question; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 객체의 상태 값을 외부에서 변경할 수 있도록 열어두기 때문에 불필요한
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞습니다..! 불필요한 메소드는 노출하지 않은게 정답이라고 생각합니다. 코드 수정하겠습니다 ㅎㅎ |
||
|
||
public String getContents() { | ||
|
@@ -98,8 +117,8 @@ public void setDeleted(boolean deleted) { | |
public String toString() { | ||
return "Answer{" + | ||
"id=" + id + | ||
", writerId=" + writerId + | ||
", questionId=" + questionId + | ||
", writerId=" + writer + | ||
", questionId=" + question + | ||
", contents='" + contents + '\'' + | ||
", deleted=" + deleted + | ||
'}'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
spring.datasource.url=jdbc:h2:~/test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE | ||
spring.datasource.url=jdbc:h2:~/test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL | ||
spring.datasource.username=sa | ||
spring.h2.console.enabled=true | ||
|
||
logging.level.org.hibernate.type.descriptor.sql = trace | ||
spring.jpa.properties.hibernate.format_sql=true | ||
spring.jpa.show-sql=true | ||
spring.jpa.show-sql=true | ||
|
||
spring.jpa.hibernate.ddl-auto=create-drop |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
테스트를 위한 코드를 도메인에 추가하는 것은 되도록 지양하는게 좋겠네요.
아래 피드백을 참고해주세요! #58 (comment)