Skip to content

Commit

Permalink
add more example work with new Criteria Api methods and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Freeongoo committed Jun 15, 2019
1 parent 7721fce commit 13a3030
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
public interface CommentDao extends BaseDao<Comment, Long> {

public List<Comment> findByPostId(Long postId);

public List<Comment> findNotInPassedIds(List<Long> ids);

public List<Comment> findLikeName(String partOfName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import hello.entity.oneToMany.Post;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.*;
import java.util.List;

@Repository
Expand All @@ -25,7 +22,6 @@ public List<Comment> findByPostId(Long postId) {
CriteriaQuery<Comment> criteriaQuery = getCriteriaQuery();

Root<Comment> root = getRoot(criteriaQuery);
criteriaQuery.select(root);

Join<Comment, Post> joinPost = root.join("post");
Predicate predicate = getCriteriaBuilder().equal(joinPost.get("id"), postId);
Expand All @@ -38,4 +34,38 @@ public List<Comment> findByPostId(Long postId) {
.createQuery(criteriaQuery)
.getResultList();
}

@Override
public List<Comment> findNotInPassedIds(List<Long> ids) {
CriteriaQuery<Comment> criteriaQuery = getCriteriaQuery();
Root<Comment> root = getRoot(criteriaQuery);
Path<Object> fieldId = root.get("id");
Predicate in = fieldId.in(ids);
Predicate predicate = getCriteriaBuilder().not(in);

criteriaQuery
.select(root)
.where(predicate);

return getSession()
.createQuery(criteriaQuery)
.getResultList();
}

@Override
public List<Comment> findLikeName(String partOfName) {
CriteriaQuery<Comment> criteriaQuery = getCriteriaQuery();
Root<Comment> root = getRoot(criteriaQuery);
Path<String> fieldName = root.get("name");
String s = "%" + partOfName + "%";
Predicate predicate = getCriteriaBuilder().like(fieldName, s);

criteriaQuery
.select(root)
.where(predicate);

return getSession()
.createQuery(criteriaQuery)
.getResultList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ public void findByPostId() {
}
}

@Test
public void findNotInPassedIds() {
List<Long> ids = new ArrayList<>();
ids.add(1L);
ids.add(2L);
ids.add(3L);
ids.add(4L);
ids.add(5L);
ids.add(6L);
ids.add(7L);
List<Comment> comments = commentDao.findNotInPassedIds(ids);

assertThat(comments.size(), equalTo(1));
Comment comment = comments.iterator().next();
assertThat(comment.getId(), equalTo(8L));
}

@Test
public void findLikeName() {
List<Comment> comments = commentDao.findLikeName("omm");

assertThat(comments.size(), equalTo(7));
}

@Test
public void getByProps_WhenFieldEqualNull() {
Map<String, List<?>> props = new HashMap<>();
Expand Down

0 comments on commit 13a3030

Please sign in to comment.