OpenJPA by default fetches multi-valued relations lazily. It helps to improve
performance by minimizing database joins unless user application requires to
bring the multi-valued relation in memory. 

Your program will still work if you perform the query within a transaction.
i.e. in your main program add
em.getTransaction().begin() before em.createNamedQuery().
As the current program invokes the query without an active transaction, the
resultant instances that the calling program receives are 'detached' and
hence can not load their unloaded multi-valued collection any more. But if
the query were executed within a transaction, then
module.getDWPageCollection() will make OpenJPA fetch the collection from the
database. 

 


Łukasz Budnik wrote:
> 
> Hi All!
> 
> I have a problem while migrating from Oracle TopLink to OpenJPA.
> 
> I have two very simple tables one to many: one dw_modules can have
> many dw_pages.
> 
> Those tables are very simple:
> dw_modules has a primary key: "id"
> dw_pages has a primary key: "id" and a foreign key "parentmodule_id"
> 
> I have automatically generated class entities:
> 
> DWModule with
> 
> @OneToMany(mappedBy = "parentmoduleId")
>     private Collection<DWPage> dWPageCollection;
> 
> and DWPage with:
> 
> @JoinColumn(name = "PARENTMODULE_ID", referencedColumnName = "ID")
>     @ManyToOne
>     private DWModule parentmoduleId;
> 
> 
> Also, I have a standalone application to test it.
> 
> public static void main(String[] args) {
>         EntityManager em = emf.createEntityManager();
>         DWModule module = (DWModule)
> em.createNamedQuery("DWModule.findById").setParameter("id",
> 3).getSingleResult();
>         System.out.println(module.getName());
>         for (DWPage page: module.getDWPageCollection()) {
>             System.out.println("\t- " + page.getName());
>         }
>     }
> 
> First of all I have tested my current solution (Oracle TopLink), the
> result is as predicted, everything is OK:
> 
> Search
>         - BasicPatientSearch
>         - ExaminationByDiagnosisSearch
>         - ExaminationByDescriptionSearch
> 
> Now, I have changed to OpenJPA provider and run the program again, the
> result is:
> 
> Search
> Exception in thread "main" java.lang.NullPointerException
>         at jpatests.Runner.main(Runner.java:37)
> 
> Search is the name of the module
> 
> BUT I get a null pointer on this line:
> 
> for (DWPage page: module.getDWPageCollection()) {
> 
> so I changed:
> 
> @OneToMany(mappedBy = "parentmoduleId", fetch= FetchType.LAZY)
>     private Collection<DWPage> dWPageCollection;
> 
> nope, still null pointer, once again:
> 
> @OneToMany(mappedBy = "parentmoduleId", fetch= FetchType.EAGER)
>     private Collection<DWPage> dWPageCollection;
> 
> yes, this time DWPage collection is not null and the result as expected.
> 
> I have over 50 tables (and entities) with probably more than 50
> collections...
> do I have to change ALL classes?
> 
> or is there a way to make OpenJPA fetch all collections like Oracle
> TopLink (at least at the LAZY level).
> 
> best regards
> Łukasz
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Oracle-TopLink-and-OpenJPA-different-behaviour-tp14777235p14808091.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to