Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Hibernate Hibernate4 Hibernate Built-in criterion "between" Integer

Related Tutorials


 
 

Share on Google+Share on Google+

Hibernate Built-in criterion "between" Integer

Advertisement
In this tutorial you will learn about how to use the built-in criterion ' between ' with the integer values.

Hibernate Built-in criterion "between" Integer

In this tutorial you will learn about how to use the built-in criterion ' between ' with the integer values.

Hibernate provides the facility to make a Criteria query restricted. An interface Criterion is responsible for representing a query criterion as an object oriented this query might be used in a Criteria query to make it restricted. However, this interface may be implemented in the class where a custom restriction criteria is to be defined. A class Restrictions is a factory class that provides the built-in criterion.

Example :

Here I am going to give an example of built-in criterion "between" which is provided by the Restrictions factory class. Between criterion applied the between constraint on the specified properties i.e it returns the value between the specified minimum and maximum integer value range. In this example I will fetch the id, name, gender of Employee1 from the table employeename1 between the specified age including low & high range.

Table employeename1

CREATE TABLE `employeename1` ( 
`id` int(10) NOT NULL, 
`name` varchar(15) default NULL, 
`gender` varchar(5) default NULL, 
`age` int(10) default '0', 
PRIMARY KEY (`id`) 
)

Employee1.java

package roseindia;

public class Employee1 {
private int id; 
private String name;
private String gender;
private int age;

public Employee1()
{

}
public Employee1(String name, String gender, int age)
{
this.name = name;
this.gender = gender;
this.age = age;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}

Employee1.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name= "roseindia.Employee1" table="employeename1">
<id name= "id" type="int" >
<generator class="native" />
</id>
<property name="name" />
<property name="gender" />
<property name="age" type="int" />

</class>

</hibernate-mapping>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.10.13:3306/data
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>

</hibernate-configuration>

HibernateBetweenIntegerExample.java

package roseindia;

import java.util.List;
import java.util.Iterator;
import org.hibernate.SessionFactory;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Criteria;

public class HibernateBetweenIntegerExample {
private static SessionFactory sessionFactory=null;
private static ServiceRegistry serviceRegistry=null;
public static void main(String args[])
{
Session session = null;

try
{
Configuration cfg = new Configuration().addResource("roseindia/Employee1.hbm.xml");
cfg.configure();

serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
}
catch(Throwable th)
{
System.err.println("Failed to create sessionFactory object."
+ th);
throw new ExceptionInInitializerError(th);
}
try
{
session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Employee1.class);
List ls = criteria.add(Restrictions.between("age", 26, 29)).list();
Iterator it = ls.iterator();
System.out.println("Id \t Name \tGender \tAge");
while(it.hasNext())
{
Employee1 emp1 = (Employee1)it.next();
System.out.println(emp1.getId()+"\t"+emp1.getName()+"\t"+emp1.getGender()+"\t"+emp1.getAge());
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
session.close();
}
}

}

Output :

table employeename1

When you will execute the java file HibernateBetweenIntegerExample.java ( RightClick -> Run As -> Java Application ) a query and output will be displayed on your console as :

INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Mar 23, 2012 6:36:39 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.gender as gender0_0_, this_.age as age0_0_ from employeename1 this_ where this_.age between ? and ?

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 26, 2012

Related Tutorials

Discuss: Hibernate Built-in criterion "between" Integer  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com