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