From d3a2fca992148b7a6d98e2af9d515d826ee42662 Mon Sep 17 00:00:00 2001 From: currenjin Date: Tue, 4 Mar 2025 18:04:17 +0900 Subject: [PATCH] JpaDeepDive: DirtyChecking --- .../application/DirtyCheckingService.java | 32 ++++++++++++++ .../dirtycheck/DirtyCheckingBasicTest.java | 43 ++++++++++--------- 2 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 jpa-deep-dive-by-currenjin/src/main/java/com/currenjin/application/DirtyCheckingService.java diff --git a/jpa-deep-dive-by-currenjin/src/main/java/com/currenjin/application/DirtyCheckingService.java b/jpa-deep-dive-by-currenjin/src/main/java/com/currenjin/application/DirtyCheckingService.java new file mode 100644 index 0000000..f036e0c --- /dev/null +++ b/jpa-deep-dive-by-currenjin/src/main/java/com/currenjin/application/DirtyCheckingService.java @@ -0,0 +1,32 @@ +package com.currenjin.application; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.currenjin.domain.Post; +import com.currenjin.infrastucture.PostRepository; + +@Service +public class DirtyCheckingService { + @Autowired + PostRepository postRepository; + + @Transactional + public void updateTitleWithSave(Long postId, String newTitle) { + Post post = postRepository.findById(postId) + .orElseThrow(() -> new IllegalArgumentException("Post not found")); + + post.setTitle(newTitle); + + postRepository.save(post); + } + + @Transactional + public void updateTitleWithoutSave(Long postId, String newTitle) { + Post post = postRepository.findById(postId) + .orElseThrow(() -> new IllegalArgumentException("Post not found")); + + post.setTitle(newTitle); + } +} diff --git a/jpa-deep-dive-by-currenjin/src/test/java/com/currenjin/jpa/dirtycheck/DirtyCheckingBasicTest.java b/jpa-deep-dive-by-currenjin/src/test/java/com/currenjin/jpa/dirtycheck/DirtyCheckingBasicTest.java index 3d2c706..2bfba3b 100644 --- a/jpa-deep-dive-by-currenjin/src/test/java/com/currenjin/jpa/dirtycheck/DirtyCheckingBasicTest.java +++ b/jpa-deep-dive-by-currenjin/src/test/java/com/currenjin/jpa/dirtycheck/DirtyCheckingBasicTest.java @@ -1,28 +1,31 @@ package com.currenjin.jpa.dirtycheck; -import com.currenjin.domain.Post; -import com.currenjin.infrastucture.PostRepository; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.Commit; -import org.springframework.transaction.annotation.Transactional; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; +import com.currenjin.application.DirtyCheckingService; +import com.currenjin.domain.Post; +import com.currenjin.infrastucture.PostRepository; @SpringBootTest public class DirtyCheckingBasicTest { + public static final String NEW_TITLE = "변경된 제목"; @PersistenceContext private EntityManager entityManager; @Autowired private PostRepository postRepository; + @Autowired + private DirtyCheckingService sut; + private Long testPostId; @BeforeEach @@ -36,20 +39,20 @@ public void setUp() { } @Test - @Transactional - void 기본_더티체킹_테스트() { - Post post = entityManager.find(Post.class, testPostId); - String originalTitle = post.getTitle(); + void 더티체킹_없이_수정한다() { + sut.updateTitleWithSave(testPostId, NEW_TITLE); + + Post actual = postRepository.findById(testPostId).get(); - String newTitle = "변경된 제목"; - post.setTitle(newTitle); + assertEquals(NEW_TITLE, actual.getTitle()); + } - entityManager.flush(); - entityManager.clear(); + @Test + void 더티체킹으로_수정한다() { + sut.updateTitleWithoutSave(testPostId, NEW_TITLE); - Post updatedPost = entityManager.find(Post.class, testPostId); + Post actual = postRepository.findById(testPostId).get(); - assertEquals(newTitle, updatedPost.getTitle()); - assertNotEquals(originalTitle, updatedPost.getTitle()); + assertEquals(NEW_TITLE, actual.getTitle()); } } \ No newline at end of file