|
| 1 | +package org.hibernate.orm.test.collection; |
| 2 | + |
| 3 | +import jakarta.persistence.*; |
| 4 | +import org.hibernate.cfg.AvailableSettings; |
| 5 | +import org.hibernate.testing.orm.junit.*; |
| 6 | +import org.junit.jupiter.api.BeforeAll; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.atomic.AtomicReference; |
| 13 | + |
| 14 | +@DomainModel(annotatedClasses = { |
| 15 | + OneToManyUnidirectionalBatchingTest.Person.class, |
| 16 | + OneToManyUnidirectionalBatchingTest.Phone.class, |
| 17 | +}) |
| 18 | +@SessionFactory |
| 19 | +@ServiceRegistry( |
| 20 | + settings = { |
| 21 | + @Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "50"), |
| 22 | + } |
| 23 | +) |
| 24 | +@JiraKey(value = "HHH-19322") |
| 25 | +public class OneToManyUnidirectionalBatchingTest { |
| 26 | + |
| 27 | + private final AtomicReference<Long> person1Id = new AtomicReference<>(); |
| 28 | + private final AtomicReference<Long> person2Id = new AtomicReference<>(); |
| 29 | + private final AtomicReference<Long> phoneId = new AtomicReference<>(); |
| 30 | + |
| 31 | + @BeforeAll |
| 32 | + public void prepare(SessionFactoryScope scope) { |
| 33 | + scope.inTransaction(session -> { |
| 34 | + Phone phone = new Phone(); |
| 35 | + Phone dontCare = new Phone(); |
| 36 | + session.persist(phone); |
| 37 | + phoneId.set(phone.id); |
| 38 | + session.persist(dontCare); |
| 39 | + |
| 40 | + Person person1 = new Person(); |
| 41 | + Person person2 = new Person(); |
| 42 | + session.persist(person1); |
| 43 | + person1Id.set(person1.id); |
| 44 | + session.persist(person2); |
| 45 | + person2Id.set(person2.id); |
| 46 | + |
| 47 | + person1.getPhones().addAll(Arrays.asList(phone, dontCare)); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testBatching(SessionFactoryScope scope) { |
| 53 | + scope.inTransaction(session -> { |
| 54 | + // load person2 first, such that changes to its phones collection are flushed before person1's |
| 55 | + Person person2 = session.find(Person.class, person2Id.get()); |
| 56 | + Person person1 = session.find(Person.class, person1Id.get()); |
| 57 | + Phone phone = session.find(Phone.class, phoneId.get()); |
| 58 | + |
| 59 | + person1.getPhones().remove(phone); |
| 60 | + person2.getPhones().add(phone); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + @Entity(name = "Person") |
| 65 | + public static class Person { |
| 66 | + |
| 67 | + @Id |
| 68 | + @GeneratedValue |
| 69 | + private Long id; |
| 70 | + |
| 71 | + @JoinColumn(name = "person_id") |
| 72 | + @OneToMany |
| 73 | + private List<Phone> phones = new ArrayList<>(); |
| 74 | + |
| 75 | + public List<Phone> getPhones() { |
| 76 | + return phones; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Entity(name = "Phone") |
| 81 | + public static class Phone { |
| 82 | + |
| 83 | + @Id |
| 84 | + @GeneratedValue |
| 85 | + private Long id; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments