From 9938fa14f79df5f364036e761443bae87b5b3a98 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Fri, 3 Jan 2025 14:16:07 +0100 Subject: [PATCH] HHH-4160 Add test to check JPA compliance for deleting detached entities --- .../DeleteDetachedJpaComplianceTest.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/deletedetached/DeleteDetachedJpaComplianceTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/deletedetached/DeleteDetachedJpaComplianceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/deletedetached/DeleteDetachedJpaComplianceTest.java new file mode 100644 index 000000000000..8efdedefdedc --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/deletedetached/DeleteDetachedJpaComplianceTest.java @@ -0,0 +1,99 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.deletedetached; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.IdClass; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@JiraKey("HHH-4160") +@Jpa(annotatedClasses = { + DeleteDetachedJpaComplianceTest.RestaurantWithCompositeKey.class, + DeleteDetachedJpaComplianceTest.Restaurant.class +}) +public class DeleteDetachedJpaComplianceTest { + @Test + void testComposite(EntityManagerFactoryScope scope) { + RestaurantWithCompositeKey restaurant = new RestaurantWithCompositeKey(); + restaurant.name = "Some stuff about the thing"; + scope.inTransaction( s -> s.persist( restaurant ) ); + scope.inTransaction( s -> { + RestaurantWithCompositeKey otherRestaurant = s.find( + RestaurantWithCompositeKey.class, + new RestaurantPK( restaurant.regionId, restaurant.restaurantId ) + ); + assertNotNull( otherRestaurant ); + assertThrows(IllegalArgumentException.class, + () -> s.remove( restaurant ), + "Given entity is not associated with the persistence context" + ); + } ); + scope.inTransaction( s -> { + assertNotNull( s.find( + RestaurantWithCompositeKey.class, + new RestaurantPK( restaurant.regionId, restaurant.restaurantId ) + ) ); + } ); + } + + @Test + void testRegular(EntityManagerFactoryScope scope) { + Restaurant restaurant = new Restaurant(); + restaurant.name = "Some stuff about the thing"; + scope.inTransaction( s -> s.persist( restaurant ) ); + scope.inTransaction( s -> { + Restaurant otherRestaurant = s.find( Restaurant.class, restaurant.restaurantId ); + assertNotNull( otherRestaurant ); + assertThrows(IllegalArgumentException.class, + () -> s.remove( restaurant ), + "Given entity is not associated with the persistence context" + ); + } ); + scope.inTransaction( s -> { + assertNotNull( s.find( Restaurant.class, restaurant.restaurantId ) ); + } ); + } + + @Entity + static class Restaurant { + @Id + @GeneratedValue + long restaurantId; + String name; + } + + @Entity + @IdClass(value = RestaurantPK.class) + static class RestaurantWithCompositeKey { + @Id + @GeneratedValue + long regionId; + @Id + @GeneratedValue + long restaurantId; + String name; + } + + static class RestaurantPK { + long regionId; + long restaurantId; + + public RestaurantPK() { + } + + public RestaurantPK(long regionId, long restaurantId) { + this.regionId = regionId; + this.restaurantId = restaurantId; + } + } +}