Dear Gavin, thank-you for your help and responses,
Here is what I have determined that I know happens when I specify a complex mapping of nested objects and try to specify Criteria on properties of the nested objects:
The first sql query generated is correct, it loads only what was outlined by the criteria to be loaded.
But every single subsequent generated sql query completely and utterly ignores anything that was entered in the Criteria and loads every single object on every single association all the way through the entire Hierarchy thereby completely and utterly defeating any reason for having specified a Criteria in the first place.
You say:
Quote:
Of course! This is expected functionality!
But why on earth is that expected? How is it helpful or useful to never be able to narrow down your results without disassociating every single object in the hierarchy and loading them all manually with repeated calls to the Criteria API?
You say that if
Quote:
If I do foo.getBars(), I should ALWAYS get ALL the Bars!
but what if I don't want all the Bars? What good is a Criteria API if the Criteria portion is circumvented further down the tree?
Quote:
If you want to retrieve only particular WorksheeProduct instances, start from the WorksheetProduct and navigate back -up- the bidirectional association. Then the retuned instances will be the instances of -WorksheetProduct- that match your Criteria.
I tried that, the moment I "back -up- " I end up with a parent that has all of the instances and not just the one I loaded. Now, in this scenario, I can see it becomes arguable as to what should be the case, was that one object loaded because it was the only one desired and so should it be the only one its parent reference contains? or was that the only reference point available and does the user really want to get everything else that the parent might have?
Regardless of the above, almost philosophical, question; whatever Criteria is specified as the restriction on what should be loaded at a certain point should be enforced from that point on down the hierarchy.
If the criteria is not enforced but used only loosely as a starting point for loading everything then it serves little purpose. It is like querying a database for only the records where 'x' = 1 but getting all the records where 'x' = 2..n anyway. No one would use a database where this occurred.
Quote:
Now, the kind of thing you want to do is possible in HQL, where you can return multiple objects in the SELECT clause, but will never be possible in the Criteria API
Why shouldn't it be possible with the Criteria API? Once again, what good is a Criteria API if the criteria portion is largely ignored or circumvented and the final results do not in any way, shape, or form, match what was asked for in the criteria?
If I wanted all records possible I would not specify any criteria.
This is effectively the same as "select * from tableA" vs. "select * from tableA where column1 = 5 and column2 = 7". If I wanted all the records I would use the first query, if only wanted records where column1=5 and column2=7 I would use the second. Logically if I want all the records through the Criteria API I would not specify any Criterion, if, however, I want only certain records I would .add(Expression.eq(...)) until I had narrowed down the list.
For the Criteria to function properly at the highest level but be ignored throughout the hierarchy thereafter is contradictory.
Quote:
P.S. There is a good argument that in the query above, we shouldn't be even fetching the WorksheetProducts, because we still need to hit the db again in order to fetch the collection. I havn't quite decided which way to go on this. Its certainly ok to prefetch objects in a <many-to-one>, and might even make good sense for a <many-to-many>, but probably inefficient for the case of <one-to-many>.
This is only true if nobody wants to narrow down to a subset of the <one-to-many> (which I do, and which you tell me I can't).
This to me is critical functionality, I switched from Toplink to Hibernate because I read in Hibernate's quick reference that it had a Criteria API and I trusted that I could use it as such as I had used similar functionality in Toplink. I have waited patiently through weeks of debugging and struggling to get Hibernate configured and operating as needed, reading and posting to this forum, waiting for bug fixes and/or functionality to be implemented, only to ultimately be told that it isn't supposed to work in a powerful and flexible way that is useful beyond a few simple scenarios. I think you guys have done a fantastic job with what you have made and your commitment to your user base so please don't think I'm bitter, I just wish it had been stated in the documentation that this functionality was not intended to be fully fledged like it is in other products.