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.
Attachments
2
13 Apr 2014, 11:58 PM
29 Sep 2011, 09:57 PM
Activity
Steve EbersoleOctober 29, 2015 at 2:11 PM
Thanks for verifying Christian
Christian BeikovOctober 29, 2015 at 1:45 PM
I just tried out the first testcase but I couldn't reproduce it. I guess this is obsolete.
Steve EbersoleOctober 28, 2015 at 3:24 AM
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.
Steve EbersoleOctober 27, 2015 at 7:17 PM
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.
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.