From d16808e0156de9445d04341717c5d72690ce0333 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 26 Jun 2023 15:23:15 +0200 Subject: [PATCH] HHH-16815 tests for ascending()/descending() with parameters --- .../orm/test/query/order/OrderTest.java | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/order/OrderTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/order/OrderTest.java index 69e1e6522a..eccbfdb92e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/order/OrderTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/order/OrderTest.java @@ -19,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class OrderTest { @Test void testAscendingDescending(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery("delete Book").executeUpdate() ); scope.inTransaction( session -> { session.persist(new Book("9781932394153", "Hibernate in Action")); session.persist(new Book("9781617290459", "Java Persistence with Hibernate")); @@ -77,6 +78,138 @@ public class OrderTest { }); } + @Test void testAscendingDescendingWithPositionalParam(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery("delete Book").executeUpdate() ); + scope.inTransaction( session -> { + session.persist(new Book("9781932394153", "Hibernate in Action")); + session.persist(new Book("9781617290459", "Java Persistence with Hibernate")); + }); + EntityDomainType bookType = scope.getSessionFactory().getJpaMetamodel().findEntityType(Book.class); + SingularAttribute title = bookType.findSingularAttribute("title"); + SingularAttribute isbn = bookType.findSingularAttribute("isbn"); + scope.inSession(session -> { + List titlesAsc = session.createSelectionQuery("from Book where title like ?1", Book.class) + .setParameter(1, "%Hibernate%") + .ascending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", titlesAsc.get(0)); + assertEquals("Java Persistence with Hibernate", titlesAsc.get(1)); + + List titlesDesc = session.createSelectionQuery("from Book where title like ?1", Book.class) + .setParameter(1, "%Hibernate%") + .descending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", titlesDesc.get(1)); + assertEquals("Java Persistence with Hibernate", titlesDesc.get(0)); + + List isbnAsc = session.createSelectionQuery("from Book where title like ?1", Book.class) + .setParameter(1, "%Hibernate%") + .ascending(isbn).descending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", isbnAsc.get(1)); + assertEquals("Java Persistence with Hibernate", isbnAsc.get(0)); + + List isbnDesc = session.createSelectionQuery("from Book where title like ?1", Book.class) + .setParameter(1, "%Hibernate%") + .descending(isbn).descending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", isbnDesc.get(0)); + assertEquals("Java Persistence with Hibernate", isbnDesc.get(1)); + + titlesAsc = session.createSelectionQuery("from Book where title like ?1 order by isbn asc", Book.class) + .setParameter(1, "%Hibernate%") + .ascending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", titlesAsc.get(1)); + assertEquals("Java Persistence with Hibernate", titlesAsc.get(0)); + titlesAsc = session.createSelectionQuery("from Book where title like ?1 order by isbn asc", Book.class) + .setParameter(1, "%Hibernate%") + .unordered() + .ascending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", titlesAsc.get(0)); + assertEquals("Java Persistence with Hibernate", titlesAsc.get(1)); + }); + } + + @Test void testAscendingDescendingWithNamedParam(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery("delete Book").executeUpdate() ); + scope.inTransaction( session -> { + session.persist(new Book("9781932394153", "Hibernate in Action")); + session.persist(new Book("9781617290459", "Java Persistence with Hibernate")); + }); + EntityDomainType bookType = scope.getSessionFactory().getJpaMetamodel().findEntityType(Book.class); + SingularAttribute title = bookType.findSingularAttribute("title"); + SingularAttribute isbn = bookType.findSingularAttribute("isbn"); + scope.inSession(session -> { + List titlesAsc = session.createSelectionQuery("from Book where title like :title", Book.class) + .setParameter("title", "%Hibernate%") + .ascending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", titlesAsc.get(0)); + assertEquals("Java Persistence with Hibernate", titlesAsc.get(1)); + + List titlesDesc = session.createSelectionQuery("from Book where title like :title", Book.class) + .setParameter("title", "%Hibernate%") + .descending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", titlesDesc.get(1)); + assertEquals("Java Persistence with Hibernate", titlesDesc.get(0)); + + List isbnAsc = session.createSelectionQuery("from Book where title like :title", Book.class) + .setParameter("title", "%Hibernate%") + .ascending(isbn).descending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", isbnAsc.get(1)); + assertEquals("Java Persistence with Hibernate", isbnAsc.get(0)); + + List isbnDesc = session.createSelectionQuery("from Book where title like :title", Book.class) + .setParameter("title", "%Hibernate%") + .descending(isbn).descending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", isbnDesc.get(0)); + assertEquals("Java Persistence with Hibernate", isbnDesc.get(1)); + + titlesAsc = session.createSelectionQuery("from Book where title like :title order by isbn asc", Book.class) + .setParameter("title", "%Hibernate%") + .ascending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", titlesAsc.get(1)); + assertEquals("Java Persistence with Hibernate", titlesAsc.get(0)); + titlesAsc = session.createSelectionQuery("from Book where title like :title order by isbn asc", Book.class) + .setParameter("title", "%Hibernate%") + .unordered() + .ascending(title) + .getResultList() + .stream().map(book -> book.title) + .collect(toList()); + assertEquals("Hibernate in Action", titlesAsc.get(0)); + assertEquals("Java Persistence with Hibernate", titlesAsc.get(1)); + }); + } + @Entity(name="Book") static class Book { @Id String isbn;