java 有没有一种可移植的方式来获得“SELECT FIRST 10 * FROM T”语义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3400589/
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
Is there a portable way to have "SELECT FIRST 10 * FROM T" semantic?
提问by stacker
I want to read data in blocks of say 10k records from a database.
我想从数据库中读取 10k 条记录块中的数据。
I found Result limitson wikipedia and it seems obvious that this can't done with sql in a portable way.
我在 wikipedia 上发现了Result 限制,这似乎不能以可移植的方式用 sql 完成。
Another approach could be JdbcTemplatewhich offers many methods for queries, but how could I decide that enough rows have been read. Through the callbacks like RowMapper and ResultSetExtractor it can't be indicated, that enough data has been read.
另一种方法可能是JdbcTemplate,它提供了许多查询方法,但我如何确定已读取足够的行。通过像 RowMapper 和 ResultSetExtractor 这样的回调,不能表明已经读取了足够的数据。
EDIT: I was looking for a solution for JdbcTemplate This postsuggests to use setMaxRowswhich I had overlooked.
编辑:我正在寻找 JdbcTemplate 的解决方案这篇文章建议使用我忽略的setMaxRows。
回答by BalusC
Grab Hibernateor JPA. Both are familiar with various database dialects and will handle the nasty DB specifics under the hoods transparently.
抓住Hibernate或JPA。两者都熟悉各种数据库方言,并将在幕后透明地处理令人讨厌的数据库细节。
In Hibernate you can paginate using Criteria#setFirstResult()and Criteria#setMaxResults(). E.g.
在 Hibernate 中,您可以使用Criteria#setFirstResult()和进行分页Criteria#setMaxResults()。例如
List users = session.createCriteria(User.class)
.addOrder(Order.asc("id"))
.setFirstResult(0) // Index of first row to be retrieved.
.setMaxResults(10) // Amount of rows to be retrieved.
.list();
In JPA you can do similar using Query#setFirstResult()and Query#setMaxResults().
在 JPA 中,您可以使用Query#setFirstResult()和做类似的事情Query#setMaxResults()。
List users = em.createQuery("SELECT u FROM User u ORDER BY u.id");
.setFirstResult(0) // Index of first row to be retrieved.
.setMaxResults(10) // Amount of rows to be retrieved.
.getResultList();
回答by OMG Ponies
There is an ANSI standard syntax from SQL:2008:
SQL:2008 中有一个 ANSI 标准语法:
SELECT t.*
FROM TABLE t
FETCH FIRST 10 ROWS ONLY
...but it's not supported on most databases at this time.
...但目前大多数数据库不支持它。
回答by Sebastián Grignoli
There is no portable way of doing that on plain SQL, because different SQL Engines use different syntaxes for that.
在普通 SQL 上没有可移植的方式来做到这一点,因为不同的 SQL 引擎为此使用不同的语法。
Use a Database Abstraction Layer, or DBAL.
使用数据库抽象层或 DBAL。
http://en.wikipedia.org/wiki/Database_abstraction_layer
http://en.wikipedia.org/wiki/Database_abstraction_layer
回答by nos
If you want a portable way, you need to move up an abstraction layer, as there's no portable SQL way(not one that databases actually implement anyways) - and use ORM mappers like e.g hibernate.
如果您想要一种可移植的方式,则需要向上移动一个抽象层,因为没有可移植的 SQL 方式(无论如何数据库实际上都没有实现) - 并使用 ORM 映射器,例如 hibernate。
If you do need raw JDBC, you'll have to write specific SQL for eache specific database - which is often the case anyway as writing 100% portabl SQL is pretty hard in all but the trivial cases.
如果您确实需要原始 JDBC,则必须为每个特定的数据库编写特定的 SQL - 无论如何,这种情况通常是这样,因为编写 100% 可移植 SQL 在所有情况下都非常困难,除了微不足道的情况。
The last resort is to run the query without any restrictions and just iterate over the 10 first results you get back - though this doesn't leverage the database capabilities and would be quite bad if your query results in manyrows.
最后的方法是在没有任何限制的情况下运行查询,并只迭代您返回的前 10 个结果 - 尽管这不会利用数据库功能,如果您的查询结果是多行,这将是非常糟糕的。
回答by Thorbj?rn Ravn Andersen
No. Thats why database abstraction layers like Hibernate contains SQL dialectswhere you choose the one to use with your database.
不。这就是为什么像 Hibernate 这样的数据库抽象层包含 SQL方言的原因,您可以在其中选择一种与数据库一起使用的方言。

