From b3cbd9a10a95a980cf0e5a3e8a45849cae3cc791 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 27 Mar 2023 13:28:26 +0200 Subject: [PATCH] HHH-16350 Add test for issue --- .../orm/test/loading/PreLoadEventTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/loading/PreLoadEventTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/loading/PreLoadEventTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/loading/PreLoadEventTest.java new file mode 100644 index 0000000000..5f19083b2a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/loading/PreLoadEventTest.java @@ -0,0 +1,62 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.loading; + +import org.hibernate.event.spi.EventType; +import org.hibernate.event.spi.PreLoadEvent; +import org.hibernate.event.spi.PreLoadEventListener; + +import org.hibernate.testing.orm.domain.gambit.BasicEntity; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Marco Belladelli + */ +@SessionFactory +@DomainModel( annotatedClasses = BasicEntity.class ) +@Jira( "https://hibernate.atlassian.net/browse/HHH-16350" ) +public class PreLoadEventTest { + @BeforeAll + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> session.persist( new BasicEntity( 1, "test" ) ) ); + scope.getSessionFactory().getEventEngine().getListenerRegistry().appendListeners( + EventType.PRE_LOAD, + new AssertingPreLoadEventListener() + ); + } + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery( "delete from BasicEntity" ).executeUpdate() ); + } + + @Test + public void testPreLoad(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final BasicEntity be = session.createQuery( "from BasicEntity", BasicEntity.class ).getSingleResult(); + assertThat( be.getData() ).isEqualTo( "test" ); + } ); + } + + public static class AssertingPreLoadEventListener implements PreLoadEventListener { + @Override + public void onPreLoad(PreLoadEvent event) { + event.getPersister().getValues( event.getEntity() ); + assertThat( event.getState() ).isNotNull(); + assertThat( event.getState() ).hasSize( 1 ); + assertThat( event.getState()[0] ).isEqualTo( "test" ); + } + } +}