Spring Data JPA

img

JPA ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” Repository๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 4๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  • Repository๋Š” CRUD ๊ธฐ๋Šฅ์™ธ์˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.

@Indexed
public interface Repository<T, ID> {

}
  • CrudRepository๋Š” ๊ธฐ๋ณธ์ ์ธ CRUD ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ค€๋‹ค.

public interface CrudRepository<T, ID> extends Repository<T, ID> {

  <S extends T> S save(S entity);      

  Optional<T> findById(ID primaryKey); 

  Iterable<T> findAll();               

  long count();                        

  void delete(T entity);               

  boolean existsById(ID primaryKey);   

  // โ€ฆ more functionality omitted.
}
  • PagingAndSortingRepository : CRUD + ํŽ˜์ด์ง•๊ณผ sorting ๊ธฐ๋Šฅ ์ œ๊ณต

public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
  Iterable<T> findAll(Sort sort);
  Page<T> findAll(Pageable pageable);
}
  • JpaRepository๋Š” PagingAndSortingRepository + JPA๊ด€๋ จ ํŠนํ™” ๊ธฐ๋Šฅ๋“ค(flusing, ๋ฐฐ์น˜์„ฑ ์ž‘์—…) ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ค€๋‹ค.

Query Creation

์‚ฌ์šฉ์ž๊ฐ€ Repository ์ธํ„ฐํŽ˜์ด์Šค์— ์ •ํ•ด์ง„ ๊ทœ์น™๋Œ€๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, Spring์ด ์•Œ์•„์„œ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์— ์ ํ•ฉํ•œ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆฌ๋Š” ๊ตฌํ˜„์ฒด๋ฅผ ๋งŒ๋“ค์–ด Bean์œผ๋กœ ๋“ฑ๋กํ•ด์ค€๋‹ค. ์ด๋•Œ ์ •ํ•ด์ง„ ๊ทœ์น™์€ ์•„๋ž˜ ํ‘œ๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ์ข‹๋‹ค.

Keyword

Sample

JPQL snippet

And

findByLastnameAndFirstname

โ€ฆ where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

โ€ฆ where x.lastname = ?1 or x.firstname = ?2

Is, Equals

findByFirstname,findByFirstnameIs,findByFirstnameEquals

โ€ฆ where x.firstname = ?1

Between

findByStartDateBetween

โ€ฆ where x.startDate between ?1 and ?2

LessThan

findByAgeLessThan

โ€ฆ where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

โ€ฆ where x.age <= ?1

GreaterThan

findByAgeGreaterThan

โ€ฆ where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

โ€ฆ where x.age >= ?1

After

findByStartDateAfter

โ€ฆ where x.startDate > ?1

Before

findByStartDateBefore

โ€ฆ where x.startDate < ?1

IsNull, Null

findByAge(Is)Null

โ€ฆ where x.age is null

IsNotNull, NotNull

findByAge(Is)NotNull

โ€ฆ where x.age not null

Like

findByFirstnameLike

โ€ฆ where x.firstname like ?1

NotLike

findByFirstnameNotLike

โ€ฆ where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

โ€ฆ where x.firstname like ?1 (parameter bound with appended %)

EndingWith

findByFirstnameEndingWith

โ€ฆ where x.firstname like ?1 (parameter bound with prepended %)

Containing

findByFirstnameContaining

โ€ฆ where x.firstname like ?1 (parameter bound wrapped in %)

OrderBy

findByAgeOrderByLastnameDesc

โ€ฆ where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

โ€ฆ where x.lastname <> ?1

In

findByAgeIn(Collection ages)

โ€ฆ where x.age in ?1

NotIn

findByAgeNotIn(Collection ages)

โ€ฆ where x.age not in ?1

True

findByActiveTrue()

โ€ฆ where x.active = true

False

findByActiveFalse()

โ€ฆ where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

โ€ฆ where UPPER(x.firstame) = UPPER(?1)

Native Queries

@Query ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์›๋ž˜ ์ฟผ๋ฆฌ๋ฌธ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
  User findByEmailAddress(String emailAddress);
}
public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
    countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
    nativeQuery = true)
  Page<User> findByLastname(String lastname, Pageable pageable);
}

์˜ˆ์‹œ

public interface MemberRepository extends CrudRepository<Member, String>{
}

์ด๋ ‡๊ฒŒ ์ƒ์†๋งŒ ํ•˜๋ฉด, ๊ธฐ๋ณธ์ ์ธ CrudRepository์˜ ๊ธฐ๋Šฅ์„ ๋‹ค ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. CRUD์— ์ถ”๊ฐ€์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์€ ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฉด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ ๋ณ„๋„๋กœ @Repository ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

@Service
public class MemeberServiceImpl implements MemberService {

    @Autowired
    MemberRepository memberRepository; 

    public boolean isEnrolledMember(String mbrId) {

        // mbrId(ID) ๊ธฐ์ค€์œผ๋กœ ํšŒ์› ์—ฌ๋ถ€ ํ™•์ธ(์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false)
        boolean isEnrolledMember = memberRepository.existsById(mbrId);         
        return isEnrolledMember;
    }
}

์ฐธ์กฐ

Last updated

Was this helpful?