Close

ORDER BY ASC/DESC Clause Example

[Updated: Mar 30, 2018, Created: Mar 30, 2018]

The ORDER BY clause orders the objects returned by the query.

The ASC keyword (the default) specifies ascending order, and the DESC keyword indicates descending order.

Example

The Entity

@Entity
public class Employee {
  @Id
  @GeneratedValue
  private long id;
  private String name;
  private int salary;
  private String dept;
    .............
}

Using Query

public class ExampleMain {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          persistEmployees();
          findEmployees();
      } finally {
          entityManagerFactory.close();
      }
  }

  public static void persistEmployees() {
      Employee employee1 = Employee.create("Diana", "IT", 3000);
      Employee employee2 = Employee.create("Rose", "Admin", 4000);
      Employee employee3 = Employee.create("Denise", "IT", 1500);
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      em.persist(employee1);
      em.persist(employee2);
      em.persist(employee3);
      em.getTransaction().commit();
      em.close();
  }

  private static void findEmployees() {
      EntityManager em = entityManagerFactory.createEntityManager();
      Query query = em.createQuery("SELECT e FROM Employee e ORDER BY e.salary");
      List<Employee> resultList = query.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }
}
Employee{id=3, name='Denise', salary=1500, dept='IT'}
Employee{id=1, name='Diana', salary=3000, dept='IT'}
Employee{id=2, name='Rose', salary=4000, dept='Admin'}

DESC Example

public class ExampleMain2 {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          persistEmployees();
          findEmployees();
      } finally {
          entityManagerFactory.close();
      }
  }

  public static void persistEmployees() {
      Employee employee1 = Employee.create("Diana", "IT", 3000);
      Employee employee2 = Employee.create("Rose", "Admin", 4000);
      Employee employee3 = Employee.create("Denise", "IT", 1500);
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      em.persist(employee1);
      em.persist(employee2);
      em.persist(employee3);
      em.getTransaction().commit();
      em.close();
  }

  private static void findEmployees() {
      EntityManager em = entityManagerFactory.createEntityManager();
      Query query = em.createQuery("SELECT e FROM Employee e ORDER BY e.salary DESC");
      List<Employee> resultList = query.getResultList();
      resultList.forEach(System.out::println);
      em.close();
  }
}
Employee{id=2, name='Rose', salary=4000, dept='Admin'}
Employee{id=1, name='Diana', salary=3000, dept='IT'}
Employee{id=3, name='Denise', salary=1500, dept='IT'}

Example Project

Dependencies and Technologies Used:

  • h2 1.4.197: H2 Database Engine.
  • hibernate-core 5.2.13.Final: The core O/RM functionality as provided by Hibernate.
  • JDK 1.8
  • Maven 3.3.9

ORDER BY ASC/DESC Clause Example Select All Download
  • order-by-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF

See Also