You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Ed Hillmann <ed...@gmail.com> on 2007/06/27 05:26:21 UTC

org.apache.openjpa.persistence.PersistenceException: null when retrieving persistent Collection

I have an item (CachedItem) that contains a persistent Collection (of
CachedItemReference objects), defined as follows:


    @OneToMany(mappedBy="cachedItem", cascade={CascadeType.PERSIST,
CascadeType.REMOVE})
    public Collection<CachedItemReference> getReferencedItems() {
        if (referencedItems == null) {
            referencedItems = new java.util.HashSet<CachedItemReference>();
        }
        return referencedItems;
    }

    public void setReferencedItems(final
Collection<CachedItemReference> references) {
        Collection<CachedItemReference> existingRefs = getReferencedItems();
        if (!(existingRefs.isEmpty())) {
            final Collection<CachedItemReference> clone
                    = new java.util.HashSet<CachedItemReference>(existingRefs);
            for (CachedItemReference reference : clone) {
                removeReference(reference);
            }
        }

        for (CachedItemReference reference : references) {
            addReference(reference);
        }
    }

    public void addReference(CachedItemReference reference) {
        getReferencedItems().add(reference);
        reference.setCachedItem(this);
    }

    public void removeReference(CachedItemReference reference) {
        getReferencedItems().remove(reference);
        reference.setCachedItem(null);
    }


In my application, I execute named search that attempts to retrieve
the CachedItem.  The named query is a simple one:

    @NamedQuery(name="findByUrl",
                query="select i from CachedItem i where
i.repository=:repository"
                + " and i.folder=:folder and i.name=:name"),

When I execute this, I'm getting a PersistenceException:

Exception in thread "main" <1.0.0-SNAPSHOT-SNAPSHOT nonfatal general
error> org.apache.openjpa.persistence.PersistenceException: null
        at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:857)
        at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:756)
        at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:776)
        at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:257)
        at org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load(SelectImpl.java:2174)
        at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:251)
        at org.apache.openjpa.jdbc.kernel.InstanceResultObjectProvider.getResultObject(InstanceResultObjectProvider.java:59)
        at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:36)
        at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1219)
        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:987)
        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:839)
        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:770)
        at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:525)
        at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:229)
        at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:269)
        at com.intecbilling.replib.cache.CacheService.findByUrl(CacheService.java:236)
        at com.intecbilling.replib.cache.Cache.findByUrl(Cache.java:499)
        at com.intecbilling.replib.CacheRepository.getInputStream(CacheRepository.java:389)
        at com.intecbilling.replib.RepositoryPerformance.measureGetInputStream(RepositoryPerformance.java:110)
        at com.intecbilling.replib.RepositoryPerformance.main(RepositoryPerformance.java:142)
Caused by: java.lang.NullPointerException
        at com.intecbilling.replib.cache.CachedItem.pcsetReferencedItems(CachedItem.java:388)
        at com.intecbilling.replib.cache.CachedItem.pcClearFields(CachedItem.java)
        at com.intecbilling.replib.cache.CachedItem.pcNewInstance(CachedItem.java)
        at org.apache.openjpa.enhance.PCRegistry.newInstance(PCRegistry.java:121)
        at org.apache.openjpa.kernel.StateManagerImpl.initialize(StateManagerImpl.java:250)
        at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:330)
        at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:255)
        at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:111)
        at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)
        at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:876)
        at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:834)
        ... 19 more

So, from what I can see, it's failing in the method
pcsetReferencedItems, which is the enhanced version of my method
setReferencedItems.  I noticed that line # 388 in the non-enhanced
method was the line:

            for (CachedItemReference reference : references) {

So, I changed the code block from

        for (CachedItemReference reference : references) {
            addReference(reference);
        }

to

        if (references != null) {
            for (CachedItemReference reference : references) {
                addReference(reference);
            }
        }

And this works without the Exception.  I get no exceptions at all (yay!).

Shouldn't the enhanced method be able to cope with the Java 1.5 for loop?

Thanks,
Ed

Re: org.apache.openjpa.persistence.PersistenceException: null when retrieving persistent Collection

Posted by Ed Hillmann <ed...@gmail.com>.
On 6/28/07, Marc Prud'hommeaux <mp...@apache.org> wrote:
> Ed-
>
> Well, the method needs to deal with the case where the collection (or
> any other persistent field) is null. It's no so much an issue with
> enhancement per-se, since enhanced or not, the code "for
> (CachedItemReference reference : references)" will throw an NPE is
> "references" is null. You could de-JDK15 it with "for (Iterator i =
> references.iterator(); i.hasNext(); )" and it would still throw an NPE.
>

That's fair.  I was under the incorrect assumption that Java coped
with a null pointer in the For loop syntax.  A quick test showed me I
was wrong. :)  I'll try to keep the white noise down to a minimum.
Sorry for the distraction. :)

Thanks,
Ed

Re: org.apache.openjpa.persistence.PersistenceException: null when retrieving persistent Collection

Posted by Marc Prud'hommeaux <mp...@apache.org>.
Ed-

Well, the method needs to deal with the case where the collection (or  
any other persistent field) is null. It's no so much an issue with  
enhancement per-se, since enhanced or not, the code "for  
(CachedItemReference reference : references)" will throw an NPE is  
"references" is null. You could de-JDK15 it with "for (Iterator i =  
references.iterator(); i.hasNext(); )" and it would still throw an NPE.



On Jun 26, 2007, at 8:26 PM, Ed Hillmann wrote:

> I have an item (CachedItem) that contains a persistent Collection (of
> CachedItemReference objects), defined as follows:
>
>
>    @OneToMany(mappedBy="cachedItem", cascade={CascadeType.PERSIST,
> CascadeType.REMOVE})
>    public Collection<CachedItemReference> getReferencedItems() {
>        if (referencedItems == null) {
>            referencedItems = new  
> java.util.HashSet<CachedItemReference>();
>        }
>        return referencedItems;
>    }
>
>    public void setReferencedItems(final
> Collection<CachedItemReference> references) {
>        Collection<CachedItemReference> existingRefs =  
> getReferencedItems();
>        if (!(existingRefs.isEmpty())) {
>            final Collection<CachedItemReference> clone
>                    = new java.util.HashSet<CachedItemReference> 
> (existingRefs);
>            for (CachedItemReference reference : clone) {
>                removeReference(reference);
>            }
>        }
>
>        for (CachedItemReference reference : references) {
>            addReference(reference);
>        }
>    }
>
>    public void addReference(CachedItemReference reference) {
>        getReferencedItems().add(reference);
>        reference.setCachedItem(this);
>    }
>
>    public void removeReference(CachedItemReference reference) {
>        getReferencedItems().remove(reference);
>        reference.setCachedItem(null);
>    }
>
>
> In my application, I execute named search that attempts to retrieve
> the CachedItem.  The named query is a simple one:
>
>    @NamedQuery(name="findByUrl",
>                query="select i from CachedItem i where
> i.repository=:repository"
>                + " and i.folder=:folder and i.name=:name"),
>
> When I execute this, I'm getting a PersistenceException:
>
> Exception in thread "main" <1.0.0-SNAPSHOT-SNAPSHOT nonfatal general
> error> org.apache.openjpa.persistence.PersistenceException: null
>        at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java: 
> 857)
>        at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java: 
> 756)
>        at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load 
> (JDBCStoreManager.java:776)
>        at org.apache.openjpa.jdbc.sql.AbstractResult.load 
> (AbstractResult.java:257)
>        at org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load 
> (SelectImpl.java:2174)
>        at org.apache.openjpa.jdbc.sql.AbstractResult.load 
> (AbstractResult.java:251)
>        at  
> org.apache.openjpa.jdbc.kernel.InstanceResultObjectProvider.getResultO 
> bject(InstanceResultObjectProvider.java:59)
>        at org.apache.openjpa.lib.rop.EagerResultList.<init> 
> (EagerResultList.java:36)
>        at org.apache.openjpa.kernel.QueryImpl.toResult 
> (QueryImpl.java:1219)
>        at org.apache.openjpa.kernel.QueryImpl.execute 
> (QueryImpl.java:987)
>        at org.apache.openjpa.kernel.QueryImpl.execute 
> (QueryImpl.java:839)
>        at org.apache.openjpa.kernel.QueryImpl.execute 
> (QueryImpl.java:770)
>        at org.apache.openjpa.kernel.DelegatingQuery.execute 
> (DelegatingQuery.java:525)
>        at org.apache.openjpa.persistence.QueryImpl.execute 
> (QueryImpl.java:229)
>        at org.apache.openjpa.persistence.QueryImpl.getResultList 
> (QueryImpl.java:269)
>        at com.intecbilling.replib.cache.CacheService.findByUrl 
> (CacheService.java:236)
>        at com.intecbilling.replib.cache.Cache.findByUrl(Cache.java: 
> 499)
>        at com.intecbilling.replib.CacheRepository.getInputStream 
> (CacheRepository.java:389)
>        at  
> com.intecbilling.replib.RepositoryPerformance.measureGetInputStream 
> (RepositoryPerformance.java:110)
>        at com.intecbilling.replib.RepositoryPerformance.main 
> (RepositoryPerformance.java:142)
> Caused by: java.lang.NullPointerException
>        at  
> com.intecbilling.replib.cache.CachedItem.pcsetReferencedItems 
> (CachedItem.java:388)
>        at com.intecbilling.replib.cache.CachedItem.pcClearFields 
> (CachedItem.java)
>        at com.intecbilling.replib.cache.CachedItem.pcNewInstance 
> (CachedItem.java)
>        at org.apache.openjpa.enhance.PCRegistry.newInstance 
> (PCRegistry.java:121)
>        at org.apache.openjpa.kernel.StateManagerImpl.initialize 
> (StateManagerImpl.java:250)
>        at  
> org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState 
> (JDBCStoreManager.java:330)
>        at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize 
> (JDBCStoreManager.java:255)
>        at  
> org.apache.openjpa.kernel.DelegatingStoreManager.initialize 
> (DelegatingStoreManager.java:111)
>        at org.apache.openjpa.kernel.ROPStoreManager.initialize 
> (ROPStoreManager.java:57)
>        at org.apache.openjpa.kernel.BrokerImpl.initialize 
> (BrokerImpl.java:876)
>        at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java: 
> 834)
>        ... 19 more
>
> So, from what I can see, it's failing in the method
> pcsetReferencedItems, which is the enhanced version of my method
> setReferencedItems.  I noticed that line # 388 in the non-enhanced
> method was the line:
>
>            for (CachedItemReference reference : references) {
>
> So, I changed the code block from
>
>        for (CachedItemReference reference : references) {
>            addReference(reference);
>        }
>
> to
>
>        if (references != null) {
>            for (CachedItemReference reference : references) {
>                addReference(reference);
>            }
>        }
>
> And this works without the Exception.  I get no exceptions at all  
> (yay!).
>
> Shouldn't the enhanced method be able to cope with the Java 1.5 for  
> loop?
>
> Thanks,
> Ed