JAVA-2305: Move Use Criteria Queries in a Spring Data Application to spring-data-jpa-query-2

This commit is contained in:
Krzysiek
2020-08-10 21:55:24 +02:00
parent 9c92f9a1e3
commit fd2f95f771
10 changed files with 62 additions and 14 deletions

View File

@@ -0,0 +1,16 @@
## Spring Data JPA - Query
This module contains articles about querying data using Spring Data JPA
### Relevant Articles:
- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries)
- More articles: [[<-- prev]](../spring-data-jpa-query)
### Eclipse Config
After importing the project into Eclipse, you may see the following error:
"No persistence xml file found in project"
This can be ignored:
- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
Or:
- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-jpa-query</artifactId>
<name>spring-data-jpa-query-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,9 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.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> {
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Book;
import java.util.List;
public interface BookRepositoryCustom {
List<Book> findBooksByAuthorNameAndTitle(String authorName, String title);
}

View File

@@ -0,0 +1,44 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Book;
import org.springframework.stereotype.Repository;
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 java.util.ArrayList;
import java.util.List;
@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();
}
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.model.Book;
import org.springframework.stereotype.Service;
import java.util.List;
import static com.baeldung.persistence.dao.BookSpecifications.hasAuthor;
import static com.baeldung.persistence.dao.BookSpecifications.titleContains;
import static org.springframework.data.jpa.domain.Specification.where;
@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)));
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.persistence.dao;
import com.baeldung.persistence.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 + "%");
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.persistence.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;
}
}