Board ( 영속성의 전이 : cascade )
페이지 정보
본문
package web.domain;
import java.util.HashSet;
import java.util.Set;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.*;
@Entity
@Data
@ToString(exclude = "imageSet")
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name= "board")
public class Board extends BaseEntity {
@Id // primary key
@GeneratedValue(strategy = GenerationType.IDENTITY) // auto_increment
private Long bno;
@Column(length = 255, nullable = false) //칼럼의 저장 길이, null 허용 여부
private String title;
@Column(columnDefinition = "TEXT", nullable = false)
private String content;
@Column(length = 20, nullable = false)
private String writer;
//제목, 내용만 수정
public void change(String title, String content) {
this.title = title;
this.content = content;
}
@OneToMany(
mappedBy = "board", //BoardImage의 board변수
cascade = {CascadeType.ALL},
fetch = FetchType.LAZY //지연 로딩
)
@Builder.Default
private Set<BoardImage> imageSet = new HashSet();
public void addImage(String uuid, String fileName) {
BoardImage boardImage = BoardImage.builder()
.uuid(uuid)
.fileName(fileName)
.board(this)
.ord(imageSet.size())
.build();
imageSet.add(boardImage);
}
public void clearImage() {
imageSet.forEach(boardImage -> boardImage.changeBoard(null));
this.imageSet.clear();
}
}
- 이전글● Board와 BoardImage의 CURD 테스트 ( Junit ) 24.06.27
- 다음글① BoardRepositoryTest ( insert ) 24.06.27
댓글목록
등록된 댓글이 없습니다.