|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.actionqueue; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.OneToMany; |
| 11 | +import org.hibernate.cfg.BatchSettings; |
| 12 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 13 | +import org.hibernate.testing.orm.junit.Jpa; |
| 14 | +import org.hibernate.testing.orm.junit.Setting; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import java.util.HashSet; |
| 18 | +import java.util.Set; |
| 19 | + |
| 20 | +@Jpa(annotatedClasses = |
| 21 | + {CollectionUpdateOrderingTest.Thing.class, |
| 22 | + CollectionUpdateOrderingTest.OtherThing.class}, |
| 23 | + integrationSettings = @Setting(name = BatchSettings.ORDER_UPDATES, value = "true")) |
| 24 | +class CollectionUpdateOrderingTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + void test(EntityManagerFactoryScope scope) { |
| 28 | + scope.inTransaction( session -> { |
| 29 | + Thing thing1 = new Thing(); |
| 30 | + Thing thing2 = new Thing(); |
| 31 | + thing2.id = 2; |
| 32 | + thing1.id = 3; //bigger |
| 33 | + OtherThing otherThing1 = new OtherThing(); |
| 34 | + OtherThing otherThing2 = new OtherThing(); |
| 35 | + otherThing1.id = 1; |
| 36 | + otherThing2.id = 2; |
| 37 | + thing1.otherThings.add(otherThing1); |
| 38 | + thing1.otherThings.add(otherThing2); |
| 39 | + session.persist( thing1 ); |
| 40 | + session.persist( thing2 ); |
| 41 | + } ); |
| 42 | + scope.inTransaction( session -> { |
| 43 | + Thing thing2 = session.find(Thing.class, 2L); |
| 44 | + Thing thing1 = session.find(Thing.class, 3L); |
| 45 | + OtherThing otherThing = thing1.otherThings.iterator().next(); |
| 46 | + thing1.otherThings.remove(otherThing); |
| 47 | + thing2.otherThings.add(otherThing); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + @Entity |
| 52 | + static class Thing { |
| 53 | + @Id long id; |
| 54 | + @OneToMany(cascade = CascadeType.PERSIST) |
| 55 | + Set<OtherThing> otherThings = new HashSet<>(); |
| 56 | + } |
| 57 | + |
| 58 | + @Entity |
| 59 | + static class OtherThing { |
| 60 | + @Id long id; |
| 61 | + } |
| 62 | +} |
0 commit comments