• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Hibernate Criteria Query API / HQL Example

July 23, 2008 //  by Krishna Srinivasan//  Leave a Comment

Hibernate Criteria Example

Criteria APIs in the hibernate framework is useful for creating the dynamic query to execute.It is an alternative way to write the queries without using HQL. The queries are generated at runtime and excuted on the fly. Application developer need not worry about writing the query in hand, he/she just need to use APIs provided in the hibernate. It is one of the elegant way to write the queries in hibernate.

also read:

  • Introduction to Hibernate
  • Hibernate Interview Questions
  • Interceptors in Hibernate
  • Hibernate Books

The following example demonstrates very simple hibernate criteria example with retriving 10 results from the table. This is the statement used for creating the Criteria object :

	Criteria criteria = session.createCriteria(Student.class)

The above code returns the Criteria for Student class. Once Criteria is created, we can access methods in the API
to set the conditions. In our exampl we called

	criteria.setMaxResults(10);

It sets the conditionto retrieve only 10 results.

This is very basic example for the beginner, in the coming sections we will explain how to create the more
complex queries using the Hibernate Criteria API.

JavaBeatHibernateExample.java

package javabeat.net.hibernate;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * source : www.javabeat.net
 */
public class JavaBeatHibernateExample {

    public static void main(String args[]) {
        Configuration configuration = new Configuration();
        // configuring hibernate
        SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Student.class);
        criteria.setMaxResults(10);
        List<student> list = criteria.list();
        for (Student  student:list)
        {
            System.out.println(student.getName());
        }
    }
}

Student.java

package javabeat.net.hibernate;

/**
 * source : www.javabeat.net
 */
public class Student {
    private long id;
    private String  name;
    private String dept;
    private int marks;
    private String section;

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getMarks() {
        return marks;
    }

    public void setMarks(int marks) {
        this.marks = marks;
    }

    public String getName() {
        return name;
    }

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

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }
}

Student.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="javabeat.net.hibernate.Student" table="Student_Details">
        <id name="id" column="id" type="java.lang.Long">
            <generator class="increment"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>
        <property name="dept" column="dept" type="java.lang.String"/>
        <property name="marks" column="marks" type="java.lang.Integer"/>
        <property name="section" column="section" type="java.lang.String"/>
    </class>
</hibernate-mapping>

Category: HibernateTag: hibernate query

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Types of Managed Bean scopes in Spring Framework
Next Post: How to use datasource in Hibernate application? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact