Initialize PersistentCollection with fetchType LAZY returns NPE

Description

@JoinTable(name = "JOIN_TABLE", joinColumns = { @JoinColumn(name = "JOIN_TABLE_ID", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ID") })
@ManyToMany(fetch = FetchType.LAZY)
private Set<JoinTable> aProperty;

When fetching this entity, and after initialize, I get all the valid attribute values but the aProperty is defined as PersistenceCollection.

This collection is not intialized yet. When I initialize this collection as follow:

(PersistentCollection) entity).forceInitialization();

Or

Hibernate.initialize(entity);

I got NullPointerException:

Caused by: java.lang.NullPointerException
at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:790)
at org.hibernate.event.spi.AbstractCollectionEvent.getLoadedOwnerOrNull(AbstractCollectionEvent.java:75)
at org.hibernate.event.spi.InitializeCollectionEvent.<init>(InitializeCollectionEvent.java:36)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1803)
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:489)
at org.hibernate.Hibernate.initialize(Hibernate.java:77)

When I check source code of class org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:790)

CollectionEntry ce = getCollectionEntry( collection );
790 if ( ce.getLoadedPersister() == null ) {
791
return null; // early exit...
792 }

Looks like the CollectionEntry ce is null. This collection entry is fetched from collectionEntries.get(coll);

// Identity map of CollectionEntry instances, by the collection wrapper
121 private IdentityMap<PersistentCollection, CollectionEntry> collectionEntries;

I checked Jboss docs, it mentioned the way to initilialize the collection is as I have done above.
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-fetching-initialization

For now, I have to set the fetchType to EAGER for my assetVersions which is not what I want to do due to performance impact.

Environment

None

Activity

Show:
Brett Meyer
October 26, 2012, 4:27 PM

, I'm a little confused by the code example. Is 'entity' the collection, or aProperty's parent entity?

Normally, I'd expect something like this:

Foo entity = session.get(Foo.class, id);
Hibernate.initialize(entity.getAProperty());
or
((PersistentCollection) entity.getAProperty()).forceInitialization();

Can the customer give any more code examples? A full test-case would be really helpful. Thanks!

Brett Meyer
October 26, 2012, 4:27 PM

Also, is the session still open when initialize is called?

Ricardo Martinelli de Oliveira
October 26, 2012, 5:38 PM

> Also, is the session still open when initialize is called?

Yes, they confirmed that the session is still open.

Brett Meyer
November 1, 2012, 8:45 PM

, I went through some of the code and test cases. Attached (hhh7629.zip) is a very simplified test, taken and modified from the customer's source. Hhh7629Test.java is the main test runner.

The test passes for me, provided that getAssetVersions() is called within an open session and transaction. I debugged and stepped through it and confirmed that getAssetVersions() passes DmHelper a PersistentBag (a PersistentCollection). The call to forceInitialization() works as expected.

Can you have the customer take a look at the test runner and see what may be different within his sequences? The actual app is huge and I'm not sure exactly what context this is being used.

Also mention that his DmHelper essentially duplicates Hibernate#initialize – he may just want to use that.

Brett Meyer
November 8, 2012, 5:47 PM

Rejecting. The problem was due to the customer attempting to share PersistenceContexts between multiple threads.

Rejected

Assignee

Brett Meyer

Reporter

Ricardo Martinelli de Oliveira

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major