Criterion
Criteria criteria = session.createCriteria(Entity.class);
Criterion criterion =Restrictions.gt("id", new Integer(11));
// .gt() : means greater than
// id is a variable of our POJO class Entity.java
criteria.add(criterion); // adding criterion object to criteria class object
List l = criteria.list(); // executing criteria query
In our above example we are fetching the data by comparing id greater than (>) 11
If we want to put more conditions on the data (multiple conditions) then we can use
.and() method ,. or() method , .eq() method using the Restrictions class
Restrictions
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(Restrictions.and(Restrictions.like("name","%M%"),Restrictions.eq("category",ne
w String(“bird”))));
List list = criteria.list();
Iterator it = list.iterator();
Configuration configurationObj = new Configuration();
configurationObj.configure("hibernate.cfg.xml");
SessionFactory sessionFactoryObj = configurationObj.buildSessionFactory();
Session sessionObj = sessionFactoryObj.openSession();
Criteria criteriaObj = sessionObj.createCriteria(Entity.class);
Criterion criterion = Restrictions.gt("id", new Integer(0));
// means : where id > 0
criteriaObj.add(criterion);
criteriaObj.addOrder(Order.desc("id"));
// will fetch the data into descending order
//criteriaObj.addOrder(Order.asc("id"));
// will fetch the data into ascending order
List entityList = criteriaObj.list();
Iterator entityListIterator=entityList.iterator();
System.out.println("ID\t NAME \t CATEGORY");
System.out.println("-------------------------");
while(entityListIterator.hasNext()){
Entity entityObj=(Entity)entityListIterator.next(); System.out.print(entityObj.getId()); System.out.print(" \t"+entityObj.getName()); System.out.println(" \t"+entityObj.getCategory());
}
System.out.println("-------------------------");
sessionObj.close();
sessionFactoryObj.close();
Criteria criteria = session.createCriteria(Entity.class);
Criterion criterion =Restrictions.gt("id", new Integer(11));
// .gt() : means greater than
// id is a variable of our POJO class Entity.java
criteria.add(criterion); // adding criterion object to criteria class object
List l = criteria.list(); // executing criteria query
In our above example we are fetching the data by comparing id greater than (>) 11
If we want to put more conditions on the data (multiple conditions) then we can use
.and() method ,. or() method , .eq() method using the Restrictions class
Restrictions
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(Restrictions.and(Restrictions.like("name","%M%"),Restrictions.eq("category",ne
w String(“bird”))));
List list = criteria.list();
Iterator it = list.iterator();
Configuration configurationObj = new Configuration();
configurationObj.configure("hibernate.cfg.xml");
SessionFactory sessionFactoryObj = configurationObj.buildSessionFactory();
Session sessionObj = sessionFactoryObj.openSession();
Criteria criteriaObj = sessionObj.createCriteria(Entity.class);
Criterion criterion = Restrictions.gt("id", new Integer(0));
// means : where id > 0
criteriaObj.add(criterion);
criteriaObj.addOrder(Order.desc("id"));
// will fetch the data into descending order
//criteriaObj.addOrder(Order.asc("id"));
// will fetch the data into ascending order
List entityList = criteriaObj.list();
Iterator entityListIterator=entityList.iterator();
System.out.println("ID\t NAME \t CATEGORY");
System.out.println("-------------------------");
while(entityListIterator.hasNext()){
Entity entityObj=(Entity)entityListIterator.next(); System.out.print(entityObj.getId()); System.out.print(" \t"+entityObj.getName()); System.out.println(" \t"+entityObj.getCategory());
}
System.out.println("-------------------------");
sessionObj.close();
sessionFactoryObj.close();
No comments:
Post a Comment