| The following NullPointerException occurs when iterating on a List of object loaded by Hibernate:
java.lang.NullPointerException
at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:788)
at org.hibernate.event.spi.AbstractCollectionEvent.getLoadedOwnerOrNull(AbstractCollectionEvent.java:58)
at org.hibernate.event.spi.InitializeCollectionEvent.<init>(InitializeCollectionEvent.java:22)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2183)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:565)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:247)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:132)
at org.hibernate.collection.internal.PersistentList.iterator(PersistentList.java:131)
at com.vf.tech.cimes.model.attribute.AttributeContainer.setAttributes(AttributeContainer.java:90)
My field declaration is:
@OneToMany(cascade=ALL, fetch=LAZY)
@OrderColumn(name="order_id")
public List<AttributeInstance> getAttributes() {
return attributes;
}
The setter method in which the NPE occurs:
public void setAttributes(List<AttributeInstance> a) {
for (AttributeInstance inst : attributes) {
inst.removePropertyChangeListener(attributeListener);
}
List<AttributeInstance> old = attributes;
attributes = a;
if (attributes != null) {
for (AttributeInstance inst : attributes) { inst.addPropertyChangeListener(attributeListener);
}
}
firePropertyChangeEvent(ATTRIBUTES_PROPERTY, old, attributes);
}
NPE occurs during a call on the Session.merge method:
Session session = getSession();
AttributeContainer tmpContainer = (AttributeContainer)session.merge(this);
The getSession() method is a utility method which create a new Session from the SessionFactory. After taking a look at the org.hibernate.engine.internal.StatefulPersistenceContext code, it seems that the problem comes from:
@Override
public Object getLoadedCollectionOwnerOrNull(PersistentCollection collection) {
final CollectionEntry ce = getCollectionEntry( collection );
if ( ce.getLoadedPersister() == null ) { return null;
}
The CollectionEntry ce returned by getCollectionEntry is null. |