From 7ae416d6dd31f767810525e262c6dc8f6dec0fad Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 20 May 2024 18:22:14 +0200 Subject: [PATCH] HHH-17943 - Add test for issue --- .../unit/DialectTempTableNoCommitTest.java | 194 ++++++++++++++++++ .../unit/H2DialectTempTableNoCommitTest.java | 113 ---------- 2 files changed, 194 insertions(+), 113 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/dialect/unit/DialectTempTableNoCommitTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/dialect/unit/H2DialectTempTableNoCommitTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/unit/DialectTempTableNoCommitTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/unit/DialectTempTableNoCommitTest.java new file mode 100644 index 0000000000..c448d9e303 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/unit/DialectTempTableNoCommitTest.java @@ -0,0 +1,194 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.dialect.unit; + +import org.hibernate.dialect.H2Dialect; +import org.hibernate.query.sqm.mutation.internal.temptable.LocalTemporaryTableStrategy; + +import org.hibernate.testing.orm.junit.DialectFeatureChecks; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.hibernate.testing.orm.junit.SkipForDialect; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * @author Jan Schatteman + */ +@DomainModel( + annotatedClasses = { + DialectTempTableNoCommitTest.Person.class, + DialectTempTableNoCommitTest.Engineer.class, + DialectTempTableNoCommitTest.Doctor.class + } +) +@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsTemporaryTable.class) +@JiraKey("HHH-17943") +public class DialectTempTableNoCommitTest { + + private static final Long ID = 1l; + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createMutationQuery( "delete from Doctor" ).executeUpdate() + ); + } + + @Test + @ServiceRegistry( + settings = @Setting(name = LocalTemporaryTableStrategy.DROP_ID_TABLES, value = "true") + ) + @SkipForDialect(dialectClass = H2Dialect.class) + @SessionFactory + public void noCommitAfterTempTableCreationAndDropTempTableTest(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // the following shouldn't commit anything to the DB + Doctor d = new Doctor(); + d.setId( ID ); + session.persist( d ); + session.flush(); + session.createMutationQuery( "update Engineer set fellow = false where fellow = true" ) + .executeUpdate(); + session.getTransaction().markRollbackOnly(); + } + ); + scope.inTransaction( + session -> { + assertNull( session.find( Doctor.class, ID ) ); + } + ); + + scope.inTransaction( + session -> { + // the following shouldn't commit anything to the DB + Doctor d = new Doctor(); + d.setId( ID ); + session.persist( d ); + session.flush(); + session.createMutationQuery( "update Engineer set fellow = false where fellow = true" ) + .executeUpdate(); + } + ); + scope.inTransaction( + session -> { + assertNotNull( session.find( Doctor.class, ID ) ); + } + ); + } + + @Test + @ServiceRegistry( + settings = @Setting(name = LocalTemporaryTableStrategy.DROP_ID_TABLES, value = "false") + ) + @SessionFactory + public void noCommitAfterTempTableCreationAndNoDropTempTableTest2(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + // the following shouldn't commit anything to the DB + Doctor d = new Doctor(); + d.setId( ID ); + session.persist( d ); + session.flush(); + session.createMutationQuery( "update Engineer set fellow = false where fellow = true" ) + .executeUpdate(); + session.getTransaction().markRollbackOnly(); + } + ); + scope.inTransaction( + session -> { + assertNull( session.find( Doctor.class, ID ) ); + } + ); + + scope.inTransaction( + session -> { + // the following shouldn't commit anything to the DB + Doctor d = new Doctor(); + d.setId( ID ); + session.persist( d ); + session.flush(); + session.createMutationQuery( "update Engineer set fellow = false where fellow = true" ) + .executeUpdate(); + } + ); + scope.inTransaction( + session -> { + assertNotNull( session.find( Doctor.class, ID ) ); + } + ); + } + + @Entity(name = "Person") + @Inheritance(strategy = InheritanceType.JOINED) + public static class Person { + + @Id + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } + + @Entity(name = "Engineer") + public static class Engineer extends Person { + + private boolean fellow; + + public boolean isFellow() { + return fellow; + } + + public void setFellow(boolean fellow) { + this.fellow = fellow; + } + } + + @Entity(name = "Doctor") + public static class Doctor { + @Id + private Long id; + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/unit/H2DialectTempTableNoCommitTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/unit/H2DialectTempTableNoCommitTest.java deleted file mode 100644 index 361952ec08..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/unit/H2DialectTempTableNoCommitTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later - * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html - */ -package org.hibernate.orm.test.dialect.unit; - -import org.hibernate.dialect.H2Dialect; - -import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.JiraKey; -import org.hibernate.testing.orm.junit.RequiresDialect; -import org.hibernate.testing.orm.junit.SessionFactory; -import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.junit.jupiter.api.Test; - -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.Inheritance; -import jakarta.persistence.InheritanceType; - -import static org.junit.jupiter.api.Assertions.assertNull; - -/** - * @author Jan Schatteman - */ -@DomainModel( - annotatedClasses = { - H2DialectTempTableNoCommitTest.Person.class, - H2DialectTempTableNoCommitTest.Engineer.class, - H2DialectTempTableNoCommitTest.Doctor.class - } -) -@SessionFactory -@RequiresDialect( H2Dialect.class ) -@JiraKey( "HHH-17943" ) -public class H2DialectTempTableNoCommitTest { - - @Test - public void noCommitAfterTempTableCreationTest(SessionFactoryScope scope) { - scope.inTransaction( - session -> { - // the following shouldn't commit anything to the DB - Doctor d = new Doctor(); - d.setId( 2L ); - session.persist( d ); - session.flush(); - session.createMutationQuery( "update Engineer set fellow = false where fellow = true" ).executeUpdate(); - session.getTransaction().markRollbackOnly(); - } - ); - scope.inTransaction( - session -> { - assertNull(session.find( Doctor.class, 2L )); - } - ); - } - - @Entity(name = "Person") - @Inheritance(strategy = InheritanceType.JOINED) - public static class Person { - - @Id - private Long id; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - } - - @Entity(name = "Engineer") - public static class Engineer extends Person { - - private boolean fellow; - - public boolean isFellow() { - return fellow; - } - - public void setFellow(boolean fellow) { - this.fellow = fellow; - } - } - - @Entity(name = "Doctor") - public static class Doctor { - @Id - private Long id; - private String name; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - -}