Skip to content

Commit 1635858

Browse files
fanatixanpivovarit
authored andcommitted
Use Criteria Queries in a Spring Data Application (eugenp#5014)
* Use Criteria Queries in a Spring Data Application * formatting fix * making the workflow more readable
1 parent 1c455d1 commit 1635858

File tree

6 files changed

+143
-0
lines changed

6 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.baeldung.persistence.criteria.dao;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.TypedQuery;
8+
import javax.persistence.criteria.CriteriaBuilder;
9+
import javax.persistence.criteria.CriteriaQuery;
10+
import javax.persistence.criteria.Predicate;
11+
import javax.persistence.criteria.Root;
12+
13+
import org.baeldung.persistence.criteria.model.Book;
14+
import org.baeldung.persistence.criteria.repository.BookRepositoryCustom;
15+
import org.springframework.stereotype.Repository;
16+
17+
@Repository
18+
public class BookRepositoryImpl implements BookRepositoryCustom {
19+
20+
private EntityManager em;
21+
22+
public BookRepositoryImpl(EntityManager em) {
23+
this.em = em;
24+
}
25+
26+
@Override
27+
public List<Book> findBooksByAuthorNameAndTitle(String authorName, String title) {
28+
CriteriaBuilder cb = em.getCriteriaBuilder();
29+
CriteriaQuery<Book> cq = cb.createQuery(Book.class);
30+
31+
Root<Book> book = cq.from(Book.class);
32+
List<Predicate> predicates = new ArrayList<>();
33+
34+
if (authorName != null) {
35+
predicates.add(cb.equal(book.get("author"), authorName));
36+
}
37+
if (title != null) {
38+
predicates.add(cb.like(book.get("title"), "%" + title + "%"));
39+
}
40+
cq.where(predicates.toArray(new Predicate[0]));
41+
42+
TypedQuery<Book> query = em.createQuery(cq);
43+
return query.getResultList();
44+
}
45+
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.baeldung.persistence.criteria.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class Book {
8+
9+
@Id
10+
private Long id;
11+
12+
private String title;
13+
14+
private String author;
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public String getTitle() {
21+
return title;
22+
}
23+
24+
public void setTitle(String title) {
25+
this.title = title;
26+
}
27+
28+
public String getAuthor() {
29+
return author;
30+
}
31+
32+
public void setAuthor(String author) {
33+
this.author = author;
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.persistence.criteria.repository;
2+
3+
import org.baeldung.persistence.criteria.model.Book;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
6+
7+
public interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom, JpaSpecificationExecutor<Book> {
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung.persistence.criteria.repository;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.persistence.criteria.model.Book;
6+
7+
public interface BookRepositoryCustom {
8+
9+
List<Book> findBooksByAuthorNameAndTitle(String authorName, String title);
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.baeldung.persistence.criteria.repository;
2+
3+
import static org.baeldung.persistence.criteria.repository.BookSpecifications.hasAuthor;
4+
import static org.baeldung.persistence.criteria.repository.BookSpecifications.titleContains;
5+
import static org.springframework.data.jpa.domain.Specifications.where;
6+
7+
import java.util.List;
8+
9+
import org.baeldung.persistence.criteria.model.Book;
10+
import org.springframework.stereotype.Service;
11+
12+
@Service
13+
public class BookService {
14+
15+
private BookRepository bookRepository;
16+
17+
public BookService(BookRepository bookRepository) {
18+
this.bookRepository = bookRepository;
19+
}
20+
21+
public List<Book> query(String author, String title) {
22+
return bookRepository.findAll(where(hasAuthor(author)).and(titleContains(title)));
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.persistence.criteria.repository;
2+
3+
import org.baeldung.persistence.criteria.model.Book;
4+
import org.springframework.data.jpa.domain.Specification;
5+
6+
public class BookSpecifications {
7+
8+
public static Specification<Book> hasAuthor(String author) {
9+
return (book, cq, cb) -> cb.equal(book.get("author"), author);
10+
}
11+
12+
public static Specification<Book> titleContains(String title) {
13+
return (book, cq, cb) -> cb.like(book.get("title"), "%" + title + "%");
14+
}
15+
16+
}

0 commit comments

Comments
 (0)