Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Advanced Methods in Hibernate Criteria Query (HCQ)

In this article we will discuss about advanced methods which may be used in Hibernate Criteria Query (HCQ).

Introduction

We will discuss different criteria which can be added to make a successful query in different conditions.

Let’s discuss simple examples to start understanding this.

I am assuming that you have read my older post so I am not rewriting the hibernate configuration file, utility class.

There is some change in course class. Below are the records which are within this table.

We will make a class Course and we will perform operation on this class using the Hibernate Criteria Query.

Listing 1: Course class

package model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 *
 * @author Anurag
 */
@Entity
public class Course implements Serializable {

    @Id
    @GeneratedValue
    @Column(name = "C_ID")
    private Long id;
    private String name;
    private int duration;
    private int rating;

    public Course() {
    }

    public Course(String n, int dur, int r) {
        name = n;
        duration = dur;
        rating = r;
    }

    public Long getId() {
        return id;
    }

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

    public int getRating() {
        return rating;
    }

    public void setRating(int rating) {
        this.rating = rating;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  • We defined a class Course with fields like id, name, duration,rating.
  • We define the setter and getter function for these.
  • We set the id to be auto generated and also we set the name for this column to be C_ID.

Now we make the main class which will use the hibernate criteria query.

like method usage

Listing 2: App2 class (like method)

package Main;

/**
 *
 * @author Anurag
 */
import java.util.Iterator;
import java.util.List;
import model.Course;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

public class App2 {

    public static void main(String args[]) {

        SessionFactory sf = Utility.getSessionFactory();
        Session s = sf.getCurrentSession();

        Transaction tx = s.beginTransaction();
        Criteria crit = s.createCriteria(Course.class);
        crit.add(Expression.like("name","%bernat%"));
        List course = crit.list();
        for (Iterator it = course.iterator(); it.hasNext();) {
            Course c = (Course) it.next();
            System.out.println("ID: " + c.getId());
            System.out.println("Name: " + c.getName());
            System.out.println("Duration: " + c.getDuration());
        }
        tx.commit();
    }
}
  • like method take 2 argument which is (field name, match term)
  • We make criteria for the field duration
  • We select all those records which starts and ends with anything but has “bernat” in between.
like method usage

Figure 1: like method usage

ne method usage

Listing 3: App2 class (ne method)

package Main;
	
/**
 *
 * @author Anurag
 */
import java.util.Iterator;
import java.util.List;
import model.Course;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

public class App2 {

    public static void main(String args[]) {

        SessionFactory sf = Utility.getSessionFactory();
        Session s = sf.getCurrentSession();

        Transaction tx = s.beginTransaction();
        Criteria crit = s.createCriteria(Course.class);
        crit.add(Expression.ne("id",2l));
        List course = crit.list();
        for (Iterator it = course.iterator(); it.hasNext();) {
            Course c = (Course) it.next();
            System.out.println("ID: " + c.getId());
            System.out.println("Name: " + c.getName());
            System.out.println("Duration: " + c.getDuration());
        }
        tx.commit();
    }
}
  • ne method take 2 argument which is (field name, value)
  • We make criteria for the field duration
  • We select all those records which have id not equal to 2.
ne method usage

Figure 2: ne method usage

Listing 4: App2 class (between method)

package Main;

/**
 *
 * @author Anurag
 */
import java.util.Iterator;
import java.util.List;
import model.Course;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

public class App2 {

    public static void main(String args[]) {

        SessionFactory sf = Utility.getSessionFactory();
        Session s = sf.getCurrentSession();

        Transaction tx = s.beginTransaction();
        Criteria crit = s.createCriteria(Course.class);
        crit.add(Expression.between("duration",0,8));
        List course = crit.list();
        for (Iterator it = course.iterator(); it.hasNext();) {
            Course c = (Course) it.next();
            System.out.println("ID: " + c.getId());
            System.out.println("Name: " + c.getName());
            System.out.println("Duration: " + c.getDuration());
        }
        tx.commit();
    }
}
  • between method take 3 argument which is (field name,from,to)
  • We make criteria for the field duration
  • We select all those records in which the duration is greater than 0 and less than 8
App2 class (between method)

Figure 3: App2 class (between method)

gtProperty method

Listing 5: App2 class (gtProperty method)

package Main;
	
/**
 *
 * @author Anurag
 */
import java.util.Iterator;
import java.util.List;
import model.Course;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

public class App2 {

    public static void main(String args[]) {

        SessionFactory sf = Utility.getSessionFactory();
        Session s = sf.getCurrentSession();

        Transaction tx = s.beginTransaction();
        Criteria crit = s.createCriteria(Course.class);
         crit.add(Expression.gtProperty("duration","rating"));
        List course = crit.list();
        for (Iterator it = course.iterator(); it.hasNext();) {
            Course c = (Course) it.next();
            System.out.println("ID: " + c.getId());
            System.out.println("Name: " + c.getName());
            System.out.println("Duration: " + c.getDuration());
        }
        tx.commit();
    }
}
  • le method take 2 argument which is (field name, field name2)
  • We make criteria for the field duration
  • We select all those records in which field1 has value greater than field 2
gtProperty method

Figure 4: gtProperty method

We will discuss some more methods in next coming articles. Hope you enjoyed this article. See you next time.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login