diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/BaseReactiveTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/BaseReactiveTest.java index c3178a2c3..6cd705f2b 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/BaseReactiveTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/BaseReactiveTest.java @@ -165,6 +165,9 @@ protected void addEntities(Configuration configuration) { } } + /** + * This method works for most common cases, but some tests might need to overrides it + */ public CompletionStage deleteEntities(Class... entities) { return getSessionFactory() .withTransaction( s -> loop( entities, entityClass -> s diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CompositeIdTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CompositeIdTest.java index 1e00b1b0f..7f92c66d0 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CompositeIdTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/CompositeIdTest.java @@ -67,10 +67,10 @@ private static String nameFromResult(List rowSet) { } private CompletionStage selectWeightFromId(Integer id) { - return getSessionFactory().withSession( - session -> session.createSelectionQuery("SELECT weight FROM GuineaPig WHERE id = " + id, Double.class ) - .getResultList() - .thenApply( CompositeIdTest::weightFromResult ) + return getSessionFactory().withSession( session -> session + .createSelectionQuery( "SELECT weight FROM GuineaPig WHERE id = " + id, Double.class ) + .getResultList() + .thenApply( CompositeIdTest::weightFromResult ) ); } @@ -79,93 +79,85 @@ private static Double weightFromResult(List rowSet) { case 0: return null; case 1: - return rowSet.get(0); + return rowSet.get( 0 ); default: - throw new AssertionError("More than one result returned: " + rowSet.size()); + throw new AssertionError( "More than one result returned: " + rowSet.size() ); } } @Test public void reactiveFind(VertxTestContext context) { final GuineaPig expectedPig = new GuineaPig( 5, "Aloi" ); - test( - context, - populateDB() - .thenCompose( v -> openSession() ) - .thenCompose( session -> session.find( GuineaPig.class, new Pig(5, "Aloi") ) ) - .thenAccept( actualPig -> assertThatPigsAreEqual( context, expectedPig, actualPig ) ) + test( context, populateDB() + .thenCompose( v -> openSession() ) + .thenCompose( session -> session.find( GuineaPig.class, new Pig( 5, "Aloi" ) ) ) + .thenAccept( actualPig -> assertThatPigsAreEqual( context, expectedPig, actualPig ) ) ); } @Test public void reactivePersist(VertxTestContext context) { - test( - context, - openSession() - .thenCompose( s -> s.persist( new GuineaPig( 10, "Tulip" ) ) - .thenCompose( v -> s.flush() ) - ) - .thenCompose( v -> selectNameFromId( 10 ) ) - .thenAccept( selectRes -> assertEquals( "Tulip", selectRes ) ) + test( context, openSession() + .thenCompose( s -> s + .persist( new GuineaPig( 10, "Tulip" ) ) + .thenCompose( v -> s.flush() ) + ) + .thenCompose( v -> selectNameFromId( 10 ) ) + .thenAccept( selectRes -> assertEquals( "Tulip", selectRes ) ) ); } @Test public void reactiveRemoveTransientEntity(VertxTestContext context) { - test( - context, - populateDB() - .thenCompose( v -> selectNameFromId( 5 ) ) - .thenAccept( Assertions::assertNotNull ) - .thenCompose( v -> openSession() ) - .thenCompose( session -> session.remove( new GuineaPig( 5, "Aloi" ) ) - .thenCompose( v -> session.flush() ) - .thenCompose( v -> session.close() ) - ) - .thenCompose( v -> selectNameFromId( 5 ) ) - .thenAccept( Assertions::assertNull ) - .handle( (r, e) -> { - assertNotNull( e ); - return r; - } ) + test( context, populateDB() + .thenCompose( v -> selectNameFromId( 5 ) ) + .thenAccept( Assertions::assertNotNull ) + .thenCompose( v -> openSession() ) + .thenCompose( session -> session + .remove( new GuineaPig( 5, "Aloi" ) ) + .thenCompose( v -> session.flush() ) + .thenCompose( v -> session.close() ) + ) + .thenCompose( v -> selectNameFromId( 5 ) ) + .thenAccept( Assertions::assertNull ) + .handle( (r, e) -> { + assertNotNull( e ); + return r; + } ) ); } @Test public void reactiveRemoveManagedEntity(VertxTestContext context) { - test( - context, - populateDB() - .thenCompose( v -> openSession() ) - .thenCompose( session -> - session.find( GuineaPig.class, new Pig(5, "Aloi") ) - .thenCompose( session::remove ) - .thenCompose( v -> session.flush() ) - .thenCompose( v -> selectNameFromId( session,5 ) ) - .thenAccept( Assertions::assertNull ) - ) + test( context, populateDB() + .thenCompose( v -> openSession() ) + .thenCompose( session -> session + .find( GuineaPig.class, new Pig( 5, "Aloi" ) ) + .thenCompose( session::remove ) + .thenCompose( v -> session.flush() ) + .thenCompose( v -> selectNameFromId( session, 5 ) ) + .thenAccept( Assertions::assertNull ) + ) ); } @Test public void reactiveUpdate(VertxTestContext context) { final double NEW_WEIGHT = 200.0; - test( - context, - populateDB() - .thenCompose( v -> openSession() ) - .thenCompose( session -> - session.find( GuineaPig.class, new Pig(5, "Aloi") ) - .thenAccept( pig -> { - assertNotNull( pig ); - // Checking we are actually changing the name - assertNotEquals( pig.getWeight(), NEW_WEIGHT ); - pig.setWeight( NEW_WEIGHT ); - } ) - .thenCompose( v -> session.flush() ) - .thenCompose( v -> session.close() ) - .thenCompose( v -> selectWeightFromId( 5 ) ) - .thenAccept( w -> assertEquals( NEW_WEIGHT, w ) ) ) + test( context, populateDB() + .thenCompose( v -> openSession() ) + .thenCompose( session -> session + .find( GuineaPig.class, new Pig( 5, "Aloi" ) ) + .thenAccept( pig -> { + assertNotNull( pig ); + // Checking we are actually changing the name + assertNotEquals( pig.getWeight(), NEW_WEIGHT ); + pig.setWeight( NEW_WEIGHT ); + } ) + .thenCompose( v -> session.flush() ) + .thenCompose( v -> session.close() ) + .thenCompose( v -> selectWeightFromId( 5 ) ) + .thenAccept( w -> assertEquals( NEW_WEIGHT, w ) ) ) ); } @@ -185,7 +177,8 @@ public Pig(Integer id, String name) { this.name = name; } - Pig() {} + Pig() { + } public Integer getId() { return id; @@ -197,25 +190,31 @@ public String getName() { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } Pig pig = (Pig) o; - return id.equals(pig.id) && - name.equals(pig.name); + return id.equals( pig.id ) && + name.equals( pig.name ); } @Override public int hashCode() { - return Objects.hash(id, name); + return Objects.hash( id, name ); } } - @Entity(name="GuineaPig") - @Table(name="Pig") + @Entity(name = "GuineaPig") + @Table(name = "Pig") @IdClass(Pig.class) public static class GuineaPig implements Serializable { - @Id private Integer id; - @Id private String name; + @Id + private Integer id; + @Id + private String name; private double weight = 100.0; diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/EagerUniqueKeyTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/EagerUniqueKeyTest.java index 83f51e10d..dd78cdc43 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/EagerUniqueKeyTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/EagerUniqueKeyTest.java @@ -90,11 +90,10 @@ public void testPersistWithReference(VertxTestContext context) { Bar bar = new Bar( "uniquePersist" ); test( context, getSessionFactory() .withTransaction( session -> session.persist( bar ) ) - .thenCompose( i -> getSessionFactory() - .withTransaction( session -> { - Foo foo = new Foo( session.getReference( Bar.class, bar.getId() ) ); - return session.persist( foo ).thenApply( v -> foo ); - } ) ) + .thenCompose( i -> getSessionFactory().withTransaction( session -> { + Foo foo = new Foo( session.getReference( Bar.class, bar.getId() ) ); + return session.persist( foo ).thenApply( v -> foo ); + } ) ) .thenCompose( result -> getSessionFactory().withTransaction( session -> session.fetch( result.getBar() ) ) ) .thenAccept( b -> assertEquals( "uniquePersist", b.getKey() ) ) ); diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/LazyReplaceOrphanedEntityTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/LazyReplaceOrphanedEntityTest.java index 084d55ea5..6793c2563 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/LazyReplaceOrphanedEntityTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/LazyReplaceOrphanedEntityTest.java @@ -10,8 +10,10 @@ import java.util.Collection; import java.util.List; import java.util.UUID; +import java.util.concurrent.CompletionStage; import org.hibernate.reactive.annotations.DisabledFor; +import org.hibernate.reactive.util.impl.CompletionStages; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -50,7 +52,7 @@ protected Collection> annotatedEntities() { @BeforeEach public void populateDb(VertxTestContext context) { theCampaign = new Campaign(); - theCampaign.setSchedule( new ExecutionDate(OffsetDateTime.now(), "ALPHA") ); + theCampaign.setSchedule( new ExecutionDate( OffsetDateTime.now(), "ALPHA" ) ); test( context, getMutinySessionFactory().withTransaction( (s, t) -> s.persist( theCampaign ) ) ); } @@ -70,6 +72,11 @@ public void testUpdateScheduleChange(VertxTestContext context) { ); } + @Override + protected CompletionStage cleanDb() { + return CompletionStages.voidFuture(); + } + @Test public void testUpdateWithMultipleScheduleChanges(VertxTestContext context) { test( context, getMutinySessionFactory() @@ -94,13 +101,14 @@ public void testUpdateWithMultipleScheduleChanges(VertxTestContext context) { ); } - @Entity (name="Campaign") + @Entity(name = "Campaign") public static class Campaign implements Serializable { - @Id @GeneratedValue + @Id + @GeneratedValue private Integer id; - @OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true) + @OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true) public Schedule schedule; public Campaign() { @@ -123,7 +131,7 @@ public Integer getId() { } } - @Entity (name="Schedule") + @Entity(name = "Schedule") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "schedule_type", discriminatorType = DiscriminatorType.STRING) public static abstract class Schedule implements Serializable { @@ -160,7 +168,7 @@ public String getCodeName() { } } - @Entity (name="ExecutionDate") + @Entity(name = "ExecutionDate") @DiscriminatorValue("EXECUTION_DATE") public static class ExecutionDate extends Schedule { @@ -170,7 +178,7 @@ public static class ExecutionDate extends Schedule { public ExecutionDate() { } - public ExecutionDate( OffsetDateTime start, String code_name ) { + public ExecutionDate(OffsetDateTime start, String code_name) { this.start = start; setCodeName( code_name ); } diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/OrderedOneToManyTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/OrderedOneToManyTest.java index 7edfa6e74..8955e94d0 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/OrderedOneToManyTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/OrderedOneToManyTest.java @@ -48,105 +48,91 @@ public void test(VertxTestContext context) { test( context, getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.persistAll( book1, book2, author ) ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .invoke( a -> assertFalse( Hibernate.isInitialized( a.books ) ) ) - .chain( a -> session.fetch( a.books ) ) - .invoke( books -> assertEquals( 2, books.size() ) ) + .withTransaction( session -> session.persistAll( book1, book2, author ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .invoke( a -> assertFalse( Hibernate.isInitialized( a.books ) ) ) + .chain( a -> session.fetch( a.books ) ) + .invoke( books -> assertEquals( 2, books.size() ) ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .createSelectionQuery( + "select distinct a from Author a left join fetch a.books", + Author.class ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.createSelectionQuery( - "select distinct a from Author a left join fetch a.books", - Author.class - ) - .getSingleResult() - .invoke( a -> assertTrue( Hibernate.isInitialized( a.books ) ) ) - .invoke( a -> assertEquals( 2, a.books.size() ) ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .chain( a -> session.fetch( a.books ) ) - .invoke( books -> books.remove( 0 ) ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .chain( a -> session.fetch( a.books ) ) - .invoke( books -> { - assertEquals( 1, books.size() ); - assertEquals( book2.title, books.get( 0 ).title ); - } ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .chain( a -> session.fetch( a.books ) ) - .chain( books -> session.find( Book.class, book1.id ).invoke( books::add ) ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .invoke( a -> assertFalse( Hibernate.isInitialized( a.books ) ) ) - .chain( a -> session.fetch( a.books ) ) - .invoke( books -> assertEquals( 2, books.size() ) ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .chain( a -> session.fetch( a.books ) ) - .invoke( books -> books.remove( 1 ) ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .chain( a -> session.fetch( a.books ) ) - .invoke( books -> { - assertEquals( 1, books.size() ); - assertEquals( book2.title, books.get( 0 ).title ); - } ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .chain( a -> session.fetch( a.books ) ) - .chain( books -> session.find( Book.class, book1.id ).invoke( books::add ) ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .invoke( a -> assertFalse( Hibernate.isInitialized( a.books ) ) ) - .chain( a -> session.fetch( a.books ) ) - .invoke( books -> assertEquals( 2, books.size() ) ) - ) - ) + .getSingleResult() + .invoke( a -> assertTrue( Hibernate.isInitialized( a.books ) ) ) + .invoke( a -> assertEquals( 2, a.books.size() ) ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .chain( a -> session.fetch( a.books ) ) + .invoke( books -> books.remove( 0 ) ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .chain( a -> session.fetch( a.books ) ) + .invoke( books -> { + assertEquals( 1, books.size() ); + assertEquals( book2.title, books.get( 0 ).title ); + } ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .chain( a -> session.fetch( a.books ) ) + .chain( books -> session.find( Book.class, book1.id ).invoke( books::add ) ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .invoke( a -> assertFalse( Hibernate.isInitialized( a.books ) ) ) + .chain( a -> session.fetch( a.books ) ) + .invoke( books -> assertEquals( 2, books.size() ) ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .chain( a -> session.fetch( a.books ) ) + .invoke( books -> books.remove( 1 ) ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .chain( a -> session.fetch( a.books ) ) + .invoke( books -> { + assertEquals( 1, books.size() ); + assertEquals( book2.title, books.get( 0 ).title ); + } ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .chain( a -> session.fetch( a.books ) ) + .chain( books -> session.find( Book.class, book1.id ).invoke( books::add ) ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .invoke( a -> assertFalse( Hibernate.isInitialized( a.books ) ) ) + .chain( a -> session.fetch( a.books ) ) + .invoke( books -> assertEquals( 2, books.size() ) ) + ) ) //TODO: this is broken, but I suspect it is broken in core also! -// .chain( () -> getMutinySessionFactory() -// .withTransaction( (session, transaction) -> session.find(Author.class, author.id) -// .chain( a -> session.fetch(a.books) ) -// .invoke( books -> books.add( books.remove(0) ) ) -// ) -// ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .invoke( a -> assertFalse( Hibernate.isInitialized( a.books ) ) ) - .chain( a -> session.fetch( a.books ) ) - .invoke( books -> assertEquals( 2, books.size() ) ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .invoke( a -> a.books = null ) - ) - ) - .chain( () -> getMutinySessionFactory() - .withTransaction( (session, transaction) -> session.find( Author.class, author.id ) - .chain( a -> session.fetch( a.books ) ) - .invoke( books -> assertTrue( books.isEmpty() ) ) - ) - ) +// .chain( () -> getMutinySessionFactory().withTransaction( (session, transaction) -> session +// .find( Author.class, author.id ) +// .chain( a -> session.fetch( a.books ) ) +// .invoke( books -> books.add( books.remove( 0 ) ) ) +// ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .invoke( a -> assertFalse( Hibernate.isInitialized( a.books ) ) ) + .chain( a -> session.fetch( a.books ) ) + .invoke( books -> assertEquals( 2, books.size() ) ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .invoke( a -> a.books = null ) + ) ) + .chain( () -> getMutinySessionFactory().withTransaction( session -> session + .find( Author.class, author.id ) + .chain( a -> session.fetch( a.books ) ) + .invoke( books -> assertTrue( books.isEmpty() ) ) + ) ) ); } diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/OrphanRemovalTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/OrphanRemovalTest.java index f45fa8be6..0dfd92f83 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/OrphanRemovalTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/OrphanRemovalTest.java @@ -8,7 +8,9 @@ import java.util.Collection; import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Set; +import java.util.concurrent.CompletionStage; import org.hibernate.reactive.annotations.DisabledFor; @@ -24,59 +26,83 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.Table; +import static jakarta.persistence.CascadeType.ALL; import static jakarta.persistence.CascadeType.PERSIST; import static jakarta.persistence.CascadeType.REMOVE; import static java.util.concurrent.TimeUnit.MINUTES; import static org.assertj.core.api.Assertions.assertThat; import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2; -import static org.junit.jupiter.api.Assertions.assertEquals; @Timeout( value = 10, timeUnit = MINUTES) @DisabledFor( value = DB2, reason = "IllegalStateException: Needed to have 6 in buffer but only had 0" ) public class OrphanRemovalTest extends BaseReactiveTest { + @Override + public CompletionStage deleteEntities(Class... entities) { + return getSessionFactory() + .withTransaction( s -> s + // Because of the Cascade options, all elements will be deleted + .createSelectionQuery( "from Shop", Shop.class ) + .getResultList() + .thenApply( List::toArray ) + .thenCompose( s::remove ) ); + } + @Override protected Collection> annotatedEntities() { - return List.of( Shop.class, Version.class, Product.class ); + return List.of( Version.class, Product.class, Shop.class ); } @Test public void testOrphan(VertxTestContext context) { + Product product = new Product( "ap1" ); + product.addVersion( new Version( "Tactical Nuclear Penguin" ) ); + Shop shop = new Shop( "shop" ); - Product product = new Product( "ap1", shop ); - product.addVersion( new Version( product ) ); shop.addProduct( product ); - shop.addProduct( new Product( "ap2", shop ) ); - shop.addProduct( new Product( "ap3", shop ) ); - shop.addProduct( new Product( "ap4", shop ) ); + shop.addProduct( new Product( "ap2" ) ); + shop.addProduct( new Product( "ap3" ) ); + shop.addProduct( new Product( "ap4" ) ); test( context, getSessionFactory() .withTransaction( session -> session.persist( shop ) ) - .thenCompose( v -> getSessionFactory() - .withTransaction( session -> session.find( Shop.class, shop.id ) - .thenCompose( result -> session.fetch( result.products ) - .thenApply( products -> result ) ) - .thenAccept( result -> { - // update - result.products.clear(); - result.addProduct( new Product( "bp5", result ) ); - result.addProduct( new Product( "bp6", result ) ); - result.addProduct( new Product( "bp7", result ) ); - } ) + .thenCompose( v -> getSessionFactory().withTransaction( session -> session + .createSelectionQuery( "select name from Product", String.class ) + .getResultList() + .thenAccept( list -> assertThat( list ) + .containsExactlyInAnyOrder( "ap1", "ap2", "ap3", "ap4" ) ) ) ) - .thenCompose( v -> getSessionFactory() - .withTransaction( session -> session - .createSelectionQuery( "select count(*) from Product", Long.class ) - .getSingleResult() - .thenAccept( result -> assertEquals( 3L, result ) ) + .thenCompose( v -> getSessionFactory().withTransaction( session -> session + .createSelectionQuery( "from Version", Version.class ) + .getResultList() + .thenAccept( list -> assertThat( list ) + .containsExactlyInAnyOrder( new Version( "Tactical Nuclear Penguin" ) ) ) ) ) - .thenCompose( v -> getSessionFactory() - .withTransaction( session -> session - .createSelectionQuery( "select name from Product", String.class ) - .getResultList() - .thenAccept( list -> assertThat( list ) - .containsExactlyInAnyOrder( "bp5", "bp6", "bp7" ) ) + .thenCompose( v -> getSessionFactory().withTransaction( session -> session + .find( Shop.class, shop.id ) + .thenCompose( foundShop -> session + .fetch( foundShop.products ) + .thenApply( products -> foundShop ) + ) + .thenAccept( foudnShop -> { + // update + foudnShop.products.clear(); + foudnShop.addProduct( new Product( "bp5" ) ); + foudnShop.addProduct( new Product( "bp6" ) ); + foudnShop.addProduct( new Product( "bp7" ) ); + } ) + ) ) + .thenCompose( v -> getSessionFactory().withTransaction( session -> session + .createSelectionQuery( "select name from Product", String.class ) + .getResultList() + .thenAccept( list -> assertThat( list ) + .containsExactlyInAnyOrder( "bp5", "bp6", "bp7" ) ) + ) ) + .thenCompose( v -> getSessionFactory().withTransaction( session -> session + .createSelectionQuery( "from Version", Version.class ) + .getResultList() + .thenAccept( list -> assertThat( list ).isEmpty() ) ) ) ); } @@ -88,19 +114,38 @@ public static class Version { @GeneratedValue private long id; + private String name; + @ManyToOne private Product product; - public Version(Product product) { - this.product = product; + Version() { } - Version() { + public Version(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + Version version = (Version) o; + return Objects.equals( name, version.name ); + } + + @Override + public int hashCode() { + return Objects.hashCode( name ); } @Override public String toString() { - return Version.class.getSimpleName() + ":" + id + ":" + product; + return Version.class.getSimpleName() + ":" + id + ":" + name + ":" + product; } } @@ -113,22 +158,39 @@ public static class Product { private long id; private String name; - Product(String name, Shop shop) { - this.name = name; - this.shop = shop; + Product() { } - Product() { + Product(String name) { + this.name = name; } @ManyToOne private Shop shop; - @OneToMany(mappedBy = "product", cascade = { PERSIST, REMOVE }) + @OneToMany(mappedBy = "product", cascade = ALL) private Set versions = new HashSet<>(); public void addVersion(Version version) { versions.add( version ); + version.product = this; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + Product product = (Product) o; + return Objects.equals( name, product.name ); + } + + @Override + public int hashCode() { + return Objects.hashCode( name ); } @Override @@ -146,18 +208,60 @@ public static class Shop { private long id; private String name; - Shop(String name) { - this.name = name; + Shop() { } - Shop() { + Shop(String name) { + this.name = name; } @OneToMany(mappedBy = "shop", cascade = { PERSIST, REMOVE }, orphanRemoval = true) private Set products = new HashSet<>(); + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getProducts() { + return products; + } + + public void setProducts(Set products) { + this.products = products; + } + public void addProduct(Product product) { products.add( product ); + product.shop = this; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + Shop shop = (Shop) o; + return Objects.equals( name, shop.name ); + } + + @Override + public int hashCode() { + return Objects.hashCode( name ); } @Override @@ -165,5 +269,4 @@ public String toString() { return Shop.class.getSimpleName() + ":" + id + ":" + name; } } - } diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReferenceTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReferenceTest.java index ac6da727a..64e7fb75e 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReferenceTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReferenceTest.java @@ -47,9 +47,9 @@ protected Collection> annotatedEntities() { } @Override - public CompletionStage deleteEntities(Class... entities) { + public CompletionStage deleteEntities(Class... types) { return getSessionFactory() - .withTransaction( s -> loop( entities, entityClass -> s + .withTransaction( s -> loop( types, entityClass -> s .createQuery( "from " + entityName( entityClass ), entityClass ) .getResultList() .thenCompose( list -> loop( list, s::remove ) ) ) ); diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SubselectElementCollectionForEmbeddableTypeListTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SubselectElementCollectionForEmbeddableTypeListTest.java index 15f6c3958..d78762b68 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SubselectElementCollectionForEmbeddableTypeListTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/SubselectElementCollectionForEmbeddableTypeListTest.java @@ -124,23 +124,20 @@ public void joinFetch(VertxTestContext context) { @Test public void noJoinFetch(VertxTestContext context) { - test ( - context, - getMutinySessionFactory() - .withTransaction( session -> session.createSelectionQuery("from Person p where p.name='Claude'", Person.class) - .getResultList() - .invoke( all -> assertEquals( 2, all.size() ) ) - .invoke( all -> all.forEach( result -> assertFalse( Hibernate.isInitialized( result.phones ) ) ) ) - .chain ( all -> session.fetch( all.get(0).phones ) ) - .invoke( phones -> { - assertTrue( Hibernate.isInitialized( phones ) ); - assertPhones( phones, "111-111-1111", "999-999-9999" ); - } ) - ) + test( context, getMutinySessionFactory().withTransaction( session -> session + .createSelectionQuery( "from Person p where p.name='Claude'", Person.class ) + .getResultList() + .invoke( all -> assertEquals( 2, all.size() ) ) + .invoke( all -> all.forEach( result -> assertFalse( Hibernate.isInitialized( result.phones ) ) ) ) + .chain( all -> session.fetch( all.get( 0 ).phones ) ) + .invoke( phones -> { + assertTrue( Hibernate.isInitialized( phones ) ); + assertPhones( phones, "111-111-1111", "999-999-9999" ); + } ) ) ); } - private static void assertPhones( List list, String... phones) { + private static void assertPhones(List list, String... phones) { assertEquals( phones.length, list.size() ); for ( String phone : phones ) { assertTrue( phonesContainNumber( list, phone ) ); diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JoinColumnsTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JoinColumnsTest.java index 0b6a4e320..aaea05efa 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JoinColumnsTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JoinColumnsTest.java @@ -11,7 +11,7 @@ import java.util.List; import java.util.Set; -import org.hibernate.TransientPropertyValueException; +import org.hibernate.TransientObjectException; import org.hibernate.reactive.BaseReactiveTest; import org.junit.jupiter.api.Test; @@ -176,7 +176,7 @@ public void testTransientReferenceExceptionWithStages(VertxTestContext context) sampleEntity.sampleJoinEntities.add( sampleJoinEntity ); return session.persist( sampleJoinEntity ); } ) ) - .thenAccept( t -> assertThat( t ).hasCauseInstanceOf( TransientPropertyValueException.class ) ) + .thenAccept( t -> assertThat( t ).hasCauseInstanceOf( TransientObjectException.class ) ) ); } @@ -196,7 +196,7 @@ public void testTransientReferenceExceptionWithMutiny(VertxTestContext context) sampleEntity.sampleJoinEntities.add( sampleJoinEntity ); return session.persist( sampleJoinEntity ); } ) ) - .invoke( t -> assertThat( t ).hasCauseInstanceOf( TransientPropertyValueException.class ) ) + .invoke( t -> assertThat( t ).hasCauseInstanceOf( TransientObjectException.class ) ) ); } diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JsonTypeTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JsonTypeTest.java index 5d18cdfad..30c5a8512 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JsonTypeTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JsonTypeTest.java @@ -51,9 +51,9 @@ protected Collection> annotatedEntities() { } @Override - public CompletionStage deleteEntities(Class... entities) { + public CompletionStage deleteEntities(Class... types) { return getSessionFactory() - .withTransaction( s -> loop( entities, entityClass -> s + .withTransaction( s -> loop( types, entityClass -> s .createSelectionQuery( "from JsonEntity", entityClass ) .getResultList() .thenCompose( list -> loop( list, entity -> s.remove( entity ) ) ) ) ); diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/StringToJsonTypeTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/StringToJsonTypeTest.java index 1f8b164ec..0aaee45ab 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/StringToJsonTypeTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/StringToJsonTypeTest.java @@ -53,9 +53,9 @@ protected Collection> annotatedEntities() { } @Override - public CompletionStage deleteEntities(Class... entities) { + public CompletionStage deleteEntities(Class... types) { return getSessionFactory() - .withTransaction( s -> loop( entities, entityClass -> s + .withTransaction( s -> loop( types, entityClass -> s .createQuery( "from JsonEntity", entityClass ) .getResultList() .thenCompose( list -> loop( list, entity -> s.remove( entity ) ) ) ) ); diff --git a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/UserJsonTypeTest.java b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/UserJsonTypeTest.java index 252f71a14..3bf8cc0ca 100644 --- a/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/UserJsonTypeTest.java +++ b/hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/UserJsonTypeTest.java @@ -50,9 +50,9 @@ protected Collection> annotatedEntities() { } @Override - public CompletionStage deleteEntities(Class... entities) { + public CompletionStage deleteEntities(Class... types) { return getSessionFactory() - .withTransaction( s -> loop( entities, entityClass -> s + .withTransaction( s -> loop( types, entityClass -> s .createQuery( "from JsonEntity", entityClass ) .getResultList() .thenCompose( list -> loop( list, s::remove ) ) ) );