From 2bbeeb5ab2b42cec5868967f4ebfde478f6f51f2 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 11 Jan 2022 12:53:23 +0100 Subject: [PATCH] Fix postLoad callback method calls --- .../entity/AbstractEntityInitializer.java | 2 - .../listener/MultiListenersAppliedTest.java | 119 ++++++++++++++++++ .../callback/listener/PersonListener.java | 18 +++ .../jpa/compliance/callback/listener/orm.xml | 18 +++ 4 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/MultiListenersAppliedTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/PersonListener.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/orm.xml diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java index 3bea815fc3..2e9650351c 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java @@ -433,10 +433,8 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces } else { final Object existingEntity = persistenceContext.getEntity( entityKey ); - if ( existingEntity != null ) { this.entityInstance = existingEntity; - registerLoadingEntity( rowProcessingState, entityInstance ); } else if ( this instanceof EntityResultInitializer && entityInstanceFromExecutionContext != null ) { this.entityInstance = entityInstanceFromExecutionContext; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/MultiListenersAppliedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/MultiListenersAppliedTest.java new file mode 100644 index 0000000000..8e3d681aee --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/MultiListenersAppliedTest.java @@ -0,0 +1,119 @@ +package org.hibernate.orm.test.jpa.compliance.callback.listener; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.Id; +import jakarta.persistence.PostLoad; +import jakarta.persistence.Query; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Jpa( + annotatedClasses = MultiListenersAppliedTest.Person.class, + xmlMappings = "org/hibernate/orm/test/jpa/compliance/callback/listener/orm.xml" +) +public class MultiListenersAppliedTest { + + @Test + public void postLoadMultiTest(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + Person person = new Person( 1, "Fab" ); + entityManager.persist( person ); + entityManager.flush(); + entityManager.refresh( person ); + final Query query = entityManager + .createQuery( "select p from Person p" ); + query.getResultList(); + + final List actual = person.getPostLoadCalls(); + + assertThat( actual.size(), is( 3 ) ); + assertTrue( + actual.contains( "AnotherPersonListener" ), + "AnotherPersonListener#postLoad has not been called" + ); + assertTrue( + actual.contains( "LastPersonListener" ), + "LastPersonListener#postLoad has not been called" + ); + assertTrue( + actual.contains( "PersonListener" ), + "PersonListener#postLoad has not been called" + ); + } + ); + } + + public static class PersonCallback { + + boolean isPostPersistCalled; + + private List postLoadCalls = new ArrayList(); + + public void setPostPersistCalled() { + this.isPostPersistCalled = true; + } + + public boolean isPostLoadCalled() { + return isPostPersistCalled; + } + + public List getPostLoadCalls() { + return postLoadCalls; + } + + public void addPostLoadCall(String name) { + postLoadCalls.add( name ); + } + } + + @Entity(name = "Person") + @EntityListeners({ AnotherPersonListener.class, LastPersonListener.class }) + public static class Person extends PersonCallback { + + Person() { + } + + public Person(Integer id, String name) { + this.id = id; + this.name = name; + } + + @Id + private Integer id; + + private String name; + } + + public static class AnotherPersonListener { + + @PostLoad + protected void postLoad(PersonCallback callback) { + callback.addPostLoadCall( "AnotherPersonListener" ); + + callback.setPostPersistCalled(); + } + + } + + public static class LastPersonListener { + + @PostLoad + protected void postLoad(PersonCallback callback) { + callback.addPostLoadCall( "LastPersonListener" ); + callback.setPostPersistCalled(); + } + + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/PersonListener.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/PersonListener.java new file mode 100644 index 0000000000..e8369ed0dc --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/PersonListener.java @@ -0,0 +1,18 @@ +/* + * 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.jpa.compliance.callback.listener; + +import jakarta.persistence.PostLoad; + +public class PersonListener { + + @PostLoad + protected void postLoad(MultiListenersAppliedTest.PersonCallback callback) { + callback.addPostLoadCall("PersonListener" ); + callback.setPostPersistCalled(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/orm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/orm.xml new file mode 100644 index 0000000000..858d1f91ee --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/callback/listener/orm.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + +