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