When an entity has a compound key made up of, in part, another entity's compound key, it seems that Hibernate does not bind parameters to the correct columns in SQL translated from HQL. It seems that the routine used to build the SQL statement via Antlr can list primary key columns in a different order than the routine that builds parameter values via reflection. Bascially, you end up with a condition like:
PK_COL_1 = ? and
PK_COL_2 = ?
and the bound values are for kCol2 and kCol1 respectively. Depending on how an HQL query is written, you can actually get the query translator to build a join this way:
TABLE1.PK_COL_1 = TABLE2.PK_COL_2 and
TABLE1.PK_COL_2 = TABLE2.PK_COL_1
Depending on the data type, this can either cause an error or an empty result set. I did a good bit of debugging, but did not find an easy solution that didn't break something else worse. I did find that explicitly setting @JoinColumn.referencedColumnName in the child ID class did force things to work correctly against Oracle but MySQL is syntactically different enough that it does not work there. I will attach an example shortly.
Hibernate 3.6.0, Windows, Oracle 10g and MySQL 5.1
Thanks for the test case!
This bug report does not indicate that the reported issue affects version 5.x. Versions prior to 5.x are no longer maintained. It would be a great help to the Hibernate team and community for someone to verify that the reported issue still affects version 5.x. If so, please add the 5.x version that you verified with to the list of affected-versions and attach the (preferably SSCCE) test case you used to do the verification to the report; from there the issues will be looked at during our triage meetings.
For details, see http://in.relation.to/2015/10/27/great-jira-cleanup-2015/
As part of verifying that this issue affects 5.0, please just set the "Affects version". Leave the "verify-affects-5.0" label and leave the issue in "Awaiting Response" status; these are critical for us to be able to track these verifications and triage them. Thanks.
I just tried out the first testcase but I couldn't reproduce it. I guess this is obsolete.
Thanks for verifying Christian