|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.collection.stale; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.GeneratedValue; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.OneToMany; |
| 12 | +import jakarta.persistence.OrderColumn; |
| 13 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 14 | +import org.hibernate.testing.orm.junit.FailureExpected; |
| 15 | +import org.hibernate.testing.orm.junit.Jpa; |
| 16 | +import org.hibernate.testing.orm.junit.Setting; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import static org.hibernate.cfg.BatchSettings.STATEMENT_BATCH_SIZE; |
| 23 | + |
| 24 | +@Jpa(annotatedClasses = {StaleOneToManyListTest.StaleListTestParent.class, |
| 25 | + StaleOneToManyListTest.StaleListTestChild.class}, |
| 26 | + properties = @Setting(name = STATEMENT_BATCH_SIZE, value = "5")) |
| 27 | +public class StaleOneToManyListTest { |
| 28 | + @Test void test1(EntityManagerFactoryScope scope) { |
| 29 | + var entity = new StaleListTestParent(); |
| 30 | + entity.childList.add( new StaleListTestChild("hello") ); |
| 31 | + entity.childList.add( new StaleListTestChild("world") ); |
| 32 | + scope.inTransaction( session -> { |
| 33 | + session.persist( entity ); |
| 34 | + } ); |
| 35 | + scope.inTransaction( session -> { |
| 36 | + var e = session.find( StaleListTestParent.class, entity.id ); |
| 37 | + e.childList.set( 0, new StaleListTestChild("goodbye") ); |
| 38 | + scope.inTransaction( session2 -> { |
| 39 | + var e2 = session2.find( StaleListTestParent.class, entity.id ); |
| 40 | + e2.childList.set( 0, new StaleListTestChild("BYE") ); |
| 41 | + } ); |
| 42 | + } ); |
| 43 | + } |
| 44 | + @Test void test2(EntityManagerFactoryScope scope) { |
| 45 | + var entity = new StaleListTestParent(); |
| 46 | + entity.childList.add( new StaleListTestChild("hello") ); |
| 47 | + entity.childList.add( new StaleListTestChild("world") ); |
| 48 | + scope.inTransaction( session -> { |
| 49 | + session.persist( entity ); |
| 50 | + } ); |
| 51 | + scope.inTransaction( session -> { |
| 52 | + var e = session.find( StaleListTestParent.class, entity.id ); |
| 53 | + e.childList.set( 1, new StaleListTestChild("everyone") ); |
| 54 | + scope.inTransaction( session2 -> { |
| 55 | + var e2 = session2.find( StaleListTestParent.class, entity.id ); |
| 56 | + e2.childList.remove( 1 ); |
| 57 | + } ); |
| 58 | + } ); |
| 59 | + } |
| 60 | + @FailureExpected(reason = "ConstraintViolationException") |
| 61 | + @Test void test3(EntityManagerFactoryScope scope) { |
| 62 | + var parent1 = new StaleListTestParent(); |
| 63 | + var parent2 = new StaleListTestParent(); |
| 64 | + var child1 = new StaleListTestChild( "hello" ); |
| 65 | + var child2 = new StaleListTestChild( "world" ); |
| 66 | + parent1.childList.add( child1 ); |
| 67 | + parent1.childList.add( child2 ); |
| 68 | + scope.inTransaction( session -> { |
| 69 | + session.persist( parent1 ); |
| 70 | + session.persist( parent2 ); |
| 71 | + } ); |
| 72 | + scope.inTransaction( session1 -> { |
| 73 | + var p = session1.find( StaleListTestParent.class, parent1.id ); |
| 74 | + var c = session1.find( StaleListTestChild.class, child1.id ); |
| 75 | + p.childList.remove( c ); |
| 76 | + scope.inTransaction( session2 -> { |
| 77 | + var pp = session2.find( StaleListTestParent.class, parent2.id ); |
| 78 | + var cc = session2.find( StaleListTestChild.class, child1.id ); |
| 79 | + pp.childList.add( cc ); |
| 80 | + } ); |
| 81 | + } ); |
| 82 | + } |
| 83 | + @Test void test4(EntityManagerFactoryScope scope) { |
| 84 | + var parent1 = new StaleListTestParent(); |
| 85 | + var parent2 = new StaleListTestParent(); |
| 86 | + var child1 = new StaleListTestChild( "hello" ); |
| 87 | + var child2 = new StaleListTestChild( "world" ); |
| 88 | + parent1.childList.add( child1 ); |
| 89 | + parent1.childList.add( child2 ); |
| 90 | + scope.inTransaction( session -> { |
| 91 | + session.persist( parent1 ); |
| 92 | + session.persist( parent2 ); |
| 93 | + } ); |
| 94 | + scope.inTransaction( session1 -> { |
| 95 | + var p = session1.find( StaleListTestParent.class, parent1.id ); |
| 96 | + var c = session1.find( StaleListTestChild.class, child1.id ); |
| 97 | + p.childList.remove( c ); |
| 98 | + } ); |
| 99 | + scope.inTransaction( session2 -> { |
| 100 | + var pp = session2.find( StaleListTestParent.class, parent2.id ); |
| 101 | + var cc = session2.find( StaleListTestChild.class, child1.id ); |
| 102 | + pp.childList.add( cc ); |
| 103 | + } ); |
| 104 | + } |
| 105 | + static @Entity(name = "StaleParent") class StaleListTestParent { |
| 106 | + @GeneratedValue @Id |
| 107 | + long id; |
| 108 | + @OneToMany(cascade = CascadeType.PERSIST) @OrderColumn |
| 109 | + List<StaleListTestChild> childList = new ArrayList<>(); |
| 110 | + } |
| 111 | + static @Entity(name = "StaleChild") class StaleListTestChild { |
| 112 | + @GeneratedValue @Id |
| 113 | + long id; |
| 114 | + String text; |
| 115 | + StaleListTestChild(String text) { |
| 116 | + this.text = text; |
| 117 | + } |
| 118 | + StaleListTestChild() { |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments