|
| 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.Basic; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.GeneratedValue; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import org.hibernate.PropertyValueException; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 20 | + |
| 21 | +@SessionFactory |
| 22 | +@DomainModel(annotatedClasses = DeleteDetachedOptionalityViolationTest.Thing.class) |
| 23 | +@JiraKey("HHH-2792") |
| 24 | +public class DeleteDetachedOptionalityViolationTest { |
| 25 | + @Test |
| 26 | + void testRemoveDetachedWithNull(SessionFactoryScope scope) { |
| 27 | + Thing thing = new Thing(); |
| 28 | + thing.stuff = "Some stuff about the thing"; |
| 29 | + scope.inTransaction( s -> s.persist( thing ) ); |
| 30 | + scope.inTransaction( s -> { |
| 31 | + Thing detachedThing = new Thing(); |
| 32 | + detachedThing.id = thing.id; |
| 33 | + assertThrows( PropertyValueException.class, |
| 34 | + () -> s.remove( detachedThing ), |
| 35 | + "not-null property references a null or transient value: " + Thing.class.getName() + ".stuff" |
| 36 | + ); |
| 37 | + } ); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testRemoveDetachedProxy(SessionFactoryScope scope) { |
| 42 | + Thing thing = new Thing(); |
| 43 | + thing.stuff = "Some stuff about the thing"; |
| 44 | + scope.inTransaction( s -> s.persist( thing ) ); |
| 45 | + Thing detachedThing = scope.fromTransaction( s -> s.getReference( Thing.class, thing.id ) ); |
| 46 | + scope.inTransaction( s -> { |
| 47 | + s.remove( detachedThing ); |
| 48 | + } ); |
| 49 | + scope.inTransaction( s -> assertNull( s.find( Thing.class, thing.id ) ) ); |
| 50 | + } |
| 51 | + |
| 52 | + @Entity |
| 53 | + static class Thing { |
| 54 | + @GeneratedValue |
| 55 | + @Id |
| 56 | + long id; |
| 57 | + @Basic(optional = false) |
| 58 | + String stuff; |
| 59 | + } |
| 60 | +} |
0 commit comments