KODO: how set up fetch plan for bidirectional relationships?
Posted
by BestPractices
on Stack Overflow
See other posts from Stack Overflow
or by BestPractices
Published on 2010-04-14T21:37:32Z
Indexed on
2010/04/14
21:43 UTC
Read the original article
Hit count: 272
Running KODO 4.2 and having an issue inefficient queries being generated by KODO. This happens when fetching an object that contains a collection where that collection has a bidrectional relationship back to the first object.
Class Classroom
{
List<Student> _students;
}
Class Student
{
Classroom _classroom;
}
If we create a fetch plan to get a list of Classrooms and their corresponding Students by setting up the following fetch plan:
fetchPlan.addField(Classroom.class,”_students”);
This will result in two queries (get the classrooms and then get all students that are in those classrooms), which is what we would expect.
However, if we include the reference back to the classroom in our fetch plan in order for the _classroom field to get populated by doing fetchPlan.addField(Student.class, “_classroom”), this will result in X number of additional queries where X is the number of students in each classroom.
Can anyone explain how to fix this? KODO already has the original Classroom objects at the point that it's executing the queries to retrieve the Classroom objects and set them in each Student object's _classroom field. So I would expect KODO to simply set those objects in the _classroom field on each Student object accordingly and not go back to the database.
Once again, the documentation is sorely lacking with Kodo/JDO/OpenJPA but from what I've read it should be able to do this more efficiently.
Note-- EAGER_FETCH.PARALLEL is turned on and I have tried this with caching (query and data caches) turned on and off and there is no difference in the resultant queries.
© Stack Overflow or respective owner