|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.stateless.events; |
| 6 | + |
| 7 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 8 | +import org.hibernate.event.service.spi.EventListenerRegistry; |
| 9 | +import org.hibernate.event.spi.EventType; |
| 10 | +import org.hibernate.event.spi.PostCollectionRecreateEvent; |
| 11 | +import org.hibernate.event.spi.PostCollectionRecreateEventListener; |
| 12 | +import org.hibernate.event.spi.PostCollectionRemoveEvent; |
| 13 | +import org.hibernate.event.spi.PostCollectionRemoveEventListener; |
| 14 | +import org.hibernate.event.spi.PreCollectionRecreateEvent; |
| 15 | +import org.hibernate.event.spi.PreCollectionRecreateEventListener; |
| 16 | +import org.hibernate.event.spi.PreCollectionRemoveEvent; |
| 17 | +import org.hibernate.event.spi.PreCollectionRemoveEventListener; |
| 18 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 19 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | + |
| 25 | +@DomainModel(annotatedClasses = {EntityA.class, EntityB.class}) |
| 26 | +@SessionFactory |
| 27 | +class CollectionListenerInStatelessSessionTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void statelessInsert(SessionFactoryScope scope) { |
| 31 | + EventListenerRegistry registry = |
| 32 | + scope.getSessionFactory().unwrap(SessionFactoryImplementor.class) |
| 33 | + .getServiceRegistry().getService(EventListenerRegistry.class); |
| 34 | + var preRecreate = new MyPreCollectionRecreateEventListener(); |
| 35 | + var preRemove = new MyPreCollectionRemoveEventListener(); |
| 36 | + var postRecreate = new MyPostCollectionRecreateEventListener(); |
| 37 | + var postRemove = new MyPostCollectionRemoveEventListener(); |
| 38 | + registry.getEventListenerGroup(EventType.PRE_COLLECTION_RECREATE) |
| 39 | + .appendListener( preRecreate ); |
| 40 | + registry.getEventListenerGroup(EventType.PRE_COLLECTION_REMOVE) |
| 41 | + .appendListener( preRemove ); |
| 42 | + registry.getEventListenerGroup(EventType.POST_COLLECTION_RECREATE) |
| 43 | + .appendListener( postRecreate ); |
| 44 | + registry.getEventListenerGroup(EventType.POST_COLLECTION_REMOVE) |
| 45 | + .appendListener( postRemove ); |
| 46 | + |
| 47 | + scope.inStatelessTransaction(statelessSession -> { |
| 48 | + EntityA a = new EntityA(); |
| 49 | + EntityB b = new EntityB(); |
| 50 | + a.children.add(b); |
| 51 | + statelessSession.insert( b ); |
| 52 | + statelessSession.insert( a ); |
| 53 | + statelessSession.delete( a ); |
| 54 | + statelessSession.delete( b ); |
| 55 | + }); |
| 56 | + |
| 57 | + assertEquals(1, preRecreate.called); |
| 58 | + assertEquals(1, preRemove.called); |
| 59 | + assertEquals(1, postRecreate.called); |
| 60 | + assertEquals(1, postRemove.called); |
| 61 | + } |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +class MyPreCollectionRecreateEventListener implements PreCollectionRecreateEventListener { |
| 66 | + |
| 67 | + int called = 0; |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onPreRecreateCollection(PreCollectionRecreateEvent event) { |
| 71 | + called++; |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +class MyPreCollectionRemoveEventListener implements PreCollectionRemoveEventListener { |
| 77 | + |
| 78 | + int called = 0; |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onPreRemoveCollection(PreCollectionRemoveEvent event) { |
| 82 | + called++; |
| 83 | + } |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +class MyPostCollectionRecreateEventListener implements PostCollectionRecreateEventListener { |
| 88 | + |
| 89 | + int called = 0; |
| 90 | + |
| 91 | + @Override |
| 92 | + public void onPostRecreateCollection(PostCollectionRecreateEvent event) { |
| 93 | + called++; |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +class MyPostCollectionRemoveEventListener implements PostCollectionRemoveEventListener { |
| 99 | + |
| 100 | + int called = 0; |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onPostRemoveCollection(PostCollectionRemoveEvent event) { |
| 104 | + called++; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments