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
| public List<Book> getBooksByMultiCriteria(String title, String editor, String isbn13, Double price) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Book> query = cb.createQuery(Book.class);
Root<Book> select = query.from(Book.class);
if (title != null && !title.isEmpty()) {
query.where(cb.equal(select.<String> get("title"),
cb.parameter(String.class, "titleSearched")));
}
if (editor != null && !editor.isEmpty()) {
query.where(cb.equal(select.<String> get("editor"),
cb.parameter(String.class, "editorSearched")));
}
if (isbn13 != null && !isbn13.isEmpty()) {
query.where(cb.like(select.<String> get("isbn13"), // PLANTE
cb.parameter(String.class, "isbn13Searched")));
}
if (price != null && price != 0.0) {
query.where(cb.equal(select.<Double> get("price"), // PLANTE
cb.parameter(Double.class, "priceSearched")));
}
TypedQuery<Book> typedQuery = em.createQuery(query);
if (title != null && !title.isEmpty()) {
typedQuery.setParameter("titleSearched", title);
}
if (editor != null && !editor.isEmpty()) {
typedQuery.setParameter("editorSearched", editor);
}
if (isbn13 != null && !isbn13.isEmpty()) {
typedQuery.setParameter("isbn13Searched", "*" + isbn13 + "*");
}
if (price != null && price != 0.00) {
typedQuery.setParameter("priceSearched", price);
}
List<Book> BooksCriter = typedQuery.getResultList();
return BooksCriter; |
Partager