{"id":443787,"date":"2025-01-02T15:01:06","date_gmt":"2025-01-02T15:01:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=443787"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2025-01-02T15:01:06","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=443787","title":{"rendered":"<span>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Spring Data JPA Specifications<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>Spring Data JPA Specifications<\/strong>\u00a0\u2014 \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0431\u0430\u0437\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u0435, \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u0443\u044f \u0438\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442\u043e\u0432, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a\u00a0<code>AND<\/code>,\u00a0<code>OR<\/code>\u00a0\u0438 \u0442.\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Java-\u043a\u043e\u0434. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b Specifications, \u0438\u0445 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<h4>\u0417\u0430\u0447\u0435\u043c \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c Specifications?<\/h4>\n<p>\u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0447\u0430\u0441\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0443 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0435\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0432\u0432\u043e\u0434\u0430. \u0422\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 SQL-\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00a0<code>@Query<\/code>\u00a0\u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438) \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u044b\u043c\u0438 \u0438\u0437-\u0437\u0430 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p><strong>Specification<\/strong> \u2014 \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0439 Spring Data JPA, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0441 <code>JpaSpecificationExecutor<\/code> \u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>toPredicate<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code>Predicate<\/code>. \u042d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f Hibernate \u0432 SQL-\u0437\u0430\u043f\u0440\u043e\u0441.  <\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 <code>Specification<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"java\">@FunctionalInterface public interface Specification&lt;T&gt; {     Predicate toPredicate(Root&lt;T&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder criteriaBuilder); }<\/code><\/pre>\n<p><strong>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b:<\/strong>  <\/p>\n<ul>\n<li>\n<p><code>Root&lt;T&gt;<\/code> \u2014 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438.<\/p>\n<\/li>\n<li>\n<p><code>CriteriaQuery&lt;?&gt;<\/code> \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b\u0431\u043e\u0440\u043a\u0430, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430).<\/p>\n<\/li>\n<li>\n<p><code>CriteriaBuilder<\/code> \u2014 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u0438\u0439, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0442. \u0434.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0434\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 Specifications.<\/p>\n<pre><code class=\"java\">@Entity @Data public class Author {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      private String name;      private String biography;      @OneToMany(mappedBy = \"author\")     private List&lt;Book&gt; books = new ArrayList&lt;&gt;(); }   @Entity @Data public class Book {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      private String title;      private Double price;      @ManyToOne     private Author author;      @ManyToOne     private Genre genre;      @ManyToMany     @JoinTable(name = \"book_readers\",             joinColumns = @JoinColumn(name = \"book_id\"),             inverseJoinColumns = @JoinColumn(name = \"reader_id\"))     private List&lt;Reader&gt; readers = new ArrayList&lt;&gt;(); }   @Entity @Data public class Genre {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      private String name;      @OneToMany(mappedBy = \"genre\")     private List&lt;Book&gt; books = new ArrayList&lt;&gt;(); }   @Entity @Data public class Reader {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      private String name;      @ManyToMany(mappedBy = \"readers\")     private List&lt;Book&gt; borrowedBooks = new ArrayList&lt;&gt;(); }<\/code><\/pre>\n<pre><code class=\"java\">@Repository public interface AuthorRepository extends JpaRepository&lt;Author, Long&gt; { }  @Repository public interface BookRepository extends JpaRepository&lt;Book, Long&gt;, JpaSpecificationExecutor&lt;Book&gt; { }  @Repository public interface GenreRepository extends JpaRepository&lt;Genre, Long&gt; { }  @Repository public interface ReaderRepository extends JpaRepository&lt;Reader, Long&gt; { }<\/code><\/pre>\n<pre><code class=\"java\">@Data @Builder public class AuthorModel {     private Long id;     private String name;     private String biography; }  @Data @Builder public class BookModel {     private Long id;     private String title;     private Double price;     private AuthorModel author;     private GenreModel genre;     private List&lt;ReaderModel&gt; readers; }   @Data @Builder public class GenreModel {     private Long id;     private String name; }  @Data @AllArgsConstructor @NoArgsConstructor public class ReaderModel {     private Long id;     private String name;      public static ReaderModel mapToReaderModel(Reader reader) {         return new ReaderModel(reader.getId(), reader.getName());     } }  public class BookMapper {      public static BookModel mapToDto(Book book) {         return BookModel.builder()                 .id(book.getId())                 .title(book.getTitle())                 .price(book.getPrice())                 .author(mapToAuthorModel(book.getAuthor()))                 .genre(mapToGenreModel(book.getGenre()))                 .readers(mapToReaderModels(book.getReaders()))                 .build();     }      private static AuthorModel mapToAuthorModel(Author author) {         return AuthorModel.builder()                 .id(author.getId())                 .name(author.getName())                 .biography(author.getBiography())                 .build();     }      private static GenreModel mapToGenreModel(Genre genre) {         return GenreModel.builder()                 .id(genre.getId())                 .name(genre.getName())                 .build();     }      private static List&lt;ReaderModel&gt; mapToReaderModels(List&lt;Reader&gt; readers) {         return readers.stream()                 .map(ReaderModel::mapToReaderModel)                 .toList();     } }<\/code><\/pre>\n<pre><code class=\"java\">@Component public class DataInitializer implements CommandLineRunner {      private final BookRepository bookRepository;     private final AuthorRepository authorRepository;     private final GenreRepository genreRepository;     private final ReaderRepository readerRepository;      public DataInitializer(BookRepository bookRepository,                            AuthorRepository authorRepository,                            GenreRepository genreRepository,                            ReaderRepository readerRepository) {         this.bookRepository = bookRepository;         this.authorRepository = authorRepository;         this.genreRepository = genreRepository;         this.readerRepository = readerRepository;     }      @Override     public void run(String... args) {         Genre fantasy = new Genre();         fantasy.setName(\"Fantasy\");         genreRepository.save(fantasy);          Genre mystery = new Genre();         mystery.setName(\"Mystery\");         genreRepository.save(mystery);          Author tolkien = new Author();         tolkien.setName(\"J.R.R. Tolkien\");         tolkien.setBiography(\"Author of The Lord of the Rings.\");         authorRepository.save(tolkien);          Reader reader1 = new Reader();         reader1.setName(\"Alice\");         readerRepository.save(reader1);          Reader reader2 = new Reader();         reader2.setName(\"Bob\");         readerRepository.save(reader2);          Reader reader3 = new Reader();         reader3.setName(\"Charlie\");         readerRepository.save(reader3);          Book book = new Book();         book.setTitle(\"The Hobbit\");         book.setAuthor(tolkien);         book.setGenre(fantasy);         book.setPrice(8.0);         book.getReaders().add(reader1);         book.getReaders().add(reader2);         bookRepository.save(book);          Book book1 = new Book();         book1.setTitle(\"The Lord of rings\");         book1.setAuthor(tolkien);         book1.setGenre(fantasy);         book1.setPrice(2.0);         book1.getReaders().add(reader1);         book1.getReaders().add(reader3);         bookRepository.save(book1);     } }<\/code><\/pre>\n<pre><code class=\"yaml\">spring:   datasource:     url: jdbc:h2:mem:testdb     driver-class-name: org.h2.Driver     username: sa     password:   h2:     console:       enabled: true   jpa:     hibernate:       ddl-auto: create     show-sql: true<\/code><\/pre>\n<p>\u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f Specifications, \u0441\u043e\u0437\u0434\u0430\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0430\u0432\u0442\u043e\u0440\u0430, \u0436\u0430\u043d\u0440\u0443, \u0446\u0435\u043d\u0435 \u0438 \u0442.\u0434.<\/p>\n<pre><code class=\"java\">public class BookSpecification {      public static Specification&lt;Book&gt; hasTitle(String title) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.equal(root.get(\"title\"), title);     }      public static Specification&lt;Book&gt; hasAuthor(String authorName) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.equal(root.join(\"author\").get(\"name\"), authorName);     }      public static Specification&lt;Book&gt; hasGenre(String genreName) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.equal(root.join(\"genre\").get(\"name\"), genreName);     }      public static Specification&lt;Book&gt; isBorrowedBy(String readerName) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.equal(root.join(\"readers\").get(\"name\"), readerName);     }      public static Specification&lt;Book&gt; priceBetween(String minPrice, String maxPrice) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.between(root.get(\"price\"), minPrice, maxPrice);     } }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u043c Specifications \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435.<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/books\") public class BookController {      private final BookRepository bookRepository;      public BookController(BookRepository bookRepository) {         this.bookRepository = bookRepository;     }      @GetMapping(\"\/search\")     public List&lt;BookModel&gt; searchBooks(             @RequestParam(required = false) String title,             @RequestParam(required = false) String author,             @RequestParam(required = false) String genre,             @RequestParam(required = false) String reader,             @RequestParam(required = false) String priceFrom,             @RequestParam(required = false) String priceTo     ) {         Specification&lt;Book&gt; spec = Specification.where(null);          if (title != null) spec = spec.and(BookSpecification.hasTitle(title));         if (author != null) spec = spec.and(BookSpecification.hasAuthor(author));         if (genre != null) spec = spec.and(BookSpecification.hasGenre(genre));         if (reader != null) spec = spec.and(BookSpecification.isBorrowedBy(reader));         if (priceFrom != null &amp;&amp; priceTo != null) spec = spec.and(BookSpecification.priceBetween(priceFrom, priceTo));         var books = bookRepository.findAll(spec);         return books.stream()                 .map(BookMapper::mapToDto)                 .toList();     } }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0443\u0441\u0442\u0443\u044e Specification, \u0438 \u043f\u043e \u043c\u0435\u0440\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 \u043c\u0435\u0442\u043e\u0434 searchBooks \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0441\u0442\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u043c\u0438 \u0438 \u043d\u043e\u0432\u044b\u043c\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c\u0438.<\/p>\n<p>\u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <a href=\"http:\/\/localhost:8080\/books\/search?reader=Alice\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8080\/books\/search?reader=Alice<\/a> \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u0441\u0435 \u043a\u043d\u0438\u0433\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0430 Alice<\/p>\n<pre><code class=\"json\">[ { \"id\": 1, \"title\": \"The Hobbit\", \"price\": 8, \"author\": { \"id\": 1, \"name\": \"J.R.R. Tolkien\", \"biography\": \"Author of The Lord of the Rings.\" }, \"genre\": { \"id\": 1, \"name\": \"Fantasy\" }, \"readers\": [ { \"id\": 1, \"name\": \"Alice\" }, { \"id\": 2, \"name\": \"Bob\" } ] }, { \"id\": 2, \"title\": \"The Lord of rings\", \"price\": 2, \"author\": { \"id\": 1, \"name\": \"J.R.R. Tolkien\", \"biography\": \"Author of The Lord of the Rings.\" }, \"genre\": { \"id\": 1, \"name\": \"Fantasy\" }, \"readers\": [ { \"id\": 1, \"name\": \"Alice\" }, { \"id\": 3, \"name\": \"Charlie\" } ] } ]<\/code><\/pre>\n<p>\u0410 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <a href=\"http:\/\/localhost:8080\/books\/search?reader=Bob&amp;genre=Fantasy\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8080\/books\/search?reader=Bob&amp;genre=Fantasy<\/a>, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043a\u043d\u0438\u0433\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b Bob \u0432 \u0436\u0430\u043d\u0440\u0435 Fantasy:<\/p>\n<pre><code class=\"json\">[ { \"id\": 1, \"title\": \"The Hobbit\", \"price\": 8, \"author\": { \"id\": 1, \"name\": \"J.R.R. Tolkien\", \"biography\": \"Author of The Lord of the Rings.\" }, \"genre\": { \"id\": 1, \"name\": \"Fantasy\" }, \"readers\": [ { \"id\": 1, \"name\": \"Alice\" }, { \"id\": 2, \"name\": \"Bob\" } ] } ]<\/code><\/pre>\n<p>\u0410 \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u0435, \u0447\u0442\u043e \u043a\u043d\u0438\u0433\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0441\u0442\u043e\u0438\u0442\u044c \u043e\u0442 9 \u0434\u043e 100 \u0434\u043e\u043b\u043b\u0430\u0440\u043e\u0432, <a href=\"http:\/\/localhost:8080\/books\/search?reader=Bob&amp;genre=Fantasy&amp;priceFrom=9&amp;priceTo=100\" rel=\"noopener noreferrer nofollow\">http:\/\/localhost:8080\/books\/search?reader=Bob&amp;genre=Fantasy&amp;priceFrom=9&amp;priceTo=100<\/a>, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0443\u0441\u0442\u043e\u0439 Json, \u0442\u0430\u043a \u043a\u0430\u043a \u043a\u043d\u0438\u0433 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043a \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430\u043c Specifications \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043d\u0435\u0441\u0442\u0438:<\/p>\n<h4>1. \u041c\u043e\u0434\u0443\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/h4>\n<p>\u041a\u0430\u0436\u0434\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 <code>Specification<\/code>, \u0447\u0442\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<h4>2. \u0414\u0438\u043d\u0430\u043c\u0438\u0447\u043d\u043e\u0441\u0442\u044c<\/h4>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0438\u0437 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0443\u0441\u043b\u043e\u0432\u0438\u0439, \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.<\/p>\n<h4>3. \u0427\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c<\/h4>\n<p>\u041a\u043e\u0434 \u0441 Specifications \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u0438 \u043f\u0440\u043e\u0449\u0435 \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0447\u0435\u043c \u0441\u043b\u043e\u0436\u043d\u044b\u0439 JPQL \u0438\u043b\u0438 SQL.<\/p>\n<h4>4. \u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f<\/h4>\n<p>Specifications \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b \u0441 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 Spring Data JPA.<\/p>\n<p>\u0410 \u043a \u043c\u0438\u043d\u0443\u0441\u0430\u043c:<\/p>\n<h4>1. \u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432<\/h4>\n<p>\u041f\u043e\u0440\u043e\u0433 \u0432\u0445\u043e\u0434\u0430 \u0434\u043b\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u0441\u043e\u043a\u0438\u043c \u0438\u0437-\u0437\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u043e\u0432 \u0440\u0430\u0431\u043e\u0442\u044b Criteria API.<\/p>\n<h4>2. \u0423\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/h4>\n<p>\u041a\u0430\u0436\u0434\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u043c, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0431\u043e\u043b\u044c\u0448\u043e\u043c\u0443 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043c\u0435\u043b\u043a\u0438\u0445 \u0444\u0430\u0439\u043b\u043e\u0432.<\/p>\n<h4>3. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445<\/h4>\n<p>\u0414\u043b\u044f \u043e\u0447\u0435\u043d\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0443\u0441\u0438\u043b\u0438\u0439, \u0447\u0435\u043c \u043f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043d\u0430\u0442\u0438\u0432\u043d\u043e\u0433\u043e SQL \u0438\u043b\u0438 JPQL.<\/p>\n<h3> \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>Specifications \u2014 \u044d\u0442\u043e \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0433\u0438\u0431\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 Spring Data JPA. \u041e\u043d\u0438 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u044e\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0435\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0437\u0430\u043c\u0435\u043d\u044f\u044f \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0438 \u0443\u043b\u0443\u0447\u0448\u0430\u044f \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u0441\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f \u043b\u0443\u0447\u0448\u0438\u0445 \u043f\u0440\u0430\u043a\u0442\u0438\u043a, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0440\u043e\u0441\u0442\u0430 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u0448 \u043f\u0440\u043e\u0435\u043a\u0442 \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, Specifications \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u0445 \u0433\u0438\u0431\u043a\u0438\u043c\u0438, \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c\u0438 \u0438 \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u043c\u0438.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/870698\/\"> https:\/\/habr.com\/ru\/articles\/870698\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><strong>Spring Data JPA Specifications<\/strong>\u00a0\u2014 \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0431\u0430\u0437\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u0435 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u0435, \u043a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u0443\u044f \u0438\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442\u043e\u0432, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a\u00a0<code>AND<\/code>,\u00a0<code>OR<\/code>\u00a0\u0438 \u0442.\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Java-\u043a\u043e\u0434. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u044b Specifications, \u0438\u0445 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<h4>\u0417\u0430\u0447\u0435\u043c \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c Specifications?<\/h4>\n<p>\u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0447\u0430\u0441\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0443 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0435\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0432\u0432\u043e\u0434\u0430. \u0422\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 SQL-\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00a0<code>@Query<\/code>\u00a0\u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438) \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u044b\u043c\u0438 \u0438\u0437-\u0437\u0430 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p><strong>Specification<\/strong> \u2014 \u044d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0439 Spring Data JPA, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0441 <code>JpaSpecificationExecutor<\/code> \u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>toPredicate<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code>Predicate<\/code>. \u042d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f Hibernate \u0432 SQL-\u0437\u0430\u043f\u0440\u043e\u0441.  <\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 <code>Specification<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"java\">@FunctionalInterface public interface Specification&lt;T&gt; {     Predicate toPredicate(Root&lt;T&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder criteriaBuilder); }<\/code><\/pre>\n<p><strong>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b:<\/strong>  <\/p>\n<ul>\n<li>\n<p><code>Root&lt;T&gt;<\/code> \u2014 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438.<\/p>\n<\/li>\n<li>\n<p><code>CriteriaQuery&lt;?&gt;<\/code> \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b\u0431\u043e\u0440\u043a\u0430, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430).<\/p>\n<\/li>\n<li>\n<p><code>CriteriaBuilder<\/code> \u2014 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u0438\u0439, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0442. \u0434.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0434\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 Specifications.<\/p>\n<pre><code class=\"java\">@Entity @Data public class Author {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      private String name;      private String biography;      @OneToMany(mappedBy = \"author\")     private List&lt;Book&gt; books = new ArrayList&lt;&gt;(); }   @Entity @Data public class Book {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      private String title;      private Double price;      @ManyToOne     private Author author;      @ManyToOne     private Genre genre;      @ManyToMany     @JoinTable(name = \"book_readers\",             joinColumns = @JoinColumn(name = \"book_id\"),             inverseJoinColumns = @JoinColumn(name = \"reader_id\"))     private List&lt;Reader&gt; readers = new ArrayList&lt;&gt;(); }   @Entity @Data public class Genre {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      private String name;      @OneToMany(mappedBy = \"genre\")     private List&lt;Book&gt; books = new ArrayList&lt;&gt;(); }   @Entity @Data public class Reader {     @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      private String name;      @ManyToMany(mappedBy = \"readers\")     private List&lt;Book&gt; borrowedBooks = new ArrayList&lt;&gt;(); }<\/code><\/pre>\n<pre><code class=\"java\">@Repository public interface AuthorRepository extends JpaRepository&lt;Author, Long&gt; { }  @Repository public interface BookRepository extends JpaRepository&lt;Book, Long&gt;, JpaSpecificationExecutor&lt;Book&gt; { }  @Repository public interface GenreRepository extends JpaRepository&lt;Genre, Long&gt; { }  @Repository public interface ReaderRepository extends JpaRepository&lt;Reader, Long&gt; { }<\/code><\/pre>\n<pre><code class=\"java\">@Data @Builder public class AuthorModel {     private Long id;     private String name;     private String biography; }  @Data @Builder public class BookModel {     private Long id;     private String title;     private Double price;     private AuthorModel author;     private GenreModel genre;     private List&lt;ReaderModel&gt; readers; }   @Data @Builder public class GenreModel {     private Long id;     private String name; }  @Data @AllArgsConstructor @NoArgsConstructor public class ReaderModel {     private Long id;     private String name;      public static ReaderModel mapToReaderModel(Reader reader) {         return new ReaderModel(reader.getId(), reader.getName());     } }  public class BookMapper {      public static BookModel mapToDto(Book book) {         return BookModel.builder()                 .id(book.getId())                 .title(book.getTitle())                 .price(book.getPrice())                 .author(mapToAuthorModel(book.getAuthor()))                 .genre(mapToGenreModel(book.getGenre()))                 .readers(mapToReaderModels(book.getReaders()))                 .build();     }      private static AuthorModel mapToAuthorModel(Author author) {         return AuthorModel.builder()                 .id(author.getId())                 .name(author.getName())                 .biography(author.getBiography())                 .build();     }      private static GenreModel mapToGenreModel(Genre genre) {         return GenreModel.builder()                 .id(genre.getId())                 .name(genre.getName())                 .build();     }      private static List&lt;ReaderModel&gt; mapToReaderModels(List&lt;Reader&gt; readers) {         return readers.stream()                 .map(ReaderModel::mapToReaderModel)                 .toList();     } }<\/code><\/pre>\n<pre><code class=\"java\">@Component public class DataInitializer implements CommandLineRunner {      private final BookRepository bookRepository;     private final AuthorRepository authorRepository;     private final GenreRepository genreRepository;     private final ReaderRepository readerRepository;      public DataInitializer(BookRepository bookRepository,                            AuthorRepository authorRepository,                            GenreRepository genreRepository,                            ReaderRepository readerRepository) {         this.bookRepository = bookRepository;         this.authorRepository = authorRepository;         this.genreRepository = genreRepository;         this.readerRepository = readerRepository;     }      @Override     public void run(String... args) {         Genre fantasy = new Genre();         fantasy.setName(\"Fantasy\");         genreRepository.save(fantasy);          Genre mystery = new Genre();         mystery.setName(\"Mystery\");         genreRepository.save(mystery);          Author tolkien = new Author();         tolkien.setName(\"J.R.R. Tolkien\");         tolkien.setBiography(\"Author of The Lord of the Rings.\");         authorRepository.save(tolkien);          Reader reader1 = new Reader();         reader1.setName(\"Alice\");         readerRepository.save(reader1);          Reader reader2 = new Reader();         reader2.setName(\"Bob\");         readerRepository.save(reader2);          Reader reader3 = new Reader();         reader3.setName(\"Charlie\");         readerRepository.save(reader3);          Book book = new Book();         book.setTitle(\"The Hobbit\");         book.setAuthor(tolkien);         book.setGenre(fantasy);         book.setPrice(8.0);         book.getReaders().add(reader1);         book.getReaders().add(reader2);         bookRepository.save(book);          Book book1 = new Book();         book1.setTitle(\"The Lord of rings\");         book1.setAuthor(tolkien);         book1.setGenre(fantasy);         book1.setPrice(2.0);         book1.getReaders().add(reader1);         book1.getReaders().add(reader3);         bookRepository.save(book1);     } }<\/code><\/pre>\n<pre><code class=\"yaml\">spring:   datasource:     url: jdbc:h2:mem:testdb     driver-class-name: org.h2.Driver     username: sa     password:   h2:     console:       enabled: true   jpa:     hibernate:       ddl-auto: create     show-sql: true<\/code><\/pre>\n<p>\u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f Specifications, \u0441\u043e\u0437\u0434\u0430\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0430\u0432\u0442\u043e\u0440\u0430, \u0436\u0430\u043d\u0440\u0443, \u0446\u0435\u043d\u0435 \u0438 \u0442.\u0434.<\/p>\n<pre><code class=\"java\">public class BookSpecification {      public static Specification&lt;Book&gt; hasTitle(String title) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.equal(root.get(\"title\"), title);     }      public static Specification&lt;Book&gt; hasAuthor(String authorName) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.equal(root.join(\"author\").get(\"name\"), authorName);     }      public static Specification&lt;Book&gt; hasGenre(String genreName) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.equal(root.join(\"genre\").get(\"name\"), genreName);     }      public static Specification&lt;Book&gt; isBorrowedBy(String readerName) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.equal(root.join(\"readers\").get(\"name\"), readerName);     }      public static Specification&lt;Book&gt; priceBetween(String minPrice, String maxPrice) {         return (root, query, criteriaBuilder) -&gt;                 criteriaBuilder.between(root.get(\"price\"), minPrice, maxPrice);     } }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u043c Specifications \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435.<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/books\") public class BookController {      private final BookRepository bookRepository;      public BookController(BookRepository bookRepository) {         this.bookRepository = bookRepository;     }      @GetMapping(\"\/search\")     public List&lt;BookModel&gt; searchBooks(             @RequestParam(required = false) String title,             @RequestParam(required = false) String author,             @RequestParam(required = false) String genre,             @RequestParam(required = false) String reader,             @RequestParam(required = false) String priceFrom,             @RequestParam(required = false) String priceTo     ) {         Specification&lt;Book&gt; spec = Specification.where(null);          if (title != null) spec = spec.and(BookSpecification.hasTitle(title));         if (author != null) spec = spec.and(BookSpecification.hasAuthor(author));         if (genre != null) spec = spec.and(BookSpecification.hasGenre(genre));         if (reader != null) spec = spec.and(BookSpecification.isBorrowedBy(reader));         if (priceFrom != null &amp;&amp; priceTo != null) spec = spec.and(BookSpecification.priceBetween(priceFrom, priceTo));         var books = bookRepository.findAll(spec);         return books.stream()                 .map(BookMapper::mapToDto)                 .toList();     } }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u043c\u044b<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-443787","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/443787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=443787"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/443787\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=443787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=443787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=443787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}