Skip to content

Commit 969baec

Browse files
committed
fix: fetch user roles before update
1 parent c8f4f5c commit 969baec

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

src/main/java/com/github/throyer/common/springboot/domain/user/repository/UserRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public interface UserRepository {
1313
Page<User> findAll(Pageable pageable);
1414
Optional<User> findById(Long id);
15+
Optional<User> findByIdFetchRoles(Long id);
1516
Optional<User> findByEmail(String email);
1617
Boolean existsByEmail(String email);
1718
void deleteById(Long id);

src/main/java/com/github/throyer/common/springboot/domain/user/repository/UserRepositoryImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public Optional<User> findById(Long id) {
3636
return queryNative.findById(id);
3737
}
3838

39+
@Override
40+
public Optional<User> findByIdFetchRoles(Long id) {
41+
return this.springData.findByIdFetchRoles(id);
42+
}
43+
3944
@Override
4045
public Optional<User> findByEmail(String email) {
4146
return queryNative.findByEmail(email);

src/main/java/com/github/throyer/common/springboot/domain/user/repository/springdata/SpringDataUserRepository.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@ default void deleteAll(Iterable<? extends User> entities) {
3434

3535
Boolean existsByEmail(String email);
3636

37-
Optional<User> findOptionalByEmail(String email);
37+
@Query("""
38+
select user from #{#entityName} user
39+
left join fetch user.roles
40+
where user.id = ?1
41+
""")
42+
Optional<User> findByIdFetchRoles(Long id);
3843
}

src/main/java/com/github/throyer/common/springboot/domain/user/service/UpdateUserService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public User update(Long id, UpdateUserProps body) {
2828
.orElseThrow(() -> unauthorized(message(NOT_AUTHORIZED_TO_MODIFY, "'user'")));
2929

3030
var actual = repository
31-
.findById(id)
31+
.findByIdFetchRoles(id)
3232
.orElseThrow(() -> notFound("User not found"));
3333

3434
validateEmailUniquenessOnModify(body, actual);

0 commit comments

Comments
 (0)