hql 查询中表达式的别名

发布于 2024-11-30 06:58:39 字数 603 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吲‖鸣 2024-12-07 06:58:39

这是一个老问题,所以我希望您现在已经找到答案,但如果您还没有,我已经学到了一些关于 nhibernate 如何处理列别名的新知识。这就是我给你的答案。

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_ ) " +
         "        ( col_0_2_ + col_0_3_ ) " + 
         "        FROM Student AS aa ";

您确实通过注意到 NH 生成它自己的列别名来回答了这个问题。我的研究告诉我,这只出现在 ORDER BY 语句中,这是他们正在解决的一个错误。所以说实话,我认为你可以在这里使用自己的列别名

sqlcmd = " SELECT  aa.course.code, " +
         "        (CASE WHEN aa.gender = 'M' THEN 1 ELSE 0 END) myCol1,  " +
         "        (CASE WHEN aa.gender = 'F' THEN 1 ELSE 0 END) myCol2,  " +
         "        ( myCol1 + myCol2 ) myCol3 " +
         "        ( myCol2 + myCol3 ) myCol4 " + 
         "        FROM Student AS aa ";

,但无论哪种情况,你都可以继续使用 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.

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_ ) " +
         "        ( col_0_2_ + col_0_3_ ) " + 
         "        FROM Student AS aa ";

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

sqlcmd = " SELECT  aa.course.code, " +
         "        (CASE WHEN aa.gender = 'M' THEN 1 ELSE 0 END) myCol1,  " +
         "        (CASE WHEN aa.gender = 'F' THEN 1 ELSE 0 END) myCol2,  " +
         "        ( myCol1 + myCol2 ) myCol3 " +
         "        ( myCol2 + myCol3 ) myCol4 " + 
         "        FROM Student AS aa ";

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文