|
| 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 | + |
| 11 | + |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import io.vertx.ext.unit.TestContext; |
| 15 | +import jakarta.persistence.Entity; |
| 16 | +import jakarta.persistence.GeneratedValue; |
| 17 | +import jakarta.persistence.Id; |
| 18 | + |
| 19 | +/** |
| 20 | + * Verifies that scheduled changes in the reactive session |
| 21 | + * are being auto-flushed before a mutation query on the same |
| 22 | + * table space is executed. |
| 23 | + */ |
| 24 | +public class PersistThenDeleteTest extends BaseReactiveTest { |
| 25 | + |
| 26 | + @Override |
| 27 | + protected Collection<Class<?>> annotatedEntities() { |
| 28 | + return List.of( PersistThenDeleteTest.Person.class ); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testPersistThenDelete(TestContext context) { |
| 33 | + test(context, |
| 34 | + getSessionFactory().withTransaction( |
| 35 | + (s, t) -> s.persist( newPerson( "foo" ), newPerson( "bar" ), newPerson( "baz" ) ) |
| 36 | + ) |
| 37 | + .thenCompose( |
| 38 | + v -> |
| 39 | + getSessionFactory().withTransaction( |
| 40 | + (s, t) -> s.createQuery( "from Person" ).getResultList().thenAccept( l -> |
| 41 | + context.assertEquals( 3, l.size() ) |
| 42 | + ) |
| 43 | + ) |
| 44 | + ) |
| 45 | + .thenCompose( |
| 46 | + v -> |
| 47 | + getSessionFactory().withTransaction( |
| 48 | + (s, t) -> |
| 49 | + s.persist( newPerson( "critical" ) ) |
| 50 | + .thenCompose( vo -> s.createQuery( "delete from Person" ) |
| 51 | + .executeUpdate() ) |
| 52 | + ) |
| 53 | + ) |
| 54 | + .thenCompose( |
| 55 | + v -> |
| 56 | + getSessionFactory().withTransaction( |
| 57 | + (s, t) -> s.createQuery( "from Person" ).getResultList().thenAccept( l -> |
| 58 | + context.assertEquals( 0, l.size() ) |
| 59 | + ) |
| 60 | + ) |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testDeleteThenPersist(TestContext context) { |
| 67 | + test(context, |
| 68 | + getSessionFactory().withTransaction( |
| 69 | + (s, t) -> s.persist( newPerson( "foo" ), newPerson( "bar" ), newPerson( "baz" ) ) |
| 70 | + ) |
| 71 | + .thenCompose( |
| 72 | + v -> |
| 73 | + getSessionFactory().withTransaction( |
| 74 | + (s, t) -> s.createQuery( "from Person" ).getResultList().thenAccept( l -> |
| 75 | + context.assertEquals( 3, l.size() ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + ) |
| 79 | + .thenCompose( |
| 80 | + v -> |
| 81 | + getSessionFactory().withTransaction( |
| 82 | + (s, t) -> |
| 83 | + s.createQuery( "delete from Person" ).executeUpdate() |
| 84 | + .thenCompose( vo -> s.persist( newPerson( "critical" ) ) ) |
| 85 | + ) |
| 86 | + ) |
| 87 | + .thenCompose( |
| 88 | + v -> |
| 89 | + getSessionFactory().withTransaction( |
| 90 | + (s, t) -> s.createQuery( "from Person" ).getResultList().thenAccept( l -> |
| 91 | + context.assertEquals( 1, l.size() ) |
| 92 | + ) |
| 93 | + ) |
| 94 | + ) |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + private static Person newPerson(String name) { |
| 99 | + final Person person = new Person(); |
| 100 | + person.name = name; |
| 101 | + return person; |
| 102 | + } |
| 103 | + |
| 104 | + @Entity(name="Person") |
| 105 | + public static class Person { |
| 106 | + |
| 107 | + @Id |
| 108 | + @GeneratedValue |
| 109 | + Integer id; |
| 110 | + |
| 111 | + String name; |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments