|
| 1 | +package org.hibernate.orm.test.flush; |
| 2 | + |
| 3 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 4 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 5 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import jakarta.persistence.Entity; |
| 11 | +import jakarta.persistence.GeneratedValue; |
| 12 | +import jakarta.persistence.Id; |
| 13 | +import jakarta.persistence.OneToOne; |
| 14 | + |
| 15 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 16 | + |
| 17 | + |
| 18 | +@DomainModel( |
| 19 | + annotatedClasses = { |
| 20 | + AutoFlushOnUpdateQueryTest.FruitLogEntry.class, |
| 21 | + AutoFlushOnUpdateQueryTest.Fruit.class, |
| 22 | + } |
| 23 | +) |
| 24 | +@SessionFactory |
| 25 | +public class AutoFlushOnUpdateQueryTest { |
| 26 | + |
| 27 | + public static final String FRUIT_NAME = "Apple"; |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + public void setUp(SessionFactoryScope scope) { |
| 31 | + scope.inTransaction( |
| 32 | + session -> { |
| 33 | + session.persist( new Fruit( FRUIT_NAME ) ); |
| 34 | + } |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + @AfterEach |
| 39 | + public void tearDown(SessionFactoryScope scope) { |
| 40 | + scope.inTransaction( |
| 41 | + session -> { |
| 42 | + session.createMutationQuery( "delete from Fruit" ).executeUpdate(); |
| 43 | + session.createMutationQuery( "delete from FruitLogEntry" ).executeUpdate(); |
| 44 | + } |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testFlushIsExecuted(SessionFactoryScope scope) { |
| 50 | + scope.inTransaction( |
| 51 | + session -> { |
| 52 | + Fruit fruit = session |
| 53 | + .createQuery( |
| 54 | + "select f from Fruit f where f.name = :name", |
| 55 | + Fruit.class |
| 56 | + ).setParameter( "name", FRUIT_NAME ).getSingleResult(); |
| 57 | + |
| 58 | + FruitLogEntry logEntry = new FruitLogEntry( fruit, "foo" ); |
| 59 | + session.persist( logEntry ); |
| 60 | + |
| 61 | + session.createMutationQuery( "update Fruit f set f.logEntry = :logEntry where f.id = :fruitId" ) |
| 62 | + .setParameter( "logEntry", logEntry ) |
| 63 | + .setParameter( "fruitId", fruit.getId() ).executeUpdate(); |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + scope.inTransaction( |
| 68 | + session -> { |
| 69 | + Fruit fruit = session |
| 70 | + .createQuery( |
| 71 | + "select f from Fruit f where f.name = :name", |
| 72 | + Fruit.class |
| 73 | + ).setParameter( "name", FRUIT_NAME ).getSingleResult(); |
| 74 | + assertThat( fruit.getLogEntry() ).isNotNull(); |
| 75 | + } |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testFlushIsExecuted2(SessionFactoryScope scope) { |
| 81 | + scope.inTransaction( |
| 82 | + session -> { |
| 83 | + Fruit fruit = session |
| 84 | + .createQuery( |
| 85 | + "select f from Fruit f where f.name = :name", |
| 86 | + Fruit.class |
| 87 | + ).setParameter( "name", FRUIT_NAME ).getSingleResult(); |
| 88 | + |
| 89 | + FruitLogEntry logEntry = new FruitLogEntry( fruit, "foo" ); |
| 90 | + session.persist( logEntry ); |
| 91 | + |
| 92 | + session.createMutationQuery( "update Fruit f set f.logEntry.id = :logEntryId where f.id = :fruitId" ) |
| 93 | + .setParameter( "logEntryId", logEntry.getId() ) |
| 94 | + .setParameter( "fruitId", fruit.getId() ).executeUpdate(); |
| 95 | + } |
| 96 | + ); |
| 97 | + |
| 98 | + scope.inTransaction( |
| 99 | + session -> { |
| 100 | + Fruit fruit = session |
| 101 | + .createQuery( |
| 102 | + "select f from Fruit f where f.name = :name", |
| 103 | + Fruit.class |
| 104 | + ).setParameter( "name", FRUIT_NAME ).getSingleResult(); |
| 105 | + assertThat( fruit.getLogEntry() ).isNotNull(); |
| 106 | + } |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + @Entity(name = "Fruit") |
| 111 | + public static class Fruit { |
| 112 | + |
| 113 | + @Id |
| 114 | + @GeneratedValue |
| 115 | + private Long id; |
| 116 | + |
| 117 | + private String name; |
| 118 | + |
| 119 | + @OneToOne |
| 120 | + private FruitLogEntry logEntry; |
| 121 | + |
| 122 | + public Fruit() { |
| 123 | + } |
| 124 | + |
| 125 | + public Fruit(String name) { |
| 126 | + this.name = name; |
| 127 | + } |
| 128 | + |
| 129 | + public Long getId() { |
| 130 | + return id; |
| 131 | + } |
| 132 | + |
| 133 | + public String getName() { |
| 134 | + return name; |
| 135 | + } |
| 136 | + |
| 137 | + public FruitLogEntry getLogEntry() { |
| 138 | + return logEntry; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Entity(name = "FruitLogEntry") |
| 143 | + public static class FruitLogEntry { |
| 144 | + |
| 145 | + @Id |
| 146 | + @GeneratedValue |
| 147 | + private Long id; |
| 148 | + |
| 149 | + @OneToOne(mappedBy = "logEntry") |
| 150 | + private Fruit fruit; |
| 151 | + |
| 152 | + private String logComments; |
| 153 | + |
| 154 | + public FruitLogEntry(Fruit fruit, String comment) { |
| 155 | + this.fruit = fruit; |
| 156 | + this.logComments = comment; |
| 157 | + } |
| 158 | + |
| 159 | + FruitLogEntry() { |
| 160 | + } |
| 161 | + |
| 162 | + public Long getId() { |
| 163 | + return id; |
| 164 | + } |
| 165 | + |
| 166 | + public Fruit getFruit() { |
| 167 | + return fruit; |
| 168 | + } |
| 169 | + |
| 170 | + public String getLogComments() { |
| 171 | + return logComments; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments