Permalink
Browse files

HHH-6043 PostLoad method invoked before collection initialised

  • Loading branch information...
brmeyer
brmeyer committed Sep 26, 2012
1 parent 67beb08 commit 3e5184e6d7e7c9344880480802cf71d07a0f506a
@@ -280,19 +280,6 @@ private static void doInitializeEntity(
session
);
- if ( session.isEventSource() ) {
- postLoadEvent.setEntity( entity ).setId( id ).setPersister( persister );
-
- final EventListenerGroup<PostLoadEventListener> listenerGroup = session
- .getFactory()
- .getServiceRegistry()
- .getService( EventListenerRegistry.class )
- .getEventListenerGroup( EventType.POST_LOAD );
- for ( PostLoadEventListener listener : listenerGroup.listeners() ) {
- listener.onPostLoad( postLoadEvent );
- }
- }
-
if ( LOG.isDebugEnabled() ) {
LOG.debugf(
"Done materializing entity %s",
@@ -304,6 +291,45 @@ private static void doInitializeEntity(
factory.getStatisticsImplementor().loadEntity( persister.getEntityName() );
}
}
+
+ /**
+ * PostLoad cannot occur during initializeEntity, as that call occurs *before*
+ * the Set collections are added to the persistence context by Loader.
+ * Without the split, LazyInitializationExceptions can occur in the Entity's
+ * postLoad if it acts upon the collection.
+ *
+ *
+ * HHH-6043
+ *
+ * @param entity
+ * @param session
+ * @param postLoadEvent
+ */
+ public static void postLoad(
+ final Object entity,
+ final SessionImplementor session,
+ final PostLoadEvent postLoadEvent) {
+
+ if ( session.isEventSource() ) {
+ final PersistenceContext persistenceContext
+ = session.getPersistenceContext();
+ final EntityEntry entityEntry = persistenceContext.getEntry(entity);
+ final Serializable id = entityEntry.getId();
+
+ postLoadEvent.setEntity( entity ).setId( entityEntry.getId() )
+ .setPersister( entityEntry.getPersister() );
+
+ final EventListenerGroup<PostLoadEventListener> listenerGroup
+ = session
+ .getFactory()
+ .getServiceRegistry()
+ .getService( EventListenerRegistry.class )
+ .getEventListenerGroup( EventType.POST_LOAD );
+ for ( PostLoadEventListener listener : listenerGroup.listeners() ) {
+ listener.onPostLoad( postLoadEvent );
+ }
+ }
+ }
private static boolean useMinimalPuts(SessionImplementor session, EntityEntry entityEntry) {
return ( session.getFactory().getSettings().isMinimalPutsEnabled() &&
@@ -1037,7 +1037,16 @@ private void initializeEntitiesAndCollections(
}
}
}
-
+
+ // Until this entire method is refactored w/ polymorphism, postLoad was
+ // split off from initializeEntity. It *must* occur after
+ // endCollectionLoad to ensure the collection is in the
+ // persistence context.
+ if ( hydratedObjects!=null ) {
+ for ( Object hydratedObject : hydratedObjects ) {
+ TwoPhaseLoad.postLoad( hydratedObject, session, post );
+ }
+ }
}
private void endCollectionLoad(
@@ -35,7 +35,6 @@ public void setId(Integer id) {
}
public void setChildren(Set<Child> children) {
this.children = children;
- //children.size(); // This will result in the same exception as the PostLoad method...
}
@PostLoad
@@ -8,7 +8,6 @@
import javax.persistence.EntityManager;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
-import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
@@ -20,7 +19,6 @@
* access the association.
*/
@Test
- @FailureExpected( jiraKey="HHH-6043" )
public void testAccessAssociatedSetInPostLoad() {
Child child = new Child();
child.setId(1);

0 comments on commit 3e5184e

Please sign in to comment.