Hi there, Michael Vorburger wrote:
1. Is the "default" fetch group ALWAYS active? I was trying to load e.g. only one field from an Entity in a query, but noticed that even if I do fp.resetFetchGroups(); and fp.clearFields(); and then a fp.addField(MyEntity.class, "name"); STILL always includes all columns which I assume are in the Default Fetch Group (all Basic fields, and all direct relations). This took me a moment to figure out, because as I read the Doc in chapter 7, I was assuming that I can entirely control this through the API.. is this intended? To really control exactly what column is in a SELECT fine granuarly under some circumstances, is it fair to say (and may be add to the doc?) that you have to annotate EVERY field as @Basic(fetch=FetchType.LAZY) or @ManyToOne(cascade = CascadeType.PERSIST, fetch=FetchType.LAZY) ? If under other circumstances one then wants the standard fetching behaviour, do you need to create your own myDefaultFetchGroup which explicitly includes all Basic fields + and all direct relations? Seems a bit cumbersome, am I getting something wrong?
To my knowledge, if you haven't activated a specific fetch group, then the default always applies. The JPA spec itself, not OpenJPA, dictates default fetch behavior for different relationships. For instance, all @*ToOne relationships are by default EAGER and all @*ToMany relationships are LAZY. Someone please correct me if I'm wrong.
2. http://openjpa.apache.org/docs/latest/manual/manual.html#ref_guide_fetch _impl says that "Even when a direct relation is not eagerly fetched, OpenJPA selects the foreign key columns and caches the values. This way when you do traverse the relation, OpenJPA can often find the related object in its cache, or at least avoid joins when loading the related object from the database." - that's very kind, but may be we have an Entity with lot's of foreign key columns and I KNOW I only want a list showing a few attributes, and I KNOW that the direct relations won't be traversed (say because the Entites are detached, may be). Is there any way to configure OpenJPA to NOT eagerly fetch direct relations, unless they are explicitly part of the Fetch Group? Actually what I'd want is to be able to by default not include foreign key columns, but have a way to explicitly include them - which is not the same as having the field as part of the FetchGroup (that will lead to a JOIN, I may just want to control whether or not to selects the foreign key column).
I believe there is a way to configure this default behavior in OpenJPA, but not sure how.
3. When I do FetchPlan.addField() for a @ManyToMany or a @PersistentCollection(elementEmbedded = true) field, there is no eager fetching (like when the same field has FetchType.EAGER)... Should this work? Can somebody point to any examples or test cases for this? Do we need to fiddle with the EagerFetchMode join/parallel??
I assume that even with OpenJPA's elementEmbedded extension, the default fetch behavior for @*ToMany is lazy.
4. Caching: How do FetchGroups and L2 (EMF not EM) Caching relate to each other in OpenJPA? If you do a query with an active FetchGroup that excludes some fields, with active Data Cache, and later another query on the same Entity with another FetchGroup (some columns may be used in both, others not), is the cache a) never used (in other words, query caching uses the Fetch Group as cache key), b) only the "missing" columns are fetched and "merged" ?
Great question.. I'd love to hear the answer :) Andy
