{"id":597,"date":"2004-05-21T10:08:04","date_gmt":"2004-05-21T14:08:04","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=597"},"modified":"2004-05-21T10:08:04","modified_gmt":"2004-05-21T14:08:04","slug":"hibernate-returning-n-records-in-a-random-order","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/05\/21\/hibernate-returning-n-records-in-a-random-order\/","title":{"rendered":"Hibernate: Returning N records in a random order"},"content":{"rendered":"<p>Hibernate: Returning N records in a random order<\/p>\n<p>I was pleasantly surprised by Hibernate yesterday when I tried need to have a web page show a listing of 3 randomly chosen articles.  Using SQL Server, you&#8217;d write a query like this:<br \/>\n<code>SELECT top 3 * FROM mytable ORDER BY newid()<\/code><br \/>\nThe <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/acdata\/ac_8_qd_04_13ec.asp\">&#8216;top n&#8217;<\/a> syntax is specific to SQL Server, which makes it non portable.  I saw that the Query class has a method that allows you to restrict the number of rows that are returned from a query:<br \/>\n<code><br \/>\nString query = \"SELECT * FROM mytable ORDER BY newid()\";<br \/>\nQuery q = session.createQuery(query);<br \/>\nq.setMaxResults(3);<br \/>\n<\/code><br \/>\nbut I thought that would be inefficient because Hibernate would have to retrieve a large resultset and then only return the first n rows.  Turns out that Hibernate actually figures out that SQL Server is the underlying platform and <a href=\"http:\/\/cvs.sourceforge.net\/viewcvs.py\/*checkout*\/hibernate\/Hibernate2\/src\/net\/sf\/hibernate\/dialect\/Attic\/SQLServerDialect.java?content-type=text%2Fplain&amp;rev=1.1.2.2.2.5\">uses the &#8216;TOP&#8217; keyword<\/a> [look at the getLimitString() method].  Hibernate has made what used to be proprietary SQL Server sql, portable across any database platform that Hibernate supports. Case in point &#8212; if you use the same method <code>q.setMaxResults(3)<\/code> when using MySQL, Hibernate will automatically use the MySQLDialect and substitute the <a href=\"http:\/\/cvs.sourceforge.net\/viewcvs.py\/*checkout*\/hibernate\/Hibernate2\/src\/net\/sf\/hibernate\/dialect\/MySQLDialect.java?content-type=text%2Fplain&amp;rev=1.11.2.10.2.8\">limit<\/a> keyword that <a href=\"http:\/\/dev.mysql.com\/doc\/mysql\/en\/LIMIT_optimisation.html\">MySQL uses<\/a>.  <\/p>\n<p>Of course, Hibernate can&#8217;t do everything&#8230; yet.  The second part of my query ordered the results in a random fashion.  You can do that in SQL Server using the newid() function; MySQL accomplishes the same thing with the rand() function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hibernate: Returning N records in a random order I was pleasantly surprised by Hibernate yesterday when I tried need to have a web page show a listing of 3 randomly chosen articles. Using SQL Server, you&#8217;d write a query like this: SELECT top 3 * FROM mytable ORDER BY newid() The &#8216;top n&#8217; syntax is &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/05\/21\/hibernate-returning-n-records-in-a-random-order\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hibernate: Returning N records in a random order<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/597"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=597"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/597\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}