Hibernate Criteria Query to find List of Domain class.

Syntax to get List of domain Object

list can be used on the Criteria object as shown below.

How to get a List of a Domain class using Hibernate's Criteria Query.

If we have a domain class Category as shown below.

package com.ekiras.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

public class Category {

 public Category(){}
 public Category(Long id){
  this.id = id;
 public Category(Long id, String name){
  this.id = id;
  this.name = name;
 @Column(nullable=false, name="id")
 private Long id;
 @Column(nullable=false, name="name")
 private String name;

       // Getters and Setters


We can get the list of all the categories using Criteria Query in the following ways

  • Simplest way to get a list of a Domain class (Category) in our example is
  • You can use pagination in the following way with list()
     public List<Category>; list(Integer offset){
      return sessionFactory.getCurrentSession()
  • Use this method as a generic method to get a list of all the records of a domain in the database.
     public List list(Class clazz){  
      return (List)getSession().createCriteria(clazz).list();
  • You can Order your list according to any property like
    public List<Category> list(Integer offset){
      return getSession()

No comments :

Post a Comment