Dev/JPA

JPA ์ฟผ๋ฆฌ๋ฉ”์†Œ๋“œ(QueryMethod)

OK-๊ฐ€์ž 2022. 3. 2. 17:23

๐Ÿค” ์‹ค์ œ๋กœ ์„œ๋น„์Šค์—์„œ๋Š” WHERE ์กฐ๊ฑด์ด ๋ณต์žกํ• ํƒ ๋ฐ...์–ด๋–ปํ•˜์ง€?

QueryMethod ํ™œ์šฉ

JPA ์‚ฌ์šฉํ•  ๋•Œ QueryMethod๊ฐ€ ๋งŽ์€ ํ•ดํƒ์„ ๋ฐ›๋Š”๋‹ค. ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ์— ๋„ค์ด๋ฐ๋ฒ ์ด์Šค๋กœ ์„ ์–ธ๋งŒ ํ•˜๋ฉด Query๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ์ˆ˜์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋จผ์ € QueryMethod๊ฐ€ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์–ด ์ง€๊ณ  ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ์•„์•ผํ•œ๋‹ค.

TYPE


โœ… ๋จผ์ € UserRepository์— ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ 

public interface UserRepository extends JpaRepository<User, Long> {

    User findByName(String name); //์ด๋ฆ„์„ ํ†ตํ•ด ์œ ์ € ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ.
}

โœ… Test ํ•ด๋ณด์ž

    @Test
    void select() {
        System.out.println(userRepository.findByName("martin"));
    }

๐Ÿ˜ˆ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์€ ๋‹น์—ฐํ•˜๋‹ค ๊ฒ€์ƒ‰ํ•œ martin์€ 2๋ช…์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

โœ… ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ๊ณ  ํ•ด๋ณด์ž.

List<User> findByName(String name); //์ด๋ฆ„์„ ํ†ตํ•ด ์œ ์ € ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ.

โœ… ์ž˜๋‚˜์™”๋‹ค.

[User(id=1, name=martin, email=martin@fast.com, createdAt=2022-02-04T00:58:41.588, updatedAt=2022-02-04T00:58:41.588), 
User(id=5, name=martin, email=james@another.com, createdAt=2022-02-04T00:58:41.603, updatedAt=2022-02-04T00:58:41.603)]

๐Ÿค”โœ…๐Ÿ‘‰๐Ÿ˜Ž๐Ÿคš๐Ÿ˜ฎ

๐Ÿคš JpaRepository์—์„œ, ์ฆ‰ ์ฟผ๋ฆฌ๋ฉ”์„œ๋“œ์—์„œ๋Š” ๋ฆฌํ„ดํƒ€์ž…์„ ๊ณ ์ •ํ•ด์•ผํ•˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฆฌํ„ด์ด 1๊ฐœ or N๊ฐœ์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ ๋ฆฌํ„ดํƒ€์ž…์„์„ ์ œ๊ณตํ•ด์ค€๋‹ค. ์–ด๋–ค ํƒ€์ž…๋“ค์„ ์ œ๊ณตํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž.

๐Ÿ˜ฎ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ œ๊ณตํ•˜๋Š” ์žฅ์ ๋„ ์žˆ์ง€๋งŒ ๋Ÿฐํƒ€์ž„(์‹คํ–‰)ํ•  ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋‚ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ •ํ™•ํ•˜๊ฒŒ ์ง€์ •ํ•ด์ฃผ๋Š”๊ฒƒ์ด ์ข‹๋‹ค.

๐Ÿค” NAMEING ํ•จ์ˆ˜ ์ด๋ฆ„์€ ์–ด๋–ป๊ฒŒ ์ง€์„๊นŒ?

JPA์—์„œ๋Š” ๊ฐ€๋…์„ฑ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ง€ํ–ฅํ•ด ์–ด๋–ค์ด๋ฆ„์œผ๋กœ ํ•ด๋„ ํ•จ์ˆ˜๋ฅผ ์ง€์ •ํ•ด๋„๋œ๋‹ค.

๐Ÿ˜ˆ ํ•˜์ง€๋งŒ ์—”ํ‹ฐํ‹ฐ์— ํƒ€์ž…์ด ๋ช…์‹œ๋˜์–ด ์žˆ์œผ๋ฉด findUserByName getUsersByID ๊ฐ™์€ ์ ‘๋‘์‚ฌ ์•ž์— User๋ผ๋Š” ๋ช…์‹œ๋Š” ํ•„์š”์—†๋‹ค.
๐Ÿ˜ˆ ๋˜ํ•œ findByByName ์ด๋ผ๊ณ  ์ณ๋„ Test๋Š” ํ†ต๊ณผํ•˜์ง€๋งŒ ์‹คํ–‰์ค‘์— ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์˜ค๋‹ˆ ์™ ๋งŒํ•˜๋ฉด ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ์œ„์˜ ์‚ฌ์ง„๋Œ€๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ผญ Test ํ•ด๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

โœ… select๋ฌธ ํ•œ๋ฒˆ ๋งŒ๋“ค์–ด๋ณด์ž.

    User findByEmail(String email);
    User getByEmail(String email);
    User readByEmail(String email);
    User queryByEmail(String email);
    User searchByEmail(String email);
    User streamByEmail(String email);

    User findUserByEmail(String email);       // ๋น„์ถ”. User๋Š” ์ด๋ฏธ ๋ช…์‹œ๋˜์–ด์žˆ๋‹ค.
    User findSomethingByEmail(String email);  // ์ด๋ ‡๊ฒŒ ํ•ด๋„ ๋˜์ง€๋งŒ, ๋น„์ถ”. ๋ช…์‹œ์ ์œผ๋กœ ํ•˜์ž
    @Test
    void select() {
        System.out.println(userRepository.findByName("martin"));
        System.out.println("findByEmail : " + userRepository.findByEmail("martin@fast.com"));
        System.out.println("getByEmail : " + userRepository.getByEmail("martin@fast.com"));
        System.out.println("readByEmail : " + userRepository.readByEmail("martin@fast.com"));
        System.out.println("queryByEmail : " + userRepository.queryByEmail("martin@fast.com"));
        System.out.println("searchByEmail : " + userRepository.searchByEmail("martin@fast.com"));
        System.out.println("streamByEmail : " + userRepository.streamByEmail("martin@fast.com"));
        System.out.println("findUserByEmail : " + userRepository.findUserByEmail("martin@fast.com"));
        System.out.println("findSomethingByEmail : " + userRepository.findSomethingByEmail("martin@fast.com"));

    }

๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ํ•จ์ˆ˜์ด๋ฆ„์€ ๋‹ฌ๋ผ๋„ ์ฟผ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค.

โœ… ์กฐ๊ธˆ ๋” ์‹ค์Šตํ•ด ๋ณด์ž. First,Top์„ ์‚ฌ์šฉํ•ด๋ณด์ž. ๊ทธ๋ฆฌ๊ณ  Last๋„ ํ•œ๋ฒˆ ์‹คํ–‰ํ•ด๋ณด์ž.

UserRepository
    List<User> findFirst2ByName(String name);

    List<User> findTop2ByName(String name);

    List<User> findLast1ByName(String name);
UserRepositoryTest
        System.out.println("findTop2ByName : " + userRepository.findTop2ByName("martin"));
        System.out.println("findFirst2ByName : " + userRepository.findFirst2ByName("martin"));
        System.out.println("findLast1ByName : " + userRepository.findLast1ByName("martin"));

๐Ÿ‘‰ martin์„ ์กฐ๊ฑด์œผ๋กœ Limit๋ฅผ ๊ฑธ์–ด์„œ 2๋ช…์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ตฟ๊ตฟ!!

๐Ÿคš ํ•˜์ง€๋งŒ findLast1ByName์€ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ–ˆ์„๊นŒ?

๐Ÿ‘‰ Test๋Š” ํ†ต๊ณผํ•˜์˜€์ง€๋งŒ ์ •์ƒ ์ž‘๋™์€ ์•„๋‹ˆ๋‹ค. Last ๋ผ๋Š” Keyword๋Š” JPA์—์„œ ๊ฐ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ผ๋ฐ˜ select์ฟผ๋ฆฌ๋ฅผ ๋ฑ‰์–ด๋ƒˆ๋‹ค.

๐Ÿคš ์ฐธ๊ณ ์‚ฌํ•ญ์œผ๋กœ... Last๋ฅผ ๋ฝ‘์œผ๋ ค๋ฉด OrderBy๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ญ์ˆœ์ •๋ ฌํ•˜๊ณ  first1์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.


๐Ÿค” Where ์กฐ๊ฑด๋“ค์ด ๊ฐ„๋‹จํ•˜์ง€ ์•Š์„ํ…๋ฐ....

๊ทธ๋ž˜์„œ ์•„๋ž˜ ์‚ฌ์ง„์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

โœ… ์‹ค์Šตํ•ด๋ณด์ž

๐Ÿ‘‰ findByEmailAndName

// AND
List<User> findByEmailAndName(String email, String name);

// OR
List<User> findByEmailOrName(String email, String name);

// After('>',~๋ณด๋‹ค ํฐ๊ฒƒ), Before('<',~๋ณด๋‹ค ์ž‘์€ ๊ฒƒ)
List<User> findByCreatedAtAfter(LocalDateTime yesterday);
List<User> findByIdAfter(Long id);

// GreaterThan ==  (After Before์™€ ๊ฐ™์ง€๋งŒ, ์ข€ ๋” ๋ฒ”์šฉ์ ์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ๋ชจ๋“  ์ˆซ์ž ๊ฐ’ ๋‚ ์งœ ๊ฐ’ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.)
List<User> findByCreatedAtGreaterThan(LocalDateTime yesterday);

// GreaterThanEqual('์ด์ƒ','>=') (์ฐธ๊ณ ๋กœ Before,After๋Š” ์ดˆ๊ณผ,๋ฏธ๋งŒ ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.)
List<User> findByCreatedAtGreaterThanEqual(LocalDateTime yesterday);

// Between (~์™€ ~์‚ฌ์ด) (์–‘๋์˜ ๊ฐ’์„ ํฌํ•จํ•œ๋‹ค!!!!)
List<User> findByCreatedAtBetween(LocalDateTime yesterday, LocalDateTime tomorrow);
List<User> findByIdBetween(Long id1, Long id2);

// (Between์„ ํ’€์–ด์„œ ์“ด ๊ฒƒ) GreaterThanEqual And LessThanEqual (~์ด์ƒ ๊ทธ๋ฆฌ๊ณ  ~์ดํ•˜) 
List<User> findByIdGreaterThanEqualAndIdLessThanEqual(Long id1, Long id2);

// IsNotNull (๋นˆ ๊ฐ’์ด ์•„๋‹Œ ๊ฒƒ ์ฐพ๊ธฐ) (IsNull์€ ๋นˆ ๊ฐ’ ์ฐพ๊ธฐ)
List<User> findByIdIsNotNull();

// In ('๋ฌธ์ž'๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ์Šคํ‚ค๋งˆ ์ฐพ๊ธฐ) (or์กฐ๊ฑด๊ณผ ๋น„์Šทํ•˜๋‹ค)
List<User> findByNameIn(List<String> names);

// StartingWith ('?' ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ์Šคํ‚ค๋งˆ ์ฐพ๊ธฐ)
List<User> findByNameStartingWith(String name);
// EndingWith ('?' ์œผ๋กœ ๋๋‚˜๋Š” ๋ฌธ์ž๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ์Šคํ‚ค๋งˆ ์ฐพ๊ธฐ)
List<User> findByNameEndingWith(String name);
// Contains ('?' ๊ธ€์ž๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ์Šคํ‚ค๋งˆ ์ฐพ๊ธฐ)
List<User> findByNameContains(String name);

// Like ( ์œ„ 3๊ฐœ ์ฟผ๋ฆฌ๋ฅผ ๋ณตํ•ฉ์ ์œผ๋กœ ์‚ฌ์šฉ , %A [A๋กœ ๋] , A% [A๋กœ ์‹œ์ž‘] , %A% [Aํฌํ•จ]
List<User> findByNameLike(String name);

// Is ,Equals (ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์ผ์น˜ํ•˜๋Š” ์ฟผ๋ฆฌ)(์ฝ”๋“œ ๊ฐ€๋…์„ฑ์„ ๋†’ํ˜€์ค€๋‹ค.)
Set<User> findUserByNameIs(String name);
Set<User> findUserByNameEquals(String name);
System.out.println("findByEmailAndName : " + userRepository.findByEmailAndName("martin@fast.com", "martin"));
System.out.println("findByEmailOrName : " + userRepository.findByEmailOrName("martin@fast.com", "dennis"));
System.out.println("findByCreatedAtAfter : " + userRepository.findByCreatedAtAfter(LocalDateTime.now().minusDays(1L)));
System.out.println("findByIdAfter : " + userRepository.findByIdAfter(4L));
System.out.println("findByCreatedAtGreaterThan : " + userRepository.findByCreatedAtGreaterThan(LocalDateTime.now().minusDays(1L)));
System.out.println("findByCreatedAtGreaterThanEqual : " + userRepository.findByCreatedAtGreaterThanEqual(LocalDateTime.now().minusDays(1L)));
System.out.println("findByCreatedAtBetween : " + userRepository.findByCreatedAtBetween(LocalDateTime.now().minusDays(1L), LocalDateTime.now().plusDays(1L)));
System.out.println("findByIdBetween : " + userRepository.findByIdBetween(1L, 3L));
System.out.println("findByIdGreaterThanEqualAndIdLessThanEqual : " + userRepository.findByIdGreaterThanEqualAndIdLessThanEqual(1L, 3L));
System.out.println("findByIdIsNotNull : " + userRepository.findByIdIsNotNull());
System.out.println("findByNameIn : " + userRepository.findByNameIn(Lists.newArrayList("martin", "dennis")));
System.out.println("findByNameStartingWith : " + userRepository.findByNameStartingWith("mar"));
System.out.println("findByNameEndingWith : " + userRepository.findByNameEndingWith("tin"));
System.out.println("findByNameContains : " + userRepository.findByNameContains("art"));
System.out.println("findByNameLike : " + userRepository.findByNameLike("%" + "art" + "%"));