Git-HUB
https://github.com/braverokmc79/spring_boot_demo2/commits/master
DemoApplication
@EnableJpaAuditing 추가
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 DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
class AbstractEntity
@EntityListeners(AuditingEntityListener.class)
데이터 변경시 @CreatedDate , @LastModifiedDate 를 인식해서 자동으로 생성 및 추가 시켜주는 역할
package net.slipp.domain;
import java.time.LocalDateTime;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
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 Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDateTime getCreateDate() {
return createDate;
}
public void setCreateDate(LocalDateTime createDate) {
this.createDate = createDate;
}
public LocalDateTime getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(LocalDateTime modifiedDate) {
this.modifiedDate = modifiedDate;
}
@Override
public String toString() {
return "AbstractEntity [id=" + id + ", createDate=" + createDate + ", modifiedDate=" + modifiedDate + "]";
}
@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;
}
}
class User
@Entity
public class User extends AbstractEntity{
//널 방지
@Column(nullable=false, length=20)
@JsonProperty
private String userId;
@JsonProperty
private String name;
@JsonProperty
private String email;
@JsonProperty
private String password;
public User() {
}
public User(String userId, String name, String email, String password) {
super();
this.userId = userId;
this.name = name;
this.email = email;
this.password = password;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean matchPassword(String newPassword){
if(newPassword==null){
return false;
}
return this.password.equals(newPassword);
}
public boolean matchId(Long newId){
if(newId==null){
return false;
}
return getId().equals(newId);
}
@Override
public String toString() {
return "User [id=" + getId() + ", userId=" + userId + ", name=" + name + ", email=" + email + ", password="
+ password + "]";
}
}
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;
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;
@Temporal(TemporalType.TIMESTAMP)
private Date datetime;
@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) {
this.datetime = new Date();
this.writer = writer;
this.title = title;
this.contents = contents;
}
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;
}
public void update(String title2, String contents2) {
this.title=title2;
this.contents=contents2;
}
public boolean isSameWriter(User sessionedUser) {
return this.writer.equals(sessionedUser);
}
public Integer getCountOfAnswer() {
return countOfAnswer;
}
public void setCountOfAnswer(Integer countOfAnswer) {
this.countOfAnswer = countOfAnswer;
}
public void addAnswer() {
this.countOfAnswer +=1;
}
public void deleteAnswer(){
this.countOfAnswer -=1;
}
@Override
public String toString() {
return "Question [id=" + getId() + ", writer=" + writer + ", title=" + title + ", contents=" + contents
+ ", datetime=" + datetime + ", countOfAnswer=" + countOfAnswer + ", answers=" + answers + "]";
}
}
댓글 ( 4)
댓글 남기기