spring data jpa : Query By Example
This commit is contained in:
@@ -9,25 +9,25 @@ import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
|
||||
public class MemberSpec {
|
||||
|
||||
public static Specification<Member> teamName(final String teamName) {
|
||||
return (root, query, criteriaBuilder) -> {
|
||||
|
||||
if (ObjectUtils.isEmpty(teamName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Join<Member, Team> t = root.join("team", JoinType.INNER);// 회원과 조인
|
||||
Join<Member, Team> t = root.join("team", JoinType.INNER); //회원과 조인
|
||||
return criteriaBuilder.equal(t.get("name"), teamName);
|
||||
};
|
||||
}
|
||||
|
||||
public static Specification<Member> username(final String username) {
|
||||
return (root, query, criteriaBuilder) -> {
|
||||
if (ObjectUtils.isEmpty(username)) {
|
||||
return null;
|
||||
}
|
||||
return criteriaBuilder.equal(root.get("name"), username);
|
||||
|
||||
return criteriaBuilder.equal(root.get("username"), username);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
@Transactional
|
||||
@Rollback(value = false) // 롤백 false
|
||||
//@Rollback(value = false) // 롤백 false
|
||||
class MemberJpaRepositoryTest {
|
||||
|
||||
@Autowired MemberJpaRepository memberJpaRepository;
|
||||
|
||||
@@ -7,10 +7,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -25,7 +22,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
@Transactional
|
||||
@Rollback(value = false)
|
||||
//@Rollback(value = false)
|
||||
public class MemberRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
@@ -314,7 +311,7 @@ public class MemberRepositoryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void specBasic() {
|
||||
void specBasic() throws Exception{
|
||||
// given
|
||||
Team teamA = new Team("teamA");
|
||||
em.persist(teamA);
|
||||
@@ -333,5 +330,33 @@ public class MemberRepositoryTest {
|
||||
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryByExample() {
|
||||
// given
|
||||
Team teamA = new Team("teamA");
|
||||
em.persist(teamA);
|
||||
|
||||
Member member1 = new Member("member1", 0, teamA);
|
||||
Member member2 = new Member("member2", 0, teamA);
|
||||
em.persist(member1);
|
||||
em.persist(member2);
|
||||
|
||||
em.flush();
|
||||
em.clear();
|
||||
|
||||
// when
|
||||
Member member = new Member("member1");
|
||||
Team team = new Team("teamA");
|
||||
member.setTeam(team);
|
||||
|
||||
ExampleMatcher matcher = ExampleMatcher.matching()
|
||||
.withIgnorePaths("age");
|
||||
Example<Member> example = Example.of(member, matcher);
|
||||
|
||||
List<Member> result = memberRepository.findAll(example);
|
||||
|
||||
assertEquals("member1", result.get(0).getUsername());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user