|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.dialect.unit; |
| 8 | + |
| 9 | +import org.hibernate.dialect.H2Dialect; |
| 10 | +import org.hibernate.query.sqm.mutation.internal.temptable.LocalTemporaryTableStrategy; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks; |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 15 | +import org.hibernate.testing.orm.junit.RequiresDialectFeature; |
| 16 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.hibernate.testing.orm.junit.Setting; |
| 20 | +import org.hibernate.testing.orm.junit.SkipForDialect; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import jakarta.persistence.Entity; |
| 25 | +import jakarta.persistence.Id; |
| 26 | +import jakarta.persistence.Inheritance; |
| 27 | +import jakarta.persistence.InheritanceType; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Jan Schatteman |
| 34 | + */ |
| 35 | +@DomainModel( |
| 36 | + annotatedClasses = { |
| 37 | + DialectTempTableNoCommitTest.Person.class, |
| 38 | + DialectTempTableNoCommitTest.Engineer.class, |
| 39 | + DialectTempTableNoCommitTest.Doctor.class |
| 40 | + } |
| 41 | +) |
| 42 | +@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsTemporaryTable.class) |
| 43 | +@JiraKey("HHH-17943") |
| 44 | +public class DialectTempTableNoCommitTest { |
| 45 | + |
| 46 | + private static final Long ID = 1l; |
| 47 | + |
| 48 | + @AfterEach |
| 49 | + public void tearDown(SessionFactoryScope scope) { |
| 50 | + scope.inTransaction( |
| 51 | + session -> |
| 52 | + session.createMutationQuery( "delete from Doctor" ).executeUpdate() |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @ServiceRegistry( |
| 58 | + settings = @Setting(name = LocalTemporaryTableStrategy.DROP_ID_TABLES, value = "true") |
| 59 | + ) |
| 60 | + @SkipForDialect(dialectClass = H2Dialect.class) |
| 61 | + @SessionFactory |
| 62 | + public void noCommitAfterTempTableCreationAndDropTempTableTest(SessionFactoryScope scope) { |
| 63 | + scope.inTransaction( |
| 64 | + session -> { |
| 65 | + // the following shouldn't commit anything to the DB |
| 66 | + Doctor d = new Doctor(); |
| 67 | + d.setId( ID ); |
| 68 | + session.persist( d ); |
| 69 | + session.flush(); |
| 70 | + session.createMutationQuery( "update Engineer set fellow = false where fellow = true" ) |
| 71 | + .executeUpdate(); |
| 72 | + session.getTransaction().markRollbackOnly(); |
| 73 | + } |
| 74 | + ); |
| 75 | + scope.inTransaction( |
| 76 | + session -> { |
| 77 | + assertNull( session.find( Doctor.class, ID ) ); |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + scope.inTransaction( |
| 82 | + session -> { |
| 83 | + // the following shouldn't commit anything to the DB |
| 84 | + Doctor d = new Doctor(); |
| 85 | + d.setId( ID ); |
| 86 | + session.persist( d ); |
| 87 | + session.flush(); |
| 88 | + session.createMutationQuery( "update Engineer set fellow = false where fellow = true" ) |
| 89 | + .executeUpdate(); |
| 90 | + } |
| 91 | + ); |
| 92 | + scope.inTransaction( |
| 93 | + session -> { |
| 94 | + assertNotNull( session.find( Doctor.class, ID ) ); |
| 95 | + } |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @ServiceRegistry( |
| 101 | + settings = @Setting(name = LocalTemporaryTableStrategy.DROP_ID_TABLES, value = "false") |
| 102 | + ) |
| 103 | + @SessionFactory |
| 104 | + public void noCommitAfterTempTableCreationAndNoDropTempTableTest2(SessionFactoryScope scope) { |
| 105 | + scope.inTransaction( |
| 106 | + session -> { |
| 107 | + // the following shouldn't commit anything to the DB |
| 108 | + Doctor d = new Doctor(); |
| 109 | + d.setId( ID ); |
| 110 | + session.persist( d ); |
| 111 | + session.flush(); |
| 112 | + session.createMutationQuery( "update Engineer set fellow = false where fellow = true" ) |
| 113 | + .executeUpdate(); |
| 114 | + session.getTransaction().markRollbackOnly(); |
| 115 | + } |
| 116 | + ); |
| 117 | + scope.inTransaction( |
| 118 | + session -> { |
| 119 | + assertNull( session.find( Doctor.class, ID ) ); |
| 120 | + } |
| 121 | + ); |
| 122 | + |
| 123 | + scope.inTransaction( |
| 124 | + session -> { |
| 125 | + // the following shouldn't commit anything to the DB |
| 126 | + Doctor d = new Doctor(); |
| 127 | + d.setId( ID ); |
| 128 | + session.persist( d ); |
| 129 | + session.flush(); |
| 130 | + session.createMutationQuery( "update Engineer set fellow = false where fellow = true" ) |
| 131 | + .executeUpdate(); |
| 132 | + } |
| 133 | + ); |
| 134 | + scope.inTransaction( |
| 135 | + session -> { |
| 136 | + assertNotNull( session.find( Doctor.class, ID ) ); |
| 137 | + } |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + @Entity(name = "Person") |
| 142 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 143 | + public static class Person { |
| 144 | + |
| 145 | + @Id |
| 146 | + private Long id; |
| 147 | + |
| 148 | + public Long getId() { |
| 149 | + return id; |
| 150 | + } |
| 151 | + |
| 152 | + public void setId(Long id) { |
| 153 | + this.id = id; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Entity(name = "Engineer") |
| 158 | + public static class Engineer extends Person { |
| 159 | + |
| 160 | + private boolean fellow; |
| 161 | + |
| 162 | + public boolean isFellow() { |
| 163 | + return fellow; |
| 164 | + } |
| 165 | + |
| 166 | + public void setFellow(boolean fellow) { |
| 167 | + this.fellow = fellow; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Entity(name = "Doctor") |
| 172 | + public static class Doctor { |
| 173 | + @Id |
| 174 | + private Long id; |
| 175 | + private String name; |
| 176 | + |
| 177 | + public Long getId() { |
| 178 | + return id; |
| 179 | + } |
| 180 | + |
| 181 | + public void setId(Long id) { |
| 182 | + this.id = id; |
| 183 | + } |
| 184 | + |
| 185 | + public String getName() { |
| 186 | + return name; |
| 187 | + } |
| 188 | + |
| 189 | + public void setName(String name) { |
| 190 | + this.name = name; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments