Skip to content

Commit

Permalink
feat: Specifications 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jmxx219 committed Jul 1, 2024
1 parent 93183c0 commit 7b1ae2c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -18,7 +19,8 @@
import study.datajpa.dto.MemberDto;
import study.datajpa.entity.Member;

public interface MemberRepository extends JpaRepository<Member, Long>, MemberRepositoryCustom {
public interface MemberRepository extends JpaRepository<Member, Long>, MemberRepositoryCustom,
JpaSpecificationExecutor<Member> {

/** 메소드 이름으로 쿼리 생성 **/
List<Member> findByUsernameAndAgeGreaterThan(String username, int age);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package study.datajpa.repository;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;
import study.datajpa.entity.Member;
import study.datajpa.entity.Team;

public class MemberSpec {

public static Specification<Member> teamName(final String teamName) {
return (Specification<Member>) (root, query, builder) -> {
if (StringUtils.isEmpty(teamName)) {
return null;
}
Join<Member, Team> t = root.join("team", JoinType.INNER); //회원과 조인
return builder.equal(t.get("name"), teamName);
};
}

public static Specification<Member> username(final String username) {
return (Specification<Member>)(root, query, builder) ->
builder.equal(root.get("username"), username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -285,4 +286,26 @@ public void callCustom() {
List<Member> memberCustom = memberRepository.findMemberCustom();
}

@Test
public void specBasic() throws Exception {
//given
Team teamA = new Team("teamA");
em.persist(teamA);

Member m1 = new Member("m1", 0, teamA);
Member m2 = new Member("m2", 0, teamA);
em.persist(m1);
em.persist(m2);

em.flush();
em.clear();

//when
Specification<Member> spec = MemberSpec.username("m1").and(MemberSpec.teamName("teamA"));
List<Member> result = memberRepository.findAll(spec);

//then
assertThat(result.size()).isEqualTo(1);
}

}

0 comments on commit 7b1ae2c

Please sign in to comment.