openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <>
Subject Re: Fetch Plan ands collections mapped
Date Wed, 21 Jul 2010 23:52:10 GMT

As long as you are inside a transaction, the fields are fetched as you access
them -- irrespective of whether they are in the fetch plan or not. Let us
take a concrete example.
Consider an entity X with three fields id and a,b.c (whether they are
single- or multi-valued is not relevant in this context). 

  1: em.getTransaction().begin();
  2: FetchPlan plan = em.getFetchPlan();
  3: plan.addField("a");
  4: plan.addField("b");
  5: X x = em.find(X.class, 1234);
     // The line above will cause OpenJPA to issue a SQL and fetch field id,
a and b.
     // So at this point X will have field id=1234, and field a and b
populated while c == null. 
  6: C c = x.getC();
     // This will cause OpenJPA to issue a separate SQL query to fetch the
value of C.
     // Because the call is made inside an active transaction.
     // even if c was a basic field of say type int or String
  7: em.getTransaction().commit();
  8: em.close()
   Had the the transaction been  committed after line 5, the instance x will
become "unmanaged".
The state of x after the transaction has ended will only contain value for
id,a,b but not c. If x.getC() is called, it will return a null -- because at
that point there is no connection to database anymore to fetch the data for


View this message in context:
Sent from the OpenJPA Users mailing list archive at

View raw message