HHH-16815 tests for ascending()/descending() with parameters

This commit is contained in:
Gavin King 2023-06-26 15:23:15 +02:00
parent 5d05dd8478
commit d16808e015
1 changed files with 133 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class OrderTest { public class OrderTest {
@Test void testAscendingDescending(SessionFactoryScope scope) { @Test void testAscendingDescending(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery("delete Book").executeUpdate() );
scope.inTransaction( session -> { scope.inTransaction( session -> {
session.persist(new Book("9781932394153", "Hibernate in Action")); session.persist(new Book("9781932394153", "Hibernate in Action"));
session.persist(new Book("9781617290459", "Java Persistence with Hibernate")); 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<Book> bookType = scope.getSessionFactory().getJpaMetamodel().findEntityType(Book.class);
SingularAttribute<? super Book, ?> title = bookType.findSingularAttribute("title");
SingularAttribute<? super Book, ?> isbn = bookType.findSingularAttribute("isbn");
scope.inSession(session -> {
List<String> 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<String> 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<String> 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<String> 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<Book> bookType = scope.getSessionFactory().getJpaMetamodel().findEntityType(Book.class);
SingularAttribute<? super Book, ?> title = bookType.findSingularAttribute("title");
SingularAttribute<? super Book, ?> isbn = bookType.findSingularAttribute("isbn");
scope.inSession(session -> {
List<String> 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<String> 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<String> 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<String> 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") @Entity(name="Book")
static class Book { static class Book {
@Id String isbn; @Id String isbn;