Skip to content

Commit 0788681

Browse files
Doyoung KimDoyoung Kim
authored andcommitted
fix: 친구 목록 조회 시 프로필 최신 데이터 반영 안되는 문제 수정
- UserService.refreshUser()에서 EntityManager.detach() 사용하여 JPA 1차 캐시 문제 해결 - 친구가 프로필(닉네임, 아바타 등) 수정 시 즉시 반영되도록 개선
1 parent 3ed7129 commit 0788681

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/main/java/com/taba/user/service/UserService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.taba.user.dto.UserMapper;
1010
import com.taba.user.entity.User;
1111
import com.taba.user.repository.UserRepository;
12+
import jakarta.persistence.EntityManager;
13+
import jakarta.persistence.PersistenceContext;
1214
import lombok.RequiredArgsConstructor;
1315
import lombok.extern.slf4j.Slf4j;
1416
import org.springframework.stereotype.Service;
@@ -25,6 +27,9 @@ public class UserService {
2527
private final FriendshipRepository friendshipRepository;
2628
private final LetterRepository letterRepository;
2729
private final FileService fileService;
30+
31+
@PersistenceContext
32+
private EntityManager entityManager;
2833

2934
@Transactional(readOnly = true)
3035
public UserDto getProfile(String userId) {
@@ -48,12 +53,21 @@ public UserDto getProfile(String userId) {
4853
/**
4954
* User 엔티티를 최신 데이터로 새로고침하여 반환합니다.
5055
* 닉네임이나 프로필 변경사항이 즉시 반영됩니다.
56+
*
57+
* JPA 1차 캐시에서 엔티티를 detach하고 DB에서 새로 로드하여
58+
* 항상 최신 데이터를 반환합니다.
5159
*/
5260
@Transactional(readOnly = true)
5361
public User refreshUser(User user) {
5462
if (user == null || user.getId() == null) {
5563
return user;
5664
}
65+
66+
// 1차 캐시에서 기존 엔티티 제거 (캐시된 오래된 데이터 방지)
67+
if (entityManager.contains(user)) {
68+
entityManager.detach(user);
69+
}
70+
5771
return userRepository.findActiveUserById(user.getId())
5872
.orElse(user);
5973
}

0 commit comments

Comments
 (0)