We had a similar problem with the Hibernate Set was NOT initialized when it tried to use it and the size() method threw a NPE.
In our case (v2.1.3 but this is still an issue with v2.1.6) we were getting objects from a collection whose owner was loaded by query rather than by get or load. Once we added code to load the original item (to which the collection is attached) by key via get/load the exception went away. We did traced the problem to these lines of code in Loader.java where it can't figure out the owner of the collection in certain circumstances.
[code]
/**
* Read one collection element from the current row of the JDBC result set
*/
private void readCollectionElement(
final Object optionalOwner,
final Serializable optionalKey,
final ResultSet rs,
final SessionImplementor session)
throws HibernateException, SQLException {
final CollectionPersister collectionPersister = getCollectionPersister();
final Serializable collectionRowKey = (Serializable) collectionPersister.readKey(rs, session);
if (collectionRowKey!=null) {
if ( log.isDebugEnabled() ) log.debug( "found row of collection: " + MessageHelper.infoString(collectionPersister, collectionRowKey) );
Object owner = optionalOwner;
if (owner==null) {
owner = session.getCollectionOwner(collectionRowKey, collectionPersister);
if (owner==null) {
//TODO: This is assertion is disabled because there is a bug that means the
// original owner of a transient, uninitialized collection is not known
// if the collection is re-referenced by a different object associated
// with the current Session
//throw new AssertionFailure("bug loading unowned collection");
}
}
PersistentCollection rowCollection = session.getLoadingCollection(collectionPersister, collectionRowKey, rs);
if (rowCollection!=null) rowCollection.readFrom(rs, collectionPersister, owner);
}
else if (optionalKey!=null) {
if ( log.isDebugEnabled() ) log.debug( "result set contains (possibly empty) collection: " + MessageHelper.infoString(collectionPersister, optionalKey) );
session.getLoadingCollection(collectionPersister, optionalKey, rs); //handle empty collection
}
}
[/code]
_________________ \|/- Keith Mashinter, WCNA & AKME Solutions
|