In Hibernate when querying by natural id with query cache we receive a NullPointerException

Solution Verified - Updated -

Issue

  • In our Parent object , we have collection called members, while adding a parent object, we have not faced any issue. While fetching the object and during collection intialiazation, we are getting following exception.

    private Object getObject( String key ) {
              Session session = HibernateUtil.getSessionFactory().getCurrentSession();
              session.beginTransaction();
              Criteria criteria = session.createCriteria( test.Parent.class );
              criteria.add( Restrictions.naturalId().set( "name", key ) );// No I18N
              criteria.setCacheable( true );
              Object obj = criteria.uniqueResult();
              Parent p=(Parent)obj;
              p.getMembers().size();
              session.getTransaction().commit();
              return obj;
    }
    
    
    
    EXCEPTION : java.lang.NullPointerException
    java.lang.NullPointerException
         at org.hibernate.engine.loading.CollectionLoadContext.addCollectionToCache(CollectionLoadContext.java:301)
         at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:259)
         at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:222)
         at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:195)
         at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:880)
         at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:865)
         at org.hibernate.loader.Loader.doQuery(Loader.java:729)
         at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
         at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
         at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
         at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
         at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:63)
         at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1720)
         at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:347)
         at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
         at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:110)
         at org.hibernate.collection.PersistentSet.size(PersistentSet.java:139)
         at test.TestManagerNew.getObject(TestManagerNew.java:69)
         at test.TestManagerNew.createAndStoreEvent(TestManagerNew.java:37)
         at test.TestManagerNew.main(TestManagerNew.java:23)
    

Environment

  • JBoss Enterprise Application Platform (EAP) 5.1
  • Hibernate 3.2

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content