@@ -498,8 +498,8 @@ Let's hit the code with our favorite thing, the Extract Method refactoring. We o
498
498
499
499
[source,java]
500
500
----
501
- static List<Book> findBooksByTitleWithPagination (Session session,
502
- String titlePattern, Page page) {
501
+ static List<Book> findBooksTitled (Session session,
502
+ String titlePattern, Page page) {
503
503
return session.createSelectionQuery("from Book where title like ?1 order by title", Book.class)
504
504
.setParameter(1, titlePattern)
505
505
.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
522
522
query = "from Book where title like :title order by title")
523
523
class Queries {
524
524
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
528
528
.setParameter("title", titlePattern)
529
529
.setPage(page)
530
530
.getResultList();
@@ -549,7 +549,7 @@ Whatever the case, the code which orchestrates a unit of work usually just calls
549
549
@Path("books/{titlePattern}")
550
550
public List<Book> findBooks(String titlePattern) {
551
551
var books = sessionFactory.fromTransaction(session ->
552
- Queries.findBooksByTitleWithPagination (session, titlePattern,
552
+ Queries.findBooksTitled (session, titlePattern,
553
553
Page.page(RESULTS_PER_PAGE, page)));
554
554
return books.isEmpty() ? Response.status(404).build() : books;
555
555
}
@@ -569,7 +569,7 @@ Suppose we simplify `Queries` to just the following:
569
569
----
570
570
interface Queries {
571
571
@HQL("where title like :title order by title")
572
- List<Book> findBooksByTitleWithPagination (String title, Page page);
572
+ List<Book> findBooksTitled (String title, Page page);
573
573
}
574
574
----
575
575
@@ -582,7 +582,7 @@ We can call it just like we were previously calling our handwritten version:
582
582
@Path("books/{titlePattern}")
583
583
public List<Book> findBooks(String titlePattern) {
584
584
var books = sessionFactory.fromTransaction(session ->
585
- Queries_.findBooksByTitleWithPagination (session, titlePattern,
585
+ Queries_.findBooksTitled (session, titlePattern,
586
586
Page.page(RESULTS_PER_PAGE, page)));
587
587
return books.isEmpty() ? Response.status(404).build() : books;
588
588
}
@@ -602,7 +602,7 @@ interface Queries {
602
602
EntityManager entityManager();
603
603
604
604
@HQL("where title like :title order by title")
605
- List<Book> findBooksByTitleWithPagination (String title, Page page);
605
+ List<Book> findBooksTitled (String title, Page page);
606
606
}
607
607
----
608
608
@@ -616,7 +616,7 @@ The `Queries` interface is now considered a _repository_, and we may use CDI to
616
616
@Path("books/{titlePattern}")
617
617
@Transactional
618
618
public List<Book> findBooks(String titlePattern) {
619
- var books = queries.findBooksByTitleWithPagination (session, titlePattern,
619
+ var books = queries.findBooksTitled (session, titlePattern,
620
620
Page.page(RESULTS_PER_PAGE, page));
621
621
return books.isEmpty() ? Response.status(404).build() : books;
622
622
}
0 commit comments