|
| 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.it; |
| 7 | + |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.List; |
| 10 | +import java.util.concurrent.CompletionStage; |
| 11 | + |
| 12 | +import org.hibernate.Hibernate; |
| 13 | +import org.hibernate.LockMode; |
| 14 | +import org.hibernate.reactive.it.reference.Author; |
| 15 | +import org.hibernate.reactive.it.reference.Book; |
| 16 | +import org.hibernate.reactive.stage.Stage; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Assertions; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import io.vertx.junit5.Timeout; |
| 22 | +import io.vertx.junit5.VertxTestContext; |
| 23 | + |
| 24 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 25 | +import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture; |
| 26 | +import static org.hibernate.reactive.util.impl.CompletionStages.loop; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | + |
| 32 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 33 | + |
| 34 | +public class ReferenceBETest extends BaseReactiveIT { |
| 35 | + |
| 36 | + @Override |
| 37 | + protected Collection<Class<?>> annotatedEntities() { |
| 38 | + return List.of( Author.class, Book.class ); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public CompletionStage<Void> deleteEntities(Class<?>... entities) { |
| 43 | + return getSessionFactory() |
| 44 | + .withTransaction( s -> loop( entities, entityClass -> s |
| 45 | + .createQuery( "from " + entityName( entityClass ), entityClass ) |
| 46 | + .getResultList() |
| 47 | + .thenCompose( list -> loop( list, s::remove ) ) ) ); |
| 48 | + } |
| 49 | + |
| 50 | + private String entityName(Class<?> entityClass) { |
| 51 | + if ( Author.class.equals( entityClass ) ) { |
| 52 | + return "Writer"; |
| 53 | + } |
| 54 | + return "Tome"; |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testDetachedEntityReference(VertxTestContext context) { |
| 59 | + final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" ); |
| 60 | + |
| 61 | + test( context, getSessionFactory() |
| 62 | + .withSession( s -> s.persist( goodOmens ).thenCompose( v -> s.flush() ) ) |
| 63 | + .thenCompose( v -> getSessionFactory().withSession( s -> { |
| 64 | + Book book = s.getReference( Book.class, goodOmens.getId() ); |
| 65 | + assertFalse( Hibernate.isInitialized( book ) ); |
| 66 | + return s.persist( new Author( "Neil Gaiman", book ) ) |
| 67 | + .thenCompose( vv -> s.flush() ); |
| 68 | + } ) ) |
| 69 | + .thenCompose( v -> getSessionFactory().withSession( s -> { |
| 70 | + Book book = s.getReference( goodOmens ); |
| 71 | + assertFalse( Hibernate.isInitialized( book ) ); |
| 72 | + return s.persist( new Author( "Terry Pratchett", book ) ) |
| 73 | + .thenCompose( vv -> s.flush() ); |
| 74 | + } ) ) |
| 75 | + .thenCompose( v -> getSessionFactory().withSession( s -> { |
| 76 | + Book book = s.getReference( goodOmens ); |
| 77 | + assertFalse( Hibernate.isInitialized( book ) ); |
| 78 | + return Stage.fetch( book ).thenCompose( vv -> Stage.fetch( book.getAuthors() ) ); |
| 79 | + } ) ) |
| 80 | + .thenAccept( optionalAssociation -> { |
| 81 | + assertTrue( Hibernate.isInitialized( optionalAssociation ) ); |
| 82 | + assertNotNull( optionalAssociation ); |
| 83 | + assertEquals( 2, optionalAssociation.size() ); |
| 84 | + } ) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testDetachedProxyReference(VertxTestContext context) { |
| 90 | + final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" ); |
| 91 | + |
| 92 | + test( context, getSessionFactory() |
| 93 | + .withSession( s -> s.persist( goodOmens ).thenCompose( v -> s.flush() ) ) |
| 94 | + .thenCompose( v -> getSessionFactory().withSession( s -> { |
| 95 | + Book reference = s.getReference( goodOmens ); |
| 96 | + assertFalse( Hibernate.isInitialized( reference ) ); |
| 97 | + return completedFuture( reference ); |
| 98 | + } ) ) |
| 99 | + .thenCompose( reference -> getSessionFactory().withSession( s -> { |
| 100 | + Book book = s.getReference( Book.class, reference.getId() ); |
| 101 | + assertFalse( Hibernate.isInitialized( book ) ); |
| 102 | + assertFalse( Hibernate.isInitialized( reference ) ); |
| 103 | + return s.persist( new Author( "Neil Gaiman", book ) ) |
| 104 | + .thenCompose( v -> s.flush() ) |
| 105 | + .thenApply( v -> reference ); |
| 106 | + } ) ) |
| 107 | + .thenCompose( reference -> getSessionFactory().withSession( s -> { |
| 108 | + Book book = s.getReference( reference ); |
| 109 | + assertFalse( Hibernate.isInitialized( book ) ); |
| 110 | + assertFalse( Hibernate.isInitialized( reference ) ); |
| 111 | + return s.persist( new Author( "Terry Pratchett", book ) ) |
| 112 | + .thenCompose( v -> s.flush() ) |
| 113 | + .thenApply( v -> reference ); |
| 114 | + } ) ) |
| 115 | + .thenCompose( reference -> getSessionFactory().withSession( s -> { |
| 116 | + Book book = s.getReference( reference ); |
| 117 | + assertFalse( Hibernate.isInitialized( book ) ); |
| 118 | + assertFalse( Hibernate.isInitialized( reference ) ); |
| 119 | + return Stage.fetch( book ).thenCompose( v -> Stage.fetch( book.getAuthors() ) ); |
| 120 | + } ) ) |
| 121 | + .thenAccept( optionalAssociation -> { |
| 122 | + assertTrue( Hibernate.isInitialized( optionalAssociation ) ); |
| 123 | + assertNotNull( optionalAssociation ); |
| 124 | + assertEquals( 2, optionalAssociation.size() ); |
| 125 | + } ) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testRemoveDetachedProxy(VertxTestContext context) { |
| 131 | + final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" ); |
| 132 | + test( context, getSessionFactory() |
| 133 | + .withSession( s -> s.persist( goodOmens ).thenCompose( v -> s.flush() ) ) |
| 134 | + .thenCompose( v -> getSessionFactory().withSession( sess -> { |
| 135 | + Book reference = sess.getReference( goodOmens ); |
| 136 | + assertFalse( Hibernate.isInitialized( reference ) ); |
| 137 | + return completedFuture( reference ); |
| 138 | + } ) ) |
| 139 | + .thenCompose( reference -> getSessionFactory().withSession( s -> s |
| 140 | + .remove( s.getReference( reference ) ).thenCompose( v -> s.flush() ) ) |
| 141 | + ) |
| 142 | + .thenCompose( reference -> getSessionFactory().withSession( s -> s |
| 143 | + .find( Book.class, goodOmens.getId() ) |
| 144 | + .thenAccept( Assertions::assertNull ) |
| 145 | + ) ) |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testRemoveWithTransaction(VertxTestContext context) { |
| 151 | + final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" ); |
| 152 | + test( context, getMutinySessionFactory() |
| 153 | + .withTransaction( s -> s.persist( goodOmens ) ) |
| 154 | + .call( () -> getMutinySessionFactory() |
| 155 | + .withSession( s -> s.find( Book.class, goodOmens.getId() ) ) |
| 156 | + .invoke( book -> assertEquals( goodOmens, book ) ) ) |
| 157 | + .call( () -> getMutinySessionFactory().withTransaction( s -> s |
| 158 | + .remove( s.getReference( Book.class, goodOmens.getId() ) ) ) ) |
| 159 | + .call( () -> getMutinySessionFactory().withSession( s -> s |
| 160 | + .find( Book.class, goodOmens.getId() ) ) ) |
| 161 | + .invoke( Assertions::assertNull ) |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void testLockDetachedProxy(VertxTestContext context) { |
| 167 | + final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" ); |
| 168 | + test( context, getSessionFactory().withSession( s -> s |
| 169 | + .persist( goodOmens ).thenCompose( v -> s.flush() ) |
| 170 | + ) |
| 171 | + .thenCompose( v -> getSessionFactory().withSession( s -> { |
| 172 | + Book reference = s.getReference( goodOmens ); |
| 173 | + assertFalse( Hibernate.isInitialized( reference ) ); |
| 174 | + return completedFuture( reference ); |
| 175 | + } ) ) |
| 176 | + .thenCompose( reference -> getSessionFactory().withSession( s -> s |
| 177 | + .lock( s.getReference( reference ), LockMode.PESSIMISTIC_FORCE_INCREMENT ) |
| 178 | + .thenCompose( v -> s.flush() ) ) |
| 179 | + ) |
| 180 | + .thenCompose( v -> getSessionFactory().withSession( s -> s |
| 181 | + .find( Book.class, goodOmens.getId() ) ) |
| 182 | + .thenAccept( book -> { |
| 183 | + assertNotNull( book ); |
| 184 | + assertEquals( 2, book.getVersion() ); |
| 185 | + } ) |
| 186 | + ) |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testRefreshDetachedProxy(VertxTestContext context) { |
| 192 | + final Book goodOmens = new Book( "Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch" ); |
| 193 | + test( context, getSessionFactory().withSession( s -> s |
| 194 | + .persist( goodOmens ).thenCompose( v -> s.flush() ) |
| 195 | + ) |
| 196 | + .thenCompose( v -> getSessionFactory().withSession( s -> { |
| 197 | + Book reference = s.getReference( goodOmens ); |
| 198 | + assertFalse( Hibernate.isInitialized( reference ) ); |
| 199 | + return completedFuture( reference ); |
| 200 | + } ) ) |
| 201 | + .thenCompose( reference -> getSessionFactory().withSession( s -> s |
| 202 | + .refresh( s.getReference( reference ) ) |
| 203 | + .thenAccept( v -> assertTrue( Hibernate.isInitialized( s.getReference( reference ) ) ) ) |
| 204 | + .thenCompose( v -> s.flush() ) ) |
| 205 | + ) |
| 206 | + .thenCompose( v -> getSessionFactory().withSession( s -> s |
| 207 | + .find( Book.class, goodOmens.getId() ) |
| 208 | + .thenAccept( book -> { |
| 209 | + assertNotNull( book ); |
| 210 | + assertEquals( 1, book.getVersion() ); |
| 211 | + } ) |
| 212 | + ) ) |
| 213 | + ); |
| 214 | + } |
| 215 | +} |
0 commit comments