From f7adc587b24f642513f3c592ff0b010a11ee9db8 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 2 Sep 2024 08:47:01 +0200 Subject: [PATCH] HHH-18553 add the test Signed-off-by: Gavin King --- .../deletedetached/DeleteDetachedTest.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/deletedetached/DeleteDetachedTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/deletedetached/DeleteDetachedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/deletedetached/DeleteDetachedTest.java new file mode 100644 index 0000000000..3ad05e8c4d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/deletedetached/DeleteDetachedTest.java @@ -0,0 +1,65 @@ +package org.hibernate.orm.test.deletedetached; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.OptimisticLockException; +import jakarta.persistence.Version; +import org.hibernate.StaleObjectStateException; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +@SessionFactory +@DomainModel(annotatedClasses = DeleteDetachedTest.Thing.class) +@JiraKey("HHH-18553") +public class DeleteDetachedTest { + @Test void testCanRemove(SessionFactoryScope scope) { + Thing thing = new Thing(); + thing.stuff = "Some stuff about the thing"; + scope.inTransaction(s -> s.persist(thing)); + scope.inTransaction(s -> { + Thing otherThing = s.find(Thing.class, thing.id); + assertNotNull(otherThing); + s.remove(thing); + assertFalse(s.contains(thing)); + assertFalse(s.contains(otherThing)); + }); + scope.inTransaction(s -> assertNull(s.find(Thing.class, thing.id))); + } + @Test void testCantRemove(SessionFactoryScope scope) { + Thing thing = new Thing(); + thing.stuff = "Some stuff about the thing"; + scope.inTransaction(s -> s.persist(thing)); + scope.inTransaction(s -> { + Thing otherThing = s.find(Thing.class, thing.id); + otherThing.stuff = "Other different stuff about the thing"; + assertNotNull(otherThing); + try { + s.remove(thing); + fail("Should have failed because detached object is stale"); + } + catch (OptimisticLockException exception) { + // expected + assertTrue( exception.getCause() instanceof StaleObjectStateException ); + } + assertFalse(s.contains(thing)); + assertTrue(s.contains(otherThing)); + }); + scope.inTransaction(s -> assertNotNull(s.find(Thing.class, thing.id))); + } + @Entity + static class Thing { + @GeneratedValue @Id long id; + @Version int version; + String stuff; + } +}