Tuning Hibernate-created SQL queries via XML mapping file

As much as I detest the entire coding in XML paradigm, which still seems to be de-rigeur on most Spring/Hibernate projects, something about the annotations-style of meta-magic for both APIs has always bothered me slightly, even if I couldn't actually put my finger on what exactly it is.

Regardless, we came across an undocumented** feature of Hibernate in which you can control the structure of the SQL queries that it generates via the XML mapping file when dealing with foreign-key relationships. Take a simple example, where a KFCMeal contains a single SideDish and a single Drink:





























Now, if you try and query to find all KFCMeals which came with Fries and a Dr Pepper, the query generated would be something like:

SELECT * FROM KFCMeal, SideDish, Drink
WHERE KFCMeal.sideId = SideDish.id
AND KPCMeal.drinkId = Drink.id
AND SideDish.name = "Fries"
AND Drink.name = "Dr Pepper;

The structure of this query is directly generated by the ordering of the mapping, which places the SideDish before the Drink. You could reverse the join structure of the WHERE clause by swapping the SideDish and Drink mappings. Seems obvious, doesn't it?!? - but I'd always (naively) assumed that Hibernate would sprinkle some magic fairy dust on the query as it constructed it. In projects with complex querying requirements, you can use this effect to tune the queries for performance.

Now, to return to my original point of XML vs annotations - I'm wondering how this effect would be translated into an annotation-based mapping. When Hibernate loops over your classes to load the mappings, I'm assuming that it simply loads a list of the fields from the Class definition and that the ordering of this list could not simply determined by the ordering of the fields in the Java source file. And even if it was, you'd probably end up pepper-spraying the source with "whatever you do - don't reorder these bloody fields or it'll screw performance" comments. In the absence of a proper performance testing framework, the alternative would be to have a test which asserts the construction of the query by Hibernate, which probably wouldn't be particularly pretty without getting deep down and dirty with some JDBC code.

Suggestions welcomed here in the comments!

** I say undocumented, but to be honest I didn't go to too much investigative work beyond using the Googlebrain. Your mileage may vary.