<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on"><b>Order by</b> clause is used to retrieve the data from database in the <b>sorted order by</b> any property of returned class or components. HQL supports <b>Order By </b>Clause. In our example we will retrieve the <b>data sorted</b> on the <b>userName</b> type. Here is the java example code:</p>
<p>&#8220;FROM UserDetails user ORDER BY user.userName DESC&#8221; ;</p>
</div>
<p><b>UserDetails.java</b></p>
<pre class="highlight" name="code">package com.sdnext.hibernate.tutorial.dto; 
 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 
 
@Entity 
@Table(name="USERS") 
public class UserDetails 
{ 
 @Id 
 @Column(name="USER_ID") 
 @GeneratedValue(strategy=GenerationType.AUTO) 
 private int userId; 
 
 @Column(name="USER_NAME") 
 private String userName; 
 
 public int getUserId() { 
 return userId; 
 } 
 public void setUserId(int userId) { 
 this.userId = userId; 
 } 
 public String getUserName() { 
 return userName; 
 } 
 public void setUserName(String userName) { 
 this.userName = userName; 
 } 
} 
</pre>
<div align='center' id="ads-id"></div>
<p><b>hibernate.cfg.xml</b></p>
<pre class="highlight" name="code"><;!!DOCTYPE hibernate-configuration PUBLIC 
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">; 
 
<;hibernate-configuration>; 
 <;session-factory>; 
 <;!-- Database connection settings -->; 
 <;property name="connection.driver_class">;com.mysql.jdbc.Driver<;/property>; 
 <;property name="connection.url">;jdbc:mysql://localhost:3306/hibernateDB<;/property>; 
 <;property name="connection.username">;root<;/property>; 
 <;property name="connection.password">;root<;/property>; 
 
 <;!-- JDBC connection pool (use the built-in) -->; 
 <;property name="connection.pool_size">;1<;/property>; 
 
 <;!-- SQL dialect -->; 
 <;property name="dialect">;org.hibernate.dialect.MySQLDialect<;/property>; 
 
 <;!-- Enable Hibernate's automatic session context management -->; 
 <;property name="current_session_context_class">;thread<;/property>; 
 
 <;!-- Disable the second-level cache -->; 
 <;property name="cache.provider_class">;org.hibernate.cache.NoCacheProvider<;/property>; 
 
 <;!-- Echo all executed SQL to stdout -->; 
 <;property name="show_sql">;true<;/property>; 
 
 <;!-- Drop and re-create the database schema on startup -->; 
 <;property name="hbm2ddl.auto">;update<;/property>; 
 
 <;mapping class="com.sdnext.hibernate.tutorial.dto.UserDetails">; 
 
 <;/mapping>;<;/session-factory>; 
 <;/hibernate-configuration>; 
</pre>
<p><b>HibernateTestDemo.java</b></p>
<pre class="highlight" name="code">package com.sdnext.hibernate.tutorial; 
 
import java.util.Iterator; 
 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.AnnotationConfiguration; 
 
import com.sdnext.hibernate.tutorial.dto.UserDetails; 
 
public class HibernateTestDemo { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) 
 { 
 SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
 Session session = sessionFactory.openSession(); 
 session.beginTransaction(); 
 
 String SQL_QUERY = "FROM UserDetails user ORDER BY user.userName DESC"; 
 Query query = session.createQuery(SQL_QUERY); 
 
 for(Iterator it=query.iterate();it.hasNext();) 
 { 
 UserDetails user = (UserDetails)it.next(); 
 System.out.println("User Name: " + user.getUserName()); 
 System.out.println("User Id: " + user.getUserId()); 
 } 
 session.getTransaction().commit(); 
 session.close(); 
 } 
} 
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<div style="color: red;">log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).</div>
<div style="color: red;">log4j:WARN Please initialize the log4j system properly.</div>
<p>Hibernate: select userdetail0_.USER_ID as col_0_0_ from USERS userdetail0_ order by userdetail0_.USER_NAME DESC<br />
Hibernate: select userdetail0_.USER_ID as USER1_0_0_, userdetail0_.USER_NAME as USER2_0_0_ from USERS userdetail0_ where userdetail0_.USER_ID=?<br />
User Name: Sweety Rajput<br />
User Id: 2<br />
Hibernate: select userdetail0_.USER_ID as USER1_0_0_, userdetail0_.USER_NAME as USER2_0_0_ from USERS userdetail0_ where userdetail0_.USER_ID=?<br />
User Name: Dinesh Rajput<br />
User Id: 1<br />
Hibernate: select userdetail0_.USER_ID as USER1_0_0_, userdetail0_.USER_NAME as USER2_0_0_ from USERS userdetail0_ where userdetail0_.USER_ID=?<br />
User Name: Dev Rajput<br />
User Id: 3<br />
Hibernate: select userdetail0_.USER_ID as USER1_0_0_, userdetail0_.USER_NAME as USER2_0_0_ from USERS userdetail0_ where userdetail0_.USER_ID=?<br />
User Name: Anamika Rajput<br />
User Id: 4</div>
<div class="separator" style="clear: both; text-align: center;"><img border="0" height="204" src="https://dineshonjava.com/wp-content/uploads/2012/05/orderby.jpg" width="640" /></div>
<p>
</div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/hql-where-clause-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/hql-group-by-clause-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '689'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…