hql 查询中表达式的别名
我的 Hibernate 版本是 3.2.6.ga
谷歌搜索发现很多人都遇到了同样的问题 Hibernate HQL 不能很好地处理别名。显然HQL只让 您为表中存在的列添加别名。此外,HQL 还生成自己的 查询中所有列的别名,其形式为 列_x_y_ 但我不知道这些是如何产生的。
对于我的情况,我想将两个派生列添加到第三个派生列中。 在本机 SQL 中微不足道,在 HQL 中却出人意料地困难。
我设计的简化示例:
sqlcmd = " SELECT aa.course.code, " +
" (CASE WHEN aa.gender = 'M' THEN 1 ELSE 0 END), " +
" (CASE WHEN aa.gender = 'F' THEN 1 ELSE 0 END), " +
" ( col_0_1_ + col_0_2_ ) " +
" FROM Student AS aa ";
如何将第二列和第三列添加在一起以形成 HQL 中的第四列?
TIA,
仍在学习的史蒂夫
my Hibernate version is 3.2.6.ga
Googling around reveals many people are having the same problems with
Hibernate HQL not handling aliases very well. Apparently HQL only lets
you alias a column that exists in a table. Also, HQL generates its own
aliases for all columns in the query and these have the form
col_x_y_
but how these are generated I don't know.
For my case I want to add two derived columns into a third derived column.
Trivial in native SQL, surprisingly difficult in HQL.
My contrived, simplified example:
sqlcmd = " SELECT aa.course.code, " +
" (CASE WHEN aa.gender = 'M' THEN 1 ELSE 0 END), " +
" (CASE WHEN aa.gender = 'F' THEN 1 ELSE 0 END), " +
" ( col_0_1_ + col_0_2_ ) " +
" FROM Student AS aa ";
How can I add the 2nd and 3rd columns together to form a 4th column in HQL?
TIA,
Still-learning Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个老问题,所以我希望您现在已经找到答案,但如果您还没有,我已经学到了一些关于 nhibernate 如何处理列别名的新知识。这就是我给你的答案。
您确实通过注意到 NH 生成它自己的列别名来回答了这个问题。我的研究告诉我,这只出现在 ORDER BY 语句中,这是他们正在解决的一个错误。所以说实话,我认为你可以在这里使用自己的列别名
,但无论哪种情况,你都可以继续使用 NH 向你抛出的别名,它将采用 col_x_y_ 的形式,
希望这有帮助,祝你好运
This is an old question, so I hope you've found your answer by now, but if you haven't, I've learned a few new things about how nhibernate handles column alias's. So here's my answer for you.
you really answered this yourself by noting that NH generates it's own column alias names. My research has told me that this only is in ORDER BY statements and it's a bug they're working on. So honestly I think you're okay with using your own column alias's here
but in either case you can keep using the alias's that NH throws out at you and it will be in the form of col_x_y_
hope this helps, good luck