|
| 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.concurrent.CompletionStage; |
| 11 | + |
| 12 | +import org.hibernate.metamodel.model.domain.EntityDomainType; |
| 13 | +import org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl; |
| 14 | +import org.hibernate.query.Order; |
| 15 | +import org.hibernate.reactive.containers.DatabaseConfiguration; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.condition.DisabledIf; |
| 19 | + |
| 20 | +import io.smallrye.mutiny.Uni; |
| 21 | +import io.vertx.junit5.Timeout; |
| 22 | +import io.vertx.junit5.VertxTestContext; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.metamodel.SingularAttribute; |
| 26 | + |
| 27 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 28 | +import static java.util.stream.Collectors.toList; |
| 29 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL; |
| 30 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 33 | + |
| 34 | +@DisabledIf( "skipDBs") |
| 35 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 36 | +public class OrderTest extends BaseReactiveTest { |
| 37 | + final Book book1 = new Book("9781932394153", "Hibernate in Action"); |
| 38 | + final Book book2 = new Book("9781617290459", "Java Persistence with Hibernate"); |
| 39 | + |
| 40 | + @Override |
| 41 | + protected Collection<Class<?>> annotatedEntities() { |
| 42 | + return List.of( |
| 43 | + Book.class |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + static boolean skipDBs() { |
| 48 | + // MySQL & MariaDB will fail because the simple 'String isbn' element will prevent book from being created correctly |
| 49 | + // errorMessage=Table 'hreact.Book' doesn't exist, errorCode=1146 |
| 50 | + // NOTE: ORM's OrderTest will also fail with these DB's |
| 51 | + return DatabaseConfiguration.dbType() == MYSQL || |
| 52 | + DatabaseConfiguration.dbType() == MARIA; |
| 53 | + } |
| 54 | + |
| 55 | + private CompletionStage<Void> populateDB() { |
| 56 | + return getSessionFactory() |
| 57 | + .withTransaction( session -> session.persist( book1, book2 ) ); |
| 58 | + } |
| 59 | + |
| 60 | + private Uni<Void> populateDBMunity() { |
| 61 | + return getMutinySessionFactory() |
| 62 | + .withSession( session -> session.persistAll( book1, book2 ) |
| 63 | + .chain( session::flush ) ); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testOrder(VertxTestContext context) { |
| 68 | + test( |
| 69 | + context, |
| 70 | + populateDB().thenCompose( v -> getSessionFactory() |
| 71 | + .withTransaction( (session, tx) -> session |
| 72 | + .createSelectionQuery( "select title from Book", String.class ) |
| 73 | + .getResultList() |
| 74 | + .thenAccept( list -> assertTrue( list.size() == 2 ) ) |
| 75 | + .thenCompose( vv -> { |
| 76 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 77 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel() |
| 78 | + .findEntityType( Book.class ); |
| 79 | + SingularAttribute<? super Book, ?> title = bookType.findSingularAttribute( "title" ); |
| 80 | + return session |
| 81 | + .createSelectionQuery( "from Book", Book.class ) |
| 82 | + .setOrder( Order.asc( title ) ) |
| 83 | + .getResultList(); |
| 84 | + } ).thenAccept( books -> { |
| 85 | + assertEquals( "Hibernate in Action", books.get( 0 ).title ); |
| 86 | + assertEquals( "Java Persistence with Hibernate", books.get( 1 ).title ); |
| 87 | + } ) |
| 88 | + .thenCompose( vv -> { |
| 89 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 90 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel() |
| 91 | + .findEntityType( Book.class ); |
| 92 | + SingularAttribute<? super Book, ?> title = bookType.findSingularAttribute( "title" ); |
| 93 | + return session |
| 94 | + .createSelectionQuery( "from Book", Book.class ) |
| 95 | + .setOrder( Order.desc( title ) ) |
| 96 | + .getResultList(); |
| 97 | + } ).thenAccept( books -> { |
| 98 | + assertEquals( "Hibernate in Action", books.get( 1 ).title ); |
| 99 | + assertEquals( "Java Persistence with Hibernate", books.get( 0 ).title ); |
| 100 | + } ) |
| 101 | + .thenCompose( vv -> { |
| 102 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 103 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel() |
| 104 | + .findEntityType( Book.class ); |
| 105 | + SingularAttribute<? super Book, ?> isbn = bookType.findSingularAttribute( "isbn" ); |
| 106 | + return session |
| 107 | + .createSelectionQuery( "from Book", Book.class ) |
| 108 | + .setOrder( Order.asc( isbn ) ) |
| 109 | + .getResultList(); |
| 110 | + } ).thenAccept( books -> { |
| 111 | + assertEquals( "Hibernate in Action", books.get( 1 ).title ); |
| 112 | + assertEquals( "Java Persistence with Hibernate", books.get( 0 ).title ); |
| 113 | + } ) |
| 114 | + .thenCompose( vv -> { |
| 115 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 116 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel() |
| 117 | + .findEntityType( Book.class ); |
| 118 | + SingularAttribute<? super Book, ?> isbn = bookType.findSingularAttribute( "isbn" ); |
| 119 | + return session |
| 120 | + .createSelectionQuery( "from Book", Book.class ) |
| 121 | + .setOrder( Order.desc( isbn ) ) |
| 122 | + .getResultList(); |
| 123 | + } ).thenAccept( books -> { |
| 124 | + assertEquals( "Hibernate in Action", books.get( 0 ).title ); |
| 125 | + assertEquals( "Java Persistence with Hibernate", books.get( 1 ).title ); |
| 126 | + } ) |
| 127 | + ) ) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testAscendingDescendingBySelectElement(VertxTestContext context) { |
| 133 | + test( |
| 134 | + context, |
| 135 | + populateDB().thenCompose( v -> getSessionFactory() |
| 136 | + .withSession( session -> |
| 137 | + session.createSelectionQuery( |
| 138 | + "select isbn, title from Book", |
| 139 | + Object[].class |
| 140 | + ) |
| 141 | + .setOrder( Order.asc( 2 ) ).getResultList() |
| 142 | + .thenAccept( list -> { |
| 143 | + assertEquals( list.size(), 2 ); |
| 144 | + List<?> titles = list.stream().map( book -> book[1] ).collect( toList() ); |
| 145 | + assertEquals( "Hibernate in Action", titles.get( 0 ) ); |
| 146 | + assertEquals( "Java Persistence with Hibernate", titles.get( 1 ) ); |
| 147 | + } ) |
| 148 | + .thenCompose( vv -> session.createSelectionQuery( |
| 149 | + "select isbn, title from Book", |
| 150 | + Object[].class |
| 151 | + ) |
| 152 | + .setOrder( Order.desc( 2 ) ).getResultList() |
| 153 | + .thenAccept( list -> { |
| 154 | + assertEquals( list.size(), 2 ); |
| 155 | + List<?> titles = list.stream().map( book -> book[1] ).collect( toList() ); |
| 156 | + assertEquals( "Hibernate in Action", titles.get( 1 ) ); |
| 157 | + assertEquals( "Java Persistence with Hibernate", titles.get( 0 ) ); |
| 158 | + } ) |
| 159 | + ) |
| 160 | + ) ) |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testOrderMutiny(VertxTestContext context) { |
| 166 | + test( |
| 167 | + context, |
| 168 | + populateDBMunity().call( () -> getMutinySessionFactory().withSession( |
| 169 | + session -> session.createSelectionQuery( "select title from Book", String.class ) |
| 170 | + .getResultList() |
| 171 | + .invoke( list -> assertTrue( list.size() == 2 ) ) |
| 172 | + .chain( vv -> { |
| 173 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 174 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel() |
| 175 | + .findEntityType( Book.class ); |
| 176 | + SingularAttribute<? super Book, ?> title = bookType.findSingularAttribute( "title" ); |
| 177 | + return session |
| 178 | + .createSelectionQuery( "from Book", Book.class ) |
| 179 | + .setOrder( Order.asc( title ) ) |
| 180 | + .getResultList(); |
| 181 | + } ).invoke( books -> { |
| 182 | + assertEquals( "Hibernate in Action", books.get( 0 ).title ); |
| 183 | + assertEquals( "Java Persistence with Hibernate", books.get( 1 ).title ); |
| 184 | + } ) |
| 185 | + .chain( vv -> { |
| 186 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 187 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel() |
| 188 | + .findEntityType( Book.class ); |
| 189 | + SingularAttribute<? super Book, ?> title = bookType.findSingularAttribute( "title" ); |
| 190 | + return session |
| 191 | + .createSelectionQuery( "from Book", Book.class ) |
| 192 | + .setOrder( Order.desc( title ) ) |
| 193 | + .getResultList(); |
| 194 | + } ).invoke( books -> { |
| 195 | + assertEquals( "Hibernate in Action", books.get( 1 ).title ); |
| 196 | + assertEquals( "Java Persistence with Hibernate", books.get( 0 ).title ); |
| 197 | + } ) |
| 198 | + .chain( vv -> { |
| 199 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 200 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel() |
| 201 | + .findEntityType( Book.class ); |
| 202 | + SingularAttribute<? super Book, ?> isbn = bookType.findSingularAttribute( "isbn" ); |
| 203 | + return session |
| 204 | + .createSelectionQuery( "from Book", Book.class ) |
| 205 | + .setOrder( Order.asc( isbn ) ) |
| 206 | + .getResultList(); |
| 207 | + } ).invoke( books -> { |
| 208 | + assertEquals( "Hibernate in Action", books.get( 1 ).title ); |
| 209 | + assertEquals( "Java Persistence with Hibernate", books.get( 0 ).title ); |
| 210 | + } ) |
| 211 | + .chain( vv -> { |
| 212 | + MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel(); |
| 213 | + EntityDomainType<Book> bookType = metamodel.getJpaMetamodel() |
| 214 | + .findEntityType( Book.class ); |
| 215 | + SingularAttribute<? super Book, ?> isbn = bookType.findSingularAttribute( "isbn" ); |
| 216 | + return session |
| 217 | + .createSelectionQuery( "from Book", Book.class ) |
| 218 | + .setOrder( Order.desc( isbn ) ) |
| 219 | + .getResultList(); |
| 220 | + } ).invoke( books -> { |
| 221 | + assertEquals( "Hibernate in Action", books.get( 0 ).title ); |
| 222 | + assertEquals( "Java Persistence with Hibernate", books.get( 1 ).title ); |
| 223 | + } ) |
| 224 | + ) ) |
| 225 | + ); |
| 226 | + } |
| 227 | + |
| 228 | + @Entity(name="Book") |
| 229 | + static class Book { |
| 230 | + @Id |
| 231 | + String isbn; |
| 232 | + String title; |
| 233 | + |
| 234 | + Book(String isbn, String title) { |
| 235 | + this.isbn = isbn; |
| 236 | + this.title = title; |
| 237 | + } |
| 238 | + |
| 239 | + Book() { |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments