-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookRepositoryTest.java
57 lines (42 loc) · 2.2 KB
/
BookRepositoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package io.github.scordio.playground;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
@DataJpaTest(showSql = false) // use P6Spy
class BookRepositoryTest {
@Autowired
private BookRepository underTest;
@Autowired
private AuthorRepository authorRepository;
@Autowired
private EntityManager entityManager;
@Test
void findAllByAuthorId() {
// insert into author (name, id) values ('Homer', 1);
Author homer = authorRepository.save(new Author("Homer"));
// insert into book (author_id, name, id) values (1, 'Iliad', 2);
underTest.save(new Book("Iliad", homer));
// insert into book (author_id, name, id) values (1, 'Odyssey', 3);
underTest.save(new Book("Odyssey", homer));
// select book0_.id as id1_1_, book0_.author_id as author_i3_1_, book0_.name as name2_1_ from book book0_ left outer join author author1_ on book0_.author_id=author1_.id where author1_.id=1;
underTest.findAllByAuthorId(homer.getId());
// select book0_.id as id1_1_, book0_.author_id as author_i3_1_, book0_.name as name2_1_ from book book0_ where book0_.author_id=1;
underTest.findAllByAuthorIdWithJPQL(homer.getId());
// select book0_.id as id1_1_, book0_.author_id as author_i3_1_, book0_.name as name2_1_ from book book0_ where book0_.author_id=1;
underTest.findAll(byAuthorId(homer.getId()));
// select book0_.id as id1_1_, book0_.author_id as author_i3_1_, book0_.name as name2_1_ from book book0_ where book0_.author_id=1;
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Book> cq = cb.createQuery(Book.class);
Root<Book> root = cq.from(Book.class);
cq.select(root).where(cb.equal(root.get("author").get("id"), homer.getId()));
entityManager.createQuery(cq).getResultList();
}
private static Specification<Book> byAuthorId(Long authorId) {
return (root, query, builder) -> builder.equal(root.get("author").get("id"), authorId);
}
}