Skip to content

Commit

Permalink
JpaDeepDive: DirtyChecking
Browse files Browse the repository at this point in the history
  • Loading branch information
currenjin committed Mar 4, 2025
1 parent 2713cbb commit d3a2fca
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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());
}
}

0 comments on commit d3a2fca

Please sign in to comment.