|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.events; |
| 6 | + |
| 7 | + |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.ManyToMany; |
| 11 | +import org.hibernate.HibernateException; |
| 12 | +import org.hibernate.event.spi.EventType; |
| 13 | + |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.Jira; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.UUID; |
| 23 | + |
| 24 | +import static jakarta.persistence.FetchType.LAZY; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Felix König |
| 29 | + * @author Jan Schatteman |
| 30 | + */ |
| 31 | +@DomainModel ( |
| 32 | + annotatedClasses = { |
| 33 | + PreDeleteEventListenerTest.Parent.class, PreDeleteEventListenerTest.Child.class |
| 34 | + } |
| 35 | +) |
| 36 | +@SessionFactory |
| 37 | +@Jira( value = "https://hibernate.atlassian.net/browse/HHH-19631" ) |
| 38 | +public class PreDeleteEventListenerTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + void testAccessUninitializedCollectionInListener(SessionFactoryScope scope) { |
| 42 | + |
| 43 | + scope.getSessionFactory().getEventListenerRegistry().appendListeners( EventType.PRE_DELETE, |
| 44 | + event -> { |
| 45 | + Parent parent = ((Parent) event.getEntity()); |
| 46 | + // dummy access |
| 47 | + assertThrows( HibernateException.class, () -> parent.getChildren().size() ); |
| 48 | + return false; |
| 49 | + }, event -> { |
| 50 | + Parent parent = ((Parent) event.getEntity()); |
| 51 | + // dummy access |
| 52 | + assertThrows( HibernateException.class, () -> parent.getChildren().contains(new Child()) ); |
| 53 | + return false; |
| 54 | + } ); |
| 55 | + |
| 56 | + scope.inTransaction( session -> session.persist(new Parent()) ); |
| 57 | + |
| 58 | + scope.inTransaction(session -> { |
| 59 | + var parent = session.createSelectionQuery("select p from Parent p", Parent.class).getSingleResult(); |
| 60 | + // triggers pre-delete event |
| 61 | + session.remove(parent); |
| 62 | + session.flush(); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + @Entity(name= "Parent") |
| 67 | + public static class Parent { |
| 68 | + @Id String id = UUID.randomUUID().toString(); |
| 69 | + @ManyToMany(fetch = LAZY) |
| 70 | + Set<Child> children = new HashSet<>(); |
| 71 | + public Set<Child> getChildren() { |
| 72 | + return children; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Entity(name= "Child") |
| 77 | + public static class Child { |
| 78 | + @Id |
| 79 | + String childId = UUID.randomUUID().toString(); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments