@@ -498,8 +498,8 @@ Let's hit the code with our favorite thing, the Extract Method refactoring. We o
498498
499499[source,java]
500500----
501- static List<Book> findBooksByTitleWithPagination (Session session,
502- String titlePattern, Page page) {
501+ static List<Book> findBooksTitled (Session session,
502+ String titlePattern, Page page) {
503503 return session.createSelectionQuery("from Book where title like ?1 order by title", Book.class)
504504 .setParameter(1, titlePattern)
505505 .setPage(page)
@@ -522,9 +522,9 @@ We need a place to put the annotation, so let's move our query method to a new c
522522 query = "from Book where title like :title order by title")
523523class Queries {
524524
525- static List<Book> findBooksByTitleWithPagination (Session session,
526- String titlePattern, Page page) {
527- return session.createNamedQuery (Queries_._findBooksByTitle_) //type safe reference to the named query
525+ static List<Book> findBooksTitled (Session session,
526+ String titlePattern, Page page) {
527+ return session.createQuery (Queries_._findBooksByTitle_) //type safe reference to the named query
528528 .setParameter("title", titlePattern)
529529 .setPage(page)
530530 .getResultList();
@@ -549,7 +549,7 @@ Whatever the case, the code which orchestrates a unit of work usually just calls
549549@Path("books/{titlePattern}")
550550public List<Book> findBooks(String titlePattern) {
551551 var books = sessionFactory.fromTransaction(session ->
552- Queries.findBooksByTitleWithPagination (session, titlePattern,
552+ Queries.findBooksTitled (session, titlePattern,
553553 Page.page(RESULTS_PER_PAGE, page)));
554554 return books.isEmpty() ? Response.status(404).build() : books;
555555}
@@ -569,7 +569,7 @@ Suppose we simplify `Queries` to just the following:
569569----
570570interface Queries {
571571 @HQL("where title like :title order by title")
572- List<Book> findBooksByTitleWithPagination (String title, Page page);
572+ List<Book> findBooksTitled (String title, Page page);
573573}
574574----
575575
@@ -582,7 +582,7 @@ We can call it just like we were previously calling our handwritten version:
582582@Path("books/{titlePattern}")
583583public List<Book> findBooks(String titlePattern) {
584584 var books = sessionFactory.fromTransaction(session ->
585- Queries_.findBooksByTitleWithPagination (session, titlePattern,
585+ Queries_.findBooksTitled (session, titlePattern,
586586 Page.page(RESULTS_PER_PAGE, page)));
587587 return books.isEmpty() ? Response.status(404).build() : books;
588588}
@@ -602,7 +602,7 @@ interface Queries {
602602 EntityManager entityManager();
603603
604604 @HQL("where title like :title order by title")
605- List<Book> findBooksByTitleWithPagination (String title, Page page);
605+ List<Book> findBooksTitled (String title, Page page);
606606}
607607----
608608
@@ -616,7 +616,7 @@ The `Queries` interface is now considered a _repository_, and we may use CDI to
616616@Path("books/{titlePattern}")
617617@Transactional
618618public List<Book> findBooks(String titlePattern) {
619- var books = queries.findBooksByTitleWithPagination (session, titlePattern,
619+ var books = queries.findBooksTitled (session, titlePattern,
620620 Page.page(RESULTS_PER_PAGE, page));
621621 return books.isEmpty() ? Response.status(404).build() : books;
622622}
0 commit comments