如何使用 HQL 返回 rownum 列值?(使用 Oracle 数据库)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/533376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to return rownum column value with HQL? (using oracle db)
提问by jedierikb
I have a complicated HQL query.
我有一个复杂的 HQL 查询。
I would like to access the Oracle specific rownumcolumn value as part of my returned results. How do I write my query (and/or change my hbm.xml) to support this?
我想访问 Oracle 特定的rownum列值作为返回结果的一部分。我如何编写我的查询(和/或更改我的 hbm.xml)以支持这一点?
What I have tried so far does not work:
到目前为止我尝试过的方法不起作用:
modifying my hbm.xml
修改我的 hbm.xml
<property name="rownum" type="int" update="false" insert="false" generated="never"/>
and a query such as:
和一个查询,例如:
"select dog.rownum from Dog as dog where ..."
But I get java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification
但我得到java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification
I suspect I might need to mix some sql with my complex hql query... suggestions welcome. Thanks.
我怀疑我可能需要将一些 sql 与我复杂的 hql 查询混合在一起……欢迎提出建议。谢谢。
Some background:
一些背景:
- http://ronfrancis.wordpress.com/2007/10/16/dont-forget-an-order-by-for-pagination-queries/
- hibernate uses oracle's rownum 'under the hood' to paginate results.
- http://ronfrancis.wordpress.com/2007/10/16/dont-forget-an-order-by-for-pagination-queries/
- hibernate 使用 oracle 的 rownum 'under the hood' 对结果进行分页。
采纳答案by Tony Andrews
ROWNUM doesn't belong to any table, so your query should be:
ROWNUM 不属于任何表,所以你的查询应该是:
"select rownum from Dog as dog where ..."
For example:
例如:
SQL> select emp.ename, rownum from emp;
ENAME ROWNUM
---------- ----------
SMITH 1
ALLEN 2
WARD 3
JONES 4
MARTIN 5
BLAKE 6
CLARK 7
SCOTT 8
KING 9
TURNER 10
回答by Brian Deterling
If you really want to do it in the mapping, you might try to define it as a formula instead of a column. Hibernate may not pre-pend the table name if it doesn't recognize it as a column.
如果您真的想在映射中执行此操作,您可以尝试将其定义为公式而不是列。如果 Hibernate 无法将其识别为列,则它可能不会预先添加表名。