Skip to content

Commit 3d60bfa

Browse files
committed
HHH-4160 Add test to check JPA compliance for deleting detached entities
1 parent cfe4346 commit 3d60bfa

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.deletedetached;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.GeneratedValue;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.IdClass;
11+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
12+
import org.hibernate.testing.orm.junit.JiraKey;
13+
import org.hibernate.testing.orm.junit.Jpa;
14+
import org.junit.jupiter.api.Test;
15+
16+
import static org.junit.jupiter.api.Assertions.assertNotNull;
17+
import static org.junit.jupiter.api.Assertions.assertThrows;
18+
19+
@JiraKey("HHH-4160")
20+
@Jpa(annotatedClasses = {
21+
DeleteDetachedJpaComplianceTest.RestaurantWithCompositeKey.class,
22+
DeleteDetachedJpaComplianceTest.Restaurant.class
23+
})
24+
public class DeleteDetachedJpaComplianceTest {
25+
@Test
26+
void testComposite(EntityManagerFactoryScope scope) {
27+
RestaurantWithCompositeKey restaurant = new RestaurantWithCompositeKey();
28+
restaurant.name = "Some stuff about the thing";
29+
scope.inTransaction( s -> s.persist( restaurant ) );
30+
scope.inTransaction( s -> {
31+
RestaurantWithCompositeKey otherRestaurant = s.find(
32+
RestaurantWithCompositeKey.class,
33+
new RestaurantPK( restaurant.regionId, restaurant.restaurantId )
34+
);
35+
assertNotNull( otherRestaurant );
36+
assertThrows(IllegalArgumentException.class,
37+
() -> s.remove( restaurant ),
38+
"Given entity is not associated with the persistence context"
39+
);
40+
} );
41+
scope.inTransaction( s -> {
42+
assertNotNull( s.find(
43+
RestaurantWithCompositeKey.class,
44+
new RestaurantPK( restaurant.regionId, restaurant.restaurantId )
45+
) );
46+
} );
47+
}
48+
49+
@Test
50+
void testRegular(EntityManagerFactoryScope scope) {
51+
Restaurant restaurant = new Restaurant();
52+
restaurant.name = "Some stuff about the thing";
53+
scope.inTransaction( s -> s.persist( restaurant ) );
54+
scope.inTransaction( s -> {
55+
Restaurant otherRestaurant = s.find( Restaurant.class, restaurant.restaurantId );
56+
assertNotNull( otherRestaurant );
57+
assertThrows(IllegalArgumentException.class,
58+
() -> s.remove( restaurant ),
59+
"Given entity is not associated with the persistence context"
60+
);
61+
} );
62+
scope.inTransaction( s -> {
63+
assertNotNull( s.find( Restaurant.class, restaurant.restaurantId ) );
64+
} );
65+
}
66+
67+
@Entity
68+
static class Restaurant {
69+
@Id
70+
@GeneratedValue
71+
long restaurantId;
72+
String name;
73+
}
74+
75+
@Entity
76+
@IdClass(value = RestaurantPK.class)
77+
static class RestaurantWithCompositeKey {
78+
@Id
79+
@GeneratedValue
80+
long regionId;
81+
@Id
82+
@GeneratedValue
83+
long restaurantId;
84+
String name;
85+
}
86+
87+
static class RestaurantPK {
88+
long regionId;
89+
long restaurantId;
90+
91+
public RestaurantPK() {
92+
}
93+
94+
public RestaurantPK(long regionId, long restaurantId) {
95+
this.regionId = regionId;
96+
this.restaurantId = restaurantId;
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)