|
| 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.Arrays; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.hibernate.cfg.AvailableSettings; |
| 13 | +import org.hibernate.cfg.Configuration; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import io.vertx.junit5.VertxTestContext; |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.GeneratedValue; |
| 20 | +import jakarta.persistence.GenerationType; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.Table; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | + |
| 27 | +public class IdentityGenerationWithBatchingTest extends BaseReactiveTest { |
| 28 | + |
| 29 | + @Override |
| 30 | + protected Collection<Class<?>> annotatedEntities() { |
| 31 | + return List.of( Book.class ); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected Configuration constructConfiguration() { |
| 36 | + Configuration configuration = super.constructConfiguration(); |
| 37 | + configuration.setProperty( AvailableSettings.STATEMENT_BATCH_SIZE, "5" ); |
| 38 | + return configuration; |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void test(VertxTestContext context) { |
| 43 | + String[] titles = { |
| 44 | + "Around the World in Eighty Days", |
| 45 | + "The Book of M: A Novel", |
| 46 | + "The Poppy War", |
| 47 | + "The poetry home repair manual", |
| 48 | + "Relish" |
| 49 | + }; |
| 50 | + test( context, getMutinySessionFactory() |
| 51 | + .withTransaction( s -> s.persistAll( asBooks( titles ) ) ) |
| 52 | + .chain( () -> getMutinySessionFactory().withSession( s -> s.createSelectionQuery( "from Book order by id ASC", Book.class ).getResultList() ) ) |
| 53 | + .invoke( results -> { |
| 54 | + String[] resultTitles = results.stream().map( Book::getTitle ).toArray( String[]::new ); |
| 55 | + assertThat( resultTitles ).containsExactly( titles ); |
| 56 | + Long savedId = 0L; |
| 57 | + for ( Book book : results ) { |
| 58 | + assertThat( book.getId() ).isNotNull().isGreaterThan( savedId ); |
| 59 | + savedId = book.getId(); |
| 60 | + } |
| 61 | + } ) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + private Book[] asBooks(String[] titles) { |
| 66 | + return Arrays.asList( titles ).stream().map( Book::new ).toArray( Book[]::new ); |
| 67 | + } |
| 68 | + |
| 69 | + @Entity(name = "Book") |
| 70 | + @Table(name = "books") |
| 71 | + static class Book { |
| 72 | + |
| 73 | + @Id |
| 74 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 75 | + private Long id; |
| 76 | + |
| 77 | + private String title; |
| 78 | + |
| 79 | + public Book() { |
| 80 | + } |
| 81 | + |
| 82 | + public Book(String title) { |
| 83 | + this.title = title; |
| 84 | + } |
| 85 | + |
| 86 | + public Long getId() { |
| 87 | + return id; |
| 88 | + } |
| 89 | + |
| 90 | + public void setId(Long id) { |
| 91 | + this.id = id; |
| 92 | + } |
| 93 | + |
| 94 | + public String getTitle() { |
| 95 | + return title; |
| 96 | + } |
| 97 | + |
| 98 | + public void setTitle(String title) { |
| 99 | + this.title = title; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public String toString() { |
| 104 | + return id + ":" + title; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments