Dirty란 상태의 변화가 생긴 정도
Dirty Checking은 상태 변경 검사
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class BookService {
@Transactional
public void updateBookContents(String isbn, String contents){
Book book = bookRepository.findByIsbn(isbn);
book.updateContents(contents);
}
}
@Test
@DisplayName("도서_내용_수정")
void 도서_내용_수정(){
//give
String isbn = "11111111";
Book book = bookRepository.save(Book.builder()
.isbn(isbn)
.bookName("SEMO")
.author("SEMO")
.publisher("hDream")
.contents("semosemo")
.kdc("800")
.category("800")
.keyword("800")
.img("http://image.kyobobook.co.kr/images/book/large/924/l9788901214924.jpg")
.build());
//when
String updateContents = "ABC";
bookService.updateBookContents(isbn, updateContents);
//then
Book updateBookData = bookRepository.findByIsbn(isbn);
assertThat(updateBookData.getContents(),is(updateContents));
}
Dirty Checking으로 생성되는 update뭐리는 기본적으로 모든 필드를 update
JPA는 전체 필드를 업데이트하는 방식을 기본값으로 한다.
@DynamicUpdate
로 변경 필드만 변경 가능.Entity
@Entity
@Getter
@NoArgsConstructor
@DynamicUpdate //변경된 필드만 적용
public class Book {
@Id
@Column(name = "ISBN")
private String isbn;
@Column(name = "BOOK_NAME")
private String bookName;
@Column(name = "AUTHOR")
private String author;
@Column(name = "PUBLISHER")
private String publisher;
@Column(name = "contents")
private String contents;
@Column(name = "KDC")
private String kdc;
@Column(name = "CATEGORY")
private String category;
@Column(name = "KEYWORD")
private String keyword;
@Column(name = "BOOK_IMAGE")
private String img;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
private List<BookReview> bookReviewList = new ArrayList<>();
@Builder
public Book(String isbn, String bookName, String author, String publisher,
String kdc, String category, String keyword, String img, String contents) {
this.isbn = isbn;
this.bookName = bookName;
this.author = author;
this.publisher = publisher;
this.kdc = kdc;
this.category = category;
this.keyword = keyword;
this.img = img;
this.contents = contents;
}
/**
* udpate contetns
*
* @author hyunho
* @since 2021/08/20
**/
public void updateContents(String contents){
this.contents = contents;
}
}