[JAVA-30352] Move code of article Use Criteria Queries to spring-data-jpa-query-2 (#16230)
This commit is contained in:
parent
8711ec8f29
commit
c15f637d3a
@ -0,0 +1,57 @@
|
|||||||
|
package com.baeldung.criteriaquery;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
private String title;
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
public Book(String title, String author) {
|
||||||
|
this.title = title;
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Specification<Book> hasAuthor(String author) {
|
||||||
|
return (book, cq, cb) -> cb.equal(book.get("author"), author);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Specification<Book> titleContains(String title) {
|
||||||
|
return (book, cq, cb) -> cb.like(book.get("title"), "%" + title + "%");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.criteriaquery;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom, JpaSpecificationExecutor<Book> {
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.criteriaquery;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
interface BookRepositoryCustom {
|
||||||
|
|
||||||
|
List<Book> findBooksByAuthorNameAndTitle(String authorName, String title);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.criteriaquery;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||||
|
import jakarta.persistence.criteria.CriteriaQuery;
|
||||||
|
import jakarta.persistence.criteria.Predicate;
|
||||||
|
import jakarta.persistence.criteria.Root;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class BookRepositoryImpl implements BookRepositoryCustom {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EntityManager 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]));
|
||||||
|
|
||||||
|
return em.createQuery(cq).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung.criteriaquery;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.baeldung.criteriaquery.Book.hasAuthor;
|
||||||
|
import static com.baeldung.criteriaquery.Book.titleContains;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.springframework.data.jpa.domain.Specification.where;
|
||||||
|
|
||||||
|
@DataJpaTest(showSql = false)
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
public class CriteriaQueryIntegrationTest {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
@Autowired
|
||||||
|
private BookRepository repository;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() {
|
||||||
|
entityManager.persist(new Book("title1", "author1"));
|
||||||
|
entityManager.persist(new Book("title2", "author2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAuthorAndTextInTitle_whenFindWithSpecification_ThenFound() {
|
||||||
|
List<Book> books = repository.findAll(where(hasAuthor("author1")).and(titleContains("1")));
|
||||||
|
assertEquals(1, books.size());
|
||||||
|
assertEquals("author1", books.get(0).getAuthor());
|
||||||
|
assertEquals("title1", books.get(0).getTitle());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAuthorAndTextInTitle_whenFindWithCriteriaQuery_ThenFound() {
|
||||||
|
List<Book> books = repository.findBooksByAuthorNameAndTitle("author2", "2");
|
||||||
|
assertEquals(1, books.size());
|
||||||
|
assertEquals("author2", books.get(0).getAuthor());
|
||||||
|
assertEquals("title2", books.get(0).getTitle());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user