|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive; |
| 7 | + |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import org.hibernate.metamodel.model.domain.EntityDomainType; |
| 13 | +import org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import io.vertx.junit5.Timeout; |
| 19 | +import io.vertx.junit5.VertxTestContext; |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.metamodel.SingularAttribute; |
| 23 | + |
| 24 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 25 | +import static java.util.stream.Collectors.toList; |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.hibernate.query.Order.asc; |
| 28 | +import static org.hibernate.query.Order.desc; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | + |
| 31 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 32 | +public class OrderTest extends BaseReactiveTest { |
| 33 | + final Book book1 = new Book("9781932394153", "Hibernate in Action"); |
| 34 | + final Book book2 = new Book("9781617290459", "Java Persistence with Hibernate"); |
| 35 | + |
| 36 | + @Override |
| 37 | + protected Collection<Class<?>> annotatedEntities() { |
| 38 | + return List.of( Book.class ); |
| 39 | + } |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + public void populateDB(VertxTestContext context) { |
| 43 | + test( context, getSessionFactory() |
| 44 | + .withTransaction( session -> session.persist( book1, book2 ) ) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testOrder(VertxTestContext context) { |
| 50 | + final SingularAttribute<? super Book, ?> isbn = getIsbnAttribute(); |
| 51 | + final SingularAttribute<? super Book, ?> title = getTitleAttribute(); |
| 52 | + |
| 53 | + test( |
| 54 | + context, |
| 55 | + getSessionFactory().withSession( session -> session |
| 56 | + .createSelectionQuery( "from Book", Book.class ) |
| 57 | + .setOrder( asc( title ) ).getResultList() |
| 58 | + .thenAccept( books -> assertThat( books ).contains( book1, book2 ) ) |
| 59 | + .thenCompose( v -> session |
| 60 | + .createSelectionQuery( "from Book", Book.class ) |
| 61 | + .setOrder( desc( title ) ).getResultList() |
| 62 | + ).thenAccept( books -> assertThat( books ).containsExactly( book2, book1 ) ) |
| 63 | + .thenCompose( v -> session |
| 64 | + .createSelectionQuery( "from Book", Book.class ) |
| 65 | + .setOrder( asc( isbn ) ) |
| 66 | + .getResultList() |
| 67 | + ).thenAccept( books -> assertThat( books ).containsExactly( book2, book1 ) ) |
| 68 | + .thenCompose( v -> session |
| 69 | + .createSelectionQuery( "from Book", Book.class ) |
| 70 | + .setOrder( desc( isbn ) ) |
| 71 | + .getResultList() |
| 72 | + ).thenAccept( books -> assertThat( books ).containsExactly( book1, book2 ) ) |
| 73 | + ) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testOrderMutiny(VertxTestContext context) { |
| 79 | + final SingularAttribute<? super Book, ?> isbn = getIsbnAttribute(); |
| 80 | + final SingularAttribute<? super Book, ?> title = getTitleAttribute(); |
| 81 | + |
| 82 | + test( |
| 83 | + context, |
| 84 | + getMutinySessionFactory().withSession( session -> session |
| 85 | + .createSelectionQuery( "select title from Book", String.class ) |
| 86 | + .getResultList() |
| 87 | + .invoke( list -> assertEquals( 2, list.size() ) ) |
| 88 | + .chain( v -> session |
| 89 | + .createSelectionQuery( "from Book", Book.class ) |
| 90 | + .setOrder( asc( title ) ) |
| 91 | + .getResultList() ).invoke( books -> assertThat( books ).contains( book1, book2 ) ) |
| 92 | + .chain( v -> session |
| 93 | + .createSelectionQuery( "from Book", Book.class ) |
| 94 | + .setOrder( desc( title ) ) |
| 95 | + .getResultList() ).invoke( books -> assertThat( books ).containsExactly( book2, book1 ) ) |
| 96 | + .chain( v -> session |
| 97 | + .createSelectionQuery( "from Book", Book.class ) |
| 98 | + .setOrder( asc( isbn ) ) |
| 99 | + .getResultList() ).invoke( books -> assertThat( books ).containsExactly( book2, book1 ) ) |
| 100 | + .chain( v -> session |
| 101 | + .createSelectionQuery( "from Book", Book.class ) |
| 102 | + .setOrder( desc( isbn ) ) |
| 103 | + .getResultList() ).invoke( books -> assertThat( books ).contains( book1, book2 ) ) |
| 104 | + ) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testAscDescBySelectElement(VertxTestContext context) { |
| 110 | + test( |
| 111 | + context, |
| 112 | + getSessionFactory().withSession( session -> session |
| 113 | + .createSelectionQuery( "select isbn, title from Book", Object[].class ) |
| 114 | + .setOrder( asc( 2 ) ).getResultList() |
| 115 | + .thenAccept( list -> assertOrderByBookArray( list, book1, book2 ) ) |
| 116 | + .thenCompose( v -> session |
| 117 | + .createSelectionQuery( "select isbn, title from Book", Object[].class ) |
| 118 | + .setOrder( desc( 2 ) ).getResultList() |
| 119 | + .thenAccept( list -> assertOrderByBookArray( list, book2, book1 ) ) |
| 120 | + ) |
| 121 | + ) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testAscDescBySelectElementMutiny(VertxTestContext context) { |
| 127 | + test( |
| 128 | + context, |
| 129 | + getMutinySessionFactory().withSession( session -> session |
| 130 | + .createSelectionQuery( "select isbn, title from Book", Object[].class ) |
| 131 | + .setOrder( asc( 2 ) ).getResultList() |
| 132 | + .invoke( list -> assertOrderByBookArray( list, book1, book2 ) ) |
| 133 | + .chain( v -> session |
| 134 | + .createSelectionQuery( "select isbn, title from Book", Object[].class ) |
| 135 | + .setOrder( desc( 2 ) ) |
| 136 | + .getResultList() ).invoke( list -> assertOrderByBookArray( list, book2, book1 ) ) |
| 137 | + ) |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testOrderWithList(VertxTestContext context) { |
| 143 | + final SingularAttribute<? super Book, ?> isbn = getIsbnAttribute(); |
| 144 | + final SingularAttribute<? super Book, ?> title = getTitleAttribute(); |
| 145 | + |
| 146 | + test( |
| 147 | + context, |
| 148 | + getSessionFactory().withSession( session -> session |
| 149 | + .createSelectionQuery( "from Book", Book.class ) |
| 150 | + .setOrder( List.of( asc( isbn ), desc( title ) ) ).getResultList() |
| 151 | + .thenAccept( isbnAsc -> assertThat( isbnAsc ).containsExactly( book2, book1 ) ) |
| 152 | + .thenCompose( v -> session |
| 153 | + .createSelectionQuery( "from Book", Book.class ) |
| 154 | + .setOrder( List.of( desc( isbn ), desc( title ) ) ).getResultList() |
| 155 | + .thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book1, book2 ) ) |
| 156 | + ) |
| 157 | + ) |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testOrderWithListMutiny(VertxTestContext context) { |
| 163 | + final SingularAttribute<? super Book, ?> isbn = getIsbnAttribute(); |
| 164 | + final SingularAttribute<? super Book, ?> title = getTitleAttribute(); |
| 165 | + |
| 166 | + test( |
| 167 | + context, |
| 168 | + getMutinySessionFactory().withSession( session -> session |
| 169 | + .createSelectionQuery( "from Book", Book.class ) |
| 170 | + .setOrder( List.of( asc( isbn ), desc( title ) ) ).getResultList() |
| 171 | + .invoke( isbnAsc -> assertThat( isbnAsc ).containsExactly( book2, book1 ) ) |
| 172 | + .chain( v -> session |
| 173 | + .createSelectionQuery( "from Book", Book.class ) |
| 174 | + .setOrder( List.of( desc( isbn ), desc( title ) ) ).getResultList() |
| 175 | + .invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book1, book2 ) ) |
| 176 | + ) |
| 177 | + ) |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testAscDescWithNamedParam(VertxTestContext context) { |
| 183 | + final SingularAttribute<? super Book, ?> title = getTitleAttribute(); |
| 184 | + |
| 185 | + test( |
| 186 | + context, |
| 187 | + getSessionFactory().withSession( session -> session |
| 188 | + .createSelectionQuery( "from Book where title like :title", Book.class ) |
| 189 | + .setParameter( "title", "%Hibernate%" ) |
| 190 | + .setOrder( asc( title ) ) |
| 191 | + .getResultList() |
| 192 | + .thenAccept( list -> assertOrderByBook( list, book1, book2 ) ) |
| 193 | + .thenCompose( v -> session |
| 194 | + .createSelectionQuery( "from Book where title like :title", Book.class ) |
| 195 | + .setParameter( "title", "%Hibernate%" ) |
| 196 | + .setOrder( desc( title ) ).getResultList() |
| 197 | + .thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) ) |
| 198 | + ) |
| 199 | + ) |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + public void testAscDescWithNamedParamMutiny(VertxTestContext context) { |
| 205 | + final SingularAttribute<? super Book, ?> title = getTitleAttribute(); |
| 206 | + |
| 207 | + test( |
| 208 | + context, |
| 209 | + getMutinySessionFactory().withSession( session -> session |
| 210 | + .createSelectionQuery( "from Book where title like :title", Book.class ) |
| 211 | + .setParameter( "title", "%Hibernate%" ) |
| 212 | + .setOrder( asc( title ) ) |
| 213 | + .getResultList() |
| 214 | + .invoke( list -> assertOrderByBook( list, book1, book2 ) ) |
| 215 | + .chain( v -> session |
| 216 | + .createSelectionQuery( "from Book where title like :title", Book.class ) |
| 217 | + .setParameter( "title", "%Hibernate%" ) |
| 218 | + .setOrder( desc( title ) ).getResultList() |
| 219 | + .invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) ) |
| 220 | + ) |
| 221 | + ) |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + public void testAscDescWithPositionalParam(VertxTestContext context) { |
| 227 | + final SingularAttribute<? super Book, ?> title = getTitleAttribute(); |
| 228 | + |
| 229 | + test( |
| 230 | + context, |
| 231 | + getSessionFactory().withSession( session -> session |
| 232 | + .createSelectionQuery( "from Book where title like :title", Book.class ) |
| 233 | + .setParameter( "title", "%Hibernate%" ) |
| 234 | + .setOrder( asc( title ) ) |
| 235 | + .getResultList() |
| 236 | + .thenAccept( list -> assertOrderByBook( list, book1, book2 ) ) |
| 237 | + .thenCompose( v -> session |
| 238 | + .createSelectionQuery( "from Book where title like :title", Book.class ) |
| 239 | + .setParameter( "title", "%Hibernate%" ) |
| 240 | + .setOrder( desc( title ) ).getResultList() |
| 241 | + .thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) ) |
| 242 | + ) |
| 243 | + ) |
| 244 | + ); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + public void testAscDescWithPositionalParamMutiny(VertxTestContext context) { |
| 249 | + final SingularAttribute<? super Book, ?> title = getTitleAttribute(); |
| 250 | + |
| 251 | + test( |
| 252 | + context, |
| 253 | + getMutinySessionFactory().withSession( session -> session |
| 254 | + .createSelectionQuery( "from Book where title like :title", Book.class ) |
| 255 | + .setParameter( "title", "%Hibernate%" ) |
| 256 | + .setOrder( asc( title ) ) |
| 257 | + .getResultList() |
| 258 | + .invoke( list -> assertOrderByBook( list, book1, book2 ) ) |
| 259 | + .chain( v -> session |
| 260 | + .createSelectionQuery( "from Book where title like :title", Book.class ) |
| 261 | + .setParameter( "title", "%Hibernate%" ) |
| 262 | + .setOrder( desc( title ) ).getResultList() |
| 263 | + .invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) ) |
| 264 | + ) |
| 265 | + ) |
| 266 | + ); |
| 267 | + } |
| 268 | + |
| 269 | + private void assertOrderByBookArray(List<Object[]> resultList, Book first, Book second ) { |
| 270 | + List<?> titles = resultList.stream().map( book -> (book[1]) ).collect( toList() ); |
| 271 | + assertEquals( first.title, titles.get( 0 ) ); |
| 272 | + assertEquals( second.title, titles.get( 1 ) ); |
| 273 | + } |
| 274 | + |
| 275 | + private void assertOrderByBook(List<Book> resultList, Book first, Book second ) { |
| 276 | + List<?> titles = resultList.stream().map( book -> book.title ).collect( toList() ); |
| 277 | + assertEquals( first.title, titles.get( 0 ) ); |
| 278 | + assertEquals( second.title, titles.get( 1 ) ); |
| 279 | + } |
| 280 | + |
| 281 | + private SingularAttribute<? super Book, ?> getIsbnAttribute() { |
| 282 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 283 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel().findEntityType( Book.class ); |
| 284 | + return bookType.findSingularAttribute( "isbn" ); |
| 285 | + } |
| 286 | + |
| 287 | + private SingularAttribute<? super Book, ?> getTitleAttribute() { |
| 288 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 289 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel().findEntityType( Book.class ); |
| 290 | + return bookType.findSingularAttribute( "title" ); |
| 291 | + } |
| 292 | + |
| 293 | + @Entity(name="Book") |
| 294 | + static class Book { |
| 295 | + @Id |
| 296 | + String isbn; |
| 297 | + String title; |
| 298 | + |
| 299 | + Book(String isbn, String title) { |
| 300 | + this.isbn = isbn; |
| 301 | + this.title = title; |
| 302 | + } |
| 303 | + |
| 304 | + Book() { |
| 305 | + } |
| 306 | + |
| 307 | + @Override |
| 308 | + public boolean equals(Object o) { |
| 309 | + if ( this == o ) { |
| 310 | + return true; |
| 311 | + } |
| 312 | + if ( o == null || getClass() != o.getClass() ) { |
| 313 | + return false; |
| 314 | + } |
| 315 | + Book book = (Book)o; |
| 316 | + return Objects.equals( isbn, book.isbn ) && Objects.equals( |
| 317 | + title, |
| 318 | + book.title |
| 319 | + ); |
| 320 | + } |
| 321 | + |
| 322 | + @Override |
| 323 | + public int hashCode() { |
| 324 | + return Objects.hash( isbn, title ); |
| 325 | + } |
| 326 | + } |
| 327 | +} |
0 commit comments