openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Dick <michael.d.d...@gmail.com>
Subject Re: NPE with Fetch Join Query
Date Wed, 30 Mar 2011 13:59:54 GMT
Hi Tobias,

Which version of OpenJPA are you using?

It looks like we don't have a cached version of the metadata for type B,
enabling preloading<http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_meta_repository>of
the meta data might at least work around the problem.

-mike

On Wed, Mar 30, 2011 at 5:22 AM, Tobias Trelle <tobias.trelle@codecentric.de
> wrote:

> Hi,
>
> I have a lazy loading 1:n association. I'd like to write a query to eager
> fetch that association on demand.
>
>
> @Entity
> @NamedQueries(@NamedQuery(name = "fetchB", query = "SELECT a from A a LEFT
> JOIN FETCH a.b WHERE a.id = :id"))
> public class A {
>
>    @Id
>    private long id;
>
>    private String name;
>
>    @OneToMany(fetch = FetchType.LAZY)
>    @JoinColumn(name = "FIRMA_ID")
>    private Collection b;
>    ...
>
> @Entity
> public class B {
>
>    @Id
>    private long id;
>
>    private String name;
>   ...
>
>
>
> When I execute the named query ...
>
>
> A a = em.createNamedQuery("fetchB", A.class).setParameter("id",
> 42).getSingleResult();
>
>
> ... the following SQL statement is executed against the database:
>
> SELECT t0.id, t0.name, t1.FIRMA_ID, t1.id, t1.name
>    FROM S.A t0 LEFT OUTER JOIN S.B t1 ON t0.id = t1.FIRMA_ID
>    WHERE (t0.id = ?) optimize for 1 row
>
> If I execute it with my favourite SQL tool, I get a result set of size 2.
>
> OpenJPA throws a NullpointerException:
>
>
>  org.apache.openjpa.persistence.PersistenceException: null
> FailedObject: SELECT a from A a LEFT JOIN FETCH a.b WHERE a.id = :id
> [java.lang.String]
>        at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:986)
>        at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:885)
>        at
>
> org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1030)
>        at
> org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:280)
>        at
>
> org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load(SelectImpl.java:2344)
>        at
> org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:274)
>        at
>
> org.apache.openjpa.jdbc.kernel.InstanceResultObjectProvider.getResultObject(InstanceResultObjectProvider.java:59)
>        at
> org.apache.openjpa.lib.rop.EagerResultList.(EagerResultList.java:36)
>        at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1246)
>        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1005)
>        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:861)
>        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:792)
>        at
> org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:542)
>        at
> org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:288)
>        at
> org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:302)
>        at
>
> org.apache.openjpa.persistence.QueryImpl.getSingleResult(QueryImpl.java:326)
>        at
> JpaFetchTestManual.testFindQuery(JpaFetchTestManual.java:58)
>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>        at
>
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>        at
>
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>        at java.lang.reflect.Method.invoke(Method.java:585)
>        at junit.framework.TestCase.runTest(TestCase.java:168)
>        at junit.framework.TestCase.runBare(TestCase.java:134)
>        at junit.framework.TestResult$1.protect(TestResult.java:110)
>        at junit.framework.TestResult.runProtected(TestResult.java:128)
>        at junit.framework.TestResult.run(TestResult.java:113)
>        at junit.framework.TestCase.run(TestCase.java:124)
>        at junit.framework.TestSuite.runTest(TestSuite.java:232)
>        at junit.framework.TestSuite.run(TestSuite.java:227)
>        at
>
> org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
>        at
>
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
>        at
>
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>        at
>
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
>        at
>
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
>        at
>
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
>        at
>
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
> Caused by: java.lang.NullPointerException
>        at
>
> org.apache.openjpa.jdbc.kernel.JDBCStoreManager.setInverseRelation(JDBCStoreManager.java:469)
>        at
>
> org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:429)
>        at
>
> org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:322)
>        at
>
> org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:112)
>        at
>
> org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)
>        at
> org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:1005)
>        at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:963)
>        ... 35 more
>
>
> What can be wrong with this simple example? Did I miss something?
>
> Cheers,
> Tobias
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/NPE-with-Fetch-Join-Query-tp6222526p6222526.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message