class Question
다음 코드 만으로 도 JPA 에서 자동으로 답변목록을 가져올 수 있다.
@OneToMany(mappedBy="question")
@OrderBy("id ASC")
private List<Answer> answers;
class Answer
package net.slipp.domain;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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;
@Entity
public class Answer {
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(foreignKey =@ForeignKey(name ="fk_answer_writer"))
private User writer;
@ManyToOne
@JoinColumn(foreignKey=@ForeignKey(name="fk_answer_to_question"))
private Question question;
@Lob
private String contents;
private LocalDateTime createDate;
public Answer(){
}
public Answer(User writer, Question question, String contents){
this.writer=writer;
this.question=question;
this.contents=contents;
this.createDate=LocalDateTime.now();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getWriter() {
return writer;
}
public void setWriter(User writer) {
this.writer = writer;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public LocalDateTime getCreateDate() {
return createDate;
}
public void setCreateDate(LocalDateTime createDate) {
this.createDate = createDate;
}
public String getFormattedCreateDate(){
if(createDate ==null){
return "";
}
return createDate.format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"));
}
@Override
public String toString() {
return "Answer [id=" + id + ", writer=" + writer + ", contents=" + contents + ", createDate=" + createDate
+ "]";
}
@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;
Answer other = (Answer) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
interface AnswerRepository
package net.slipp.domain;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AnswerRepository extends JpaRepository<Answer, Long>{
}
class Question
package net.slipp.domain;
import java.util.Date;
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 javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Question {
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(foreignKey=@ForeignKey(name="fk_question_writer"))
private User writer;
private String title;
@Lob
private String contents;
@Temporal(TemporalType.TIMESTAMP)
private Date datetime;
@OneToMany(mappedBy="question")
@OrderBy("id ASC")
private List<Answer> answers;
public Question() {
}
public Question(User writer, String title, String contents) {
this.datetime = new Date();
this.writer = writer;
this.title = title;
this.contents = contents;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getWriter() {
return writer;
}
public void setWriter(User writer) {
this.writer = writer;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public Date getDatetime() {
return datetime;
}
public void setDatetime(Date datetime) {
this.datetime = datetime;
}
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
@Override
public String toString() {
return "Question [id=" + id + ", writer=" + writer + ", title=" + title + ", contents=" + contents
+ ", datetime=" + datetime + "]";
}
public void update(String title2, String contents2) {
this.title=title2;
this.contents=contents2;
}
public boolean isSameWriter(User sessionedUser) {
return this.writer.equals(sessionedUser);
}
}
class Question
package net.slipp.domain;
import java.util.Date;
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 javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Question {
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(foreignKey=@ForeignKey(name="fk_question_writer"))
private User writer;
private String title;
@Lob
private String contents;
@Temporal(TemporalType.TIMESTAMP)
private Date datetime;
@OneToMany(mappedBy="question")
@OrderBy("id ASC")
private List<Answer> answers;
public Question() {
}
public Question(User writer, String title, String contents) {
this.datetime = new Date();
this.writer = writer;
this.title = title;
this.contents = contents;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getWriter() {
return writer;
}
public void setWriter(User writer) {
this.writer = writer;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public Date getDatetime() {
return datetime;
}
public void setDatetime(Date datetime) {
this.datetime = datetime;
}
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
@Override
public String toString() {
return "Question [id=" + id + ", writer=" + writer + ", title=" + title + ", contents=" + contents
+ ", datetime=" + datetime + "]";
}
public void update(String title2, String contents2) {
this.title=title2;
this.contents=contents2;
}
public boolean isSameWriter(User sessionedUser) {
return this.writer.equals(sessionedUser);
}
}
class QuestionController
@Controller
@RequestMapping("/questions")
public class QuestionController {
private static final Logger log = LoggerFactory.getLogger(QuestionController.class);
@Autowired
private QuestionRepository questionRepository;
@GetMapping("/{id}")
public String show(@PathVariable Long id, Model model){
Question question=questionRepository.findOne(id);
model.addAttribute("question", question);
model.addAttribute("answers", question.getAnswers());
return "/qna/show";
}
// 생략
}
show.html
<div class="qna-comment">
<div class="qna-comment-slipp">
<p class="qna-comment-count"><strong>2</strong>개의 의견</p>
<div class="qna-comment-slipp-articles">
{{#answers}}
<article class="article" id="answer-1405">
<div class="article-header">
<div class="article-header-thumb">
<img src="https://graph.facebook.com/v2.3/1324855987/picture" class="article-author-thumb" alt="">
</div>
<div class="article-header-text">
<a href="/users/1/자바지기" class="article-author-name">{{writer.userId}}</a>
<a href="#answer-1434" class="article-header-time" title="퍼머링크">
{{getFormattedCreateDate}}
</a>
</div>
</div>
<div class="article-doc comment-doc">
<p>{{contents}}</p>
</div>
<div class="article-util">
<ul class="article-util-list">
<li>
<a class="link-modify-article" href="/questions/413/answers/1405/form">수정</a>
</li>
<li>
<form class="delete-answer-form" action="/questions/413/answers/1405" method="POST">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="delete-answer-button">삭제</button>
</form>
</li>
</ul>
</div>
</article>
{{/answers}}
<form class="submit-write" method="post" action="/questions/{{id}}/answers">
<div class="form-group" style="padding:14px;">
<textarea class="form-control" placeholder="Update your status" name="contents"></textarea>
</div>
<input type="submit" class="btn btn-success pull-right" value="답변하기">
<div class="clearfix" />
</form>
</div>
</div>
</div>
댓글 ( 4)
댓글 남기기