Use Criteria Queries in a Spring Data Application (#5014)
* Use Criteria Queries in a Spring Data Application * formatting fix * making the workflow more readable
This commit is contained in:
parent
1c455d1c1c
commit
163585858b
|
@ -0,0 +1,46 @@
|
|||
package org.baeldung.persistence.criteria.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
import org.baeldung.persistence.criteria.repository.BookRepositoryCustom;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class BookRepositoryImpl implements BookRepositoryCustom {
|
||||
|
||||
private EntityManager em;
|
||||
|
||||
public BookRepositoryImpl(EntityManager em) {
|
||||
this.em = em;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> findBooksByAuthorNameAndTitle(String authorName, String title) {
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Book> cq = cb.createQuery(Book.class);
|
||||
|
||||
Root<Book> book = cq.from(Book.class);
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
if (authorName != null) {
|
||||
predicates.add(cb.equal(book.get("author"), authorName));
|
||||
}
|
||||
if (title != null) {
|
||||
predicates.add(cb.like(book.get("title"), "%" + title + "%"));
|
||||
}
|
||||
cq.where(predicates.toArray(new Predicate[0]));
|
||||
|
||||
TypedQuery<Book> query = em.createQuery(cq);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package org.baeldung.persistence.criteria.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String author;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.baeldung.persistence.criteria.repository;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
public interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom, JpaSpecificationExecutor<Book> {
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package org.baeldung.persistence.criteria.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
|
||||
public interface BookRepositoryCustom {
|
||||
|
||||
List<Book> findBooksByAuthorNameAndTitle(String authorName, String title);
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package org.baeldung.persistence.criteria.repository;
|
||||
|
||||
import static org.baeldung.persistence.criteria.repository.BookSpecifications.hasAuthor;
|
||||
import static org.baeldung.persistence.criteria.repository.BookSpecifications.titleContains;
|
||||
import static org.springframework.data.jpa.domain.Specifications.where;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class BookService {
|
||||
|
||||
private BookRepository bookRepository;
|
||||
|
||||
public BookService(BookRepository bookRepository) {
|
||||
this.bookRepository = bookRepository;
|
||||
}
|
||||
|
||||
public List<Book> query(String author, String title) {
|
||||
return bookRepository.findAll(where(hasAuthor(author)).and(titleContains(title)));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.baeldung.persistence.criteria.repository;
|
||||
|
||||
import org.baeldung.persistence.criteria.model.Book;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
public class BookSpecifications {
|
||||
|
||||
public static Specification<Book> hasAuthor(String author) {
|
||||
return (book, cq, cb) -> cb.equal(book.get("author"), author);
|
||||
}
|
||||
|
||||
public static Specification<Book> titleContains(String title) {
|
||||
return (book, cq, cb) -> cb.like(book.get("title"), "%" + title + "%");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue