Home > Spring > ๐Ÿƒ[Spring] Spring Data JPA๋ฅผ ์ด์šฉํ•ด ๋‹ค์–‘ํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

๐Ÿƒ[Spring] Spring Data JPA๋ฅผ ์ด์šฉํ•ด ๋‹ค์–‘ํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.
Spring Framework

๐Ÿƒ[Spring] Spring Data JPA๋ฅผ ์ด์šฉํ•ด ๋‹ค์–‘ํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

1๏ธโƒฃ findByName

  • ์‚ญ์ œ ๊ธฐ๋Šฅ์„ Spring Data JPA๋กœ ๋งŒ๋“ค์–ด ๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค!
public class UserService {
    
    private final UserRepository userRepository;
    
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    
    // ์‚ญ์ œ ๊ธฐ๋Šฅ.
    public void deleteUser(String name) {
        // SELECT * FROM user WHRER name = ?
        userRepository.findByName(name) // <- ์ด ์ฝ”๋“œ๋Š” ๋นจ๊ฐ„ ๊ธ€์”จ๋กœ ํ‘œ์‹œ๋จ.
    }
}
  • findByName()์ด ๋นจ๊ฐ„ ๊ธ€์”จ๋กœ ํ‘œ์‹œ๋˜๋Š” ์ด์œ ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
    • ๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?
      • UserRepository ์ธํ„ฐํŽ˜์ด์Šค ๋‚ด๋ถ€์— โ€œํ•จ์ˆ˜โ€๋ฅผ ์ •์˜ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
public interface UserRepository extends JpaRepository<User, Long> {
    
    User findByName(String name);
}
  • User findByName(String name);
    • findByName ํ•จ์ˆ˜๋Š” User๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, name์„ parameter(๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.
      • ์ด๋ ‡๊ฒŒ ์ •์˜ํ•˜๊ณ  ๋‚˜์„œ ๋‹ค์‹œ UserService์—์„œ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
public class UserService {
    
    private final UserRepository userRepository;
    
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    
    // ์‚ญ์ œ ๊ธฐ๋Šฅ.
    public void deleteUser(String name) {
        // SELECT * FROM user WHRER name = ?
        User user = userRepository.findByName(name);
        if (user == null) {
            throw new IllegalArgumentException();
        }
        
        userRepository.delete(user);
    }
}
  • findByName(String name)์€ User๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • findByName ํ•จ์ˆ˜์—์„œ User๊ฐ€ ์žˆ๋‹ค๋ฉด User๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์—†๋‹ค๋ฉด Null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์›๋ž˜๋ผ๋ฉด jdbcRepository๋ฅผ ์‚ฌ์šฉํ–ˆ๋”๋ผ๋ฉด DELETE FROM user WHERE name = ?; ์ด ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ๋‚ ๋ ค์คฌ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ํ•˜์ง€๋งŒ Spring Data JPA์—์„œ๋Š” ๊ทธ๋Ÿด ํ•„์š” ์—†์ด delete()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2๏ธโƒฃ Spring Data JPA์—์„œ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ํ†ตํ•ด ์ฟผ๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๊ธฐ.

  • Spring Data JPA์—์„œ๋Š” ์ด๋ฆ„์„ ํ†ตํ•ด ์ฟผ๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์ด ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ํŠน์ •ํ•œ ๊ทœ์น™์— ๋”ฐ๋ผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • Spring Data JPA๋Š” ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ํŒŒ์‹ฑํ•˜์—ฌ ์ฟผ๋ฆฌ๋กœ ๋ณ€ํ™˜ํ•˜๋ฏ€๋กœ, ์˜ฌ๋ฐ”๋ฅธ ๊ทœ์น™์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

1๏ธโƒฃ Spring Data JPA์—์„œ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ์ž‘์„ฑํ•  ๋•Œ์˜ ์ฃผ์š” ๊ทœ์น™.

1๏ธโƒฃ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ.

  • ๋ฉ”์„œ๋“œ ์ด๋ฆ„์€ ๋ณดํ†ต findBy, readBy, queryBy, countBy ๋“ฑ์˜ ์ ‘๋‘์‚ฌ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
    • ๊ทธ ๋’ค์— ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉํ•  ํ•„๋“œ ์ด๋ฆ„์„ CamelCase ๋ฐฉ์‹์œผ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ‘‰ ๊ธฐ๋ณธ ๊ตฌ์กฐ.

[์ ‘๋‘์‚ฌ][์—”ํ‹ฐํ‹ฐ์˜ ํ•„๋“œ ์ด๋ฆ„][์กฐ๊ฑด์‹]

๐Ÿ‘‰ ์˜ˆ์‹œ.

List<User> findByEmail(String email);
List<User> findByNameAndAge(String name, int age);

2๏ธโƒฃ ์ ‘๋‘์‚ฌ.

  • ๋ฉ”์„œ๋“œ ์ด๋ฆ„์€ ๋ณดํ†ต ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ‘๋‘์‚ฌ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
    • findBy : ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
    • readBy : findBy์™€ ๋น„์Šทํ•œ ์—ญํ• ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • queryBy : ๋ฐ์ดํ„ฐ ์กฐํšŒ ์‹œ ์‚ฌ์šฉ.
    • countBy : ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ์—”ํ‹ฐํ‹ฐ ๊ฐœ์ˆ˜๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
    • existsBy : ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
    • deleteBy : ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

3๏ธโƒฃ ์กฐ๊ฑด ์—ฐ๊ฒฐ ํ‚ค์›Œ๋“œ.

  • ์—ฌ๋Ÿฌ ํ•„๋“œ๋ฅผ ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์„ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
    • And : ๋‘ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
    • Or : ๋‘ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ‘‰ ์˜ˆ์‹œ

List<User> findByNameAndAge(String name, int age);
List<User> findByNameOrEmail(String name, String email);

4๏ธโƒฃ ์—ฐ์‚ฐ์ž ํ‚ค์›Œ๋“œ.

  • Spring Data JPA๋Š” ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์ง€ ํ‚ค์›Œ๋“œ๋ฅผ ์ง€์›ํ•˜์—ฌ ํ•„๋“œ ๊ฐ’์— ์กฐ๊ฑด์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Comparison(๋น„๊ต)
      • IsNull, IsNotNull : ๊ฐ’์ด null์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.
      • IsTrue, IsFalse : Boolean ๊ฐ’์ด true ๋˜๋Š” false์ธ์ง€ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.
      • LessThan, LessThanEqual : ๊ฐ’์ด ์ง€์ •๋œ ๊ฐ’๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜, ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
      • GreaterThan, GreaterThanEqual : ๊ฐ’์ด ์ง€์ •๋œ ๊ฐ’๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜, ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
      • Between : ๋‘ ๊ฐ’ ์‚ฌ์ด์— ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
    • String Operations(๋ฌธ์ž์—ด ์—ฐ์‚ฐ)
      • Like : SQL์˜ LIKE์™€ ๊ฐ™์ด ํŠน์ • ๋ฌธ์ž์—ด ํŒจํ„ด์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
      • StartingWith : ํŠน์ • ๋ฌธ์ž์—ด๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
      • EndingWith : ํŠน์ • ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
      • Containing : ํŠน์ • ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
    • Collection Operations(์ปฌ๋ ‰์…˜ ์—ฐ์‚ฐ)
      • In : ์ฃผ์–ด์ง„ ์ปฌ๋ ‰์…˜์— ๊ฐ’์ด ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
      • NotIn : ์ฃผ์–ด์ง„ ์ปฌ๋ ‰์…˜์— ๊ฐ’์ด ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ‘‰ ์˜ˆ์‹œ

List<User> findByAgeGreaterThan(int age);
List<User> findByNameStartingWith(String prefix);
List<User> findByEmailContaining(String keyword);
List<User> findByIdIn(List<Long> ids);

5๏ธโƒฃ ์ •๋ ฌํ•˜๊ธฐ

  • ์ •๋ ฌ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ OrderBy ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„๋“œ ์ด๋ฆ„๊ณผ ๋ฐฉํ–ฅ(Asc ๋˜๋Š” Desc)์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ‘‰ ์˜ˆ์‹œ

List<User> findByNameOrderByAgeAsc(String name);
List<User> findByNameOrderByAgeDesc(String name);

6๏ธโƒฃ ํŽ˜์ด์ง• ๋ฐ ์ •๋ ฌ ํŒŒ๋ผ๋ฏธํ„ฐ

  • Spring Data JPA๋Š” Pageable ๋ฐ Sort ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์›ํ•˜์—ฌ, ๋ฉ”์„œ๋“œ ์ด๋ฆ„์— OrderBy๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ ๋„ ์ •๋ ฌ๊ณผ ํŽ˜์ด์ง•์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰ ์˜ˆ์‹œ

Page<User> findByAgeGreaterThan(int age, Pageable pageable);
List<User> findByName(String name, Sort sort);

7๏ธโƒฃ ๋ณต์žกํ•œ ์ฟผ๋ฆฌ ์ž‘์„ฑ ์˜ˆ์‹œ

  • ๊ทœ์น™์„ ์ ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์กฐ๊ฑด์„ ํฌํ•จํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    List<User> findByNameAndAgeGreaterThan(String name, int age);
    List<User> findByEmailContainingAndIsActiveTrue(String keyword);
    List<User> findByCreatedAtBetween(Date startDate, Date endDate);
    List<User> findByLastNamesStartingWithAndAgeLessThanOrderByAgeDesc(String prefix, int age);
    

3๏ธโƒฃ ์š”์•ฝ.

    1. ์ ‘๋‘์‚ฌ : findBy, countBy, existsBy, deleteBy ๋“ฑ์„ ์‚ฌ์šฉ.
    1. ์กฐ๊ฑด ์—ฐ๊ฒฐ : And, Or์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ์—ฐ๊ฒฐ.
    1. ์—ฐ์‚ฐ์ž : IsNull, IsTrue, LessThan, GreaterThan, Containing, Like, Between ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•„๋“œ์— ๋‹ค์–‘ํ•œ ์กฐ๊ฑด ์ ์šฉ.
    1. ์ •๋ ฌ : OrderBy์™€ Asc ๋˜๋Š” Desc๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ ฌ ์กฐ๊ฑด์„ ์ถ”๊ฐ€.
    1. ํŽ˜์ด์ง•๊ณผ ์ •๋ ฌ ํŒŒ๋ผ๋ฏธํ„ฐ : Pageable๊ณผ Sort ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŽ˜์ด์ง• ๋ฐ ์ •๋ ฌ ๊ฐ€๋Šฅ.