HSQL Order By Ascending Order : HSQL « Hibernate « Java Tutorial

File: Main.java

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
        .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");

    Session session = hibernateUtil.getSession();

    Survey survey = new Survey();
    survey.setPurchaseDate(new Date());


    survey = new Survey();
    survey.setPurchaseDate(new Date());    
    String hql = "from Survey p order by p.price asc";
    Query query = session.createQuery(hql);
    List results = query.list();
    hibernateUtil.checkData("select * from survey");

File: Survey.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN" 

    <class name="Survey" table="SURVEY" dynamic-update="false" dynamic-insert="false">
        <cache usage="read-write"/>
    <id name="Id" type="int" unsaved-value="0">
      <column name="ID" sql-type="int" not-null="true"/> 
      <generator class="increment"></generator>
        <property name="name" type="java.lang.String" update="true" insert="true" access="property" column="name"/>
        <property name="purchaseDate" type="date" column="purchasedate" /> 

File: Survey.java

import java.util.Date;

public class Survey {
  private int id;

  private String name;

  private Date purchaseDate;

  public Date getPurchaseDate() {
    return purchaseDate;

  public void setPurchaseDate(Date purchaseDate) {
    this.purchaseDate = purchaseDate;

  public String getName() {
    return name;

  public void setName(String name) {
    this.name = name;

  public int getId() {
    return id;

  public void setId(int id) {
    this.id = id;

File: hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:data/tutorial</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>

        <property name="hibernate.cache.use_second_level_cache">false</property>
        <property name="hibernate.cache.use_query_cache">false</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>
        <!-- Mapping files -->
        <mapping resource="Survey.hbm.xml"/>

File: HibernateUtil.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
  Session session;
  Statement st;
  public HibernateUtil() throws Exception{
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();

    // Load the JDBC driver.
    System.out.println("Driver Loaded.");
    // Establish the connection to the database.
    String url = "jdbc:hsqldb:data/tutorial";

    Connection conn = DriverManager.getConnection(url, "sa", "");
    System.out.println("Got Connection.");
    st = conn.createStatement();
  public Session getSession(){
    return session;
  public void executeSQLCommand(String sql) throws Exception {

  public void checkData(String sql) throws Exception {
    ResultSet rs = st.executeQuery(sql);
    ResultSetMetaData metadata = rs.getMetaData();

    for (int i = 0; i < metadata.getColumnCount(); i++) {
      System.out.print("\t"+ metadata.getColumnLabel(i + 1)); 

    while (rs.next()) {
      for (int i = 0; i < metadata.getColumnCount(); i++) {
        Object value = rs.getObject(i + 1);
        if (value == null) {
          System.out.print("\t       ");
        } else {
  Download:  HibernateHSQLOrderByAscendingOrder.zip( 4,861 k)

21.11.1.Query a Class Name (a table)
21.11.2.Query on a field in a class
21.11.3.HSQL Where Clause And Condition
21.11.4.HSQL Table Alias
21.11.5.HSQL Reference Object Type Field
21.11.6.HSQL Order By Ascending Order
21.11.7.HSQL Order By clause
21.11.8.HSQL Named Query
21.11.9.HSQL: Set Max Result
21.11.10.HSQL Join Two Classes
21.11.11.HSQL Inner Join Class And Its Field
21.11.12.HSQL pageable result: First, Max Result
21.11.13.HSQL Fetch Associations
21.11.14.HSQL Count function