Could you explain criteria interface with simple code

Could you explain criteria interface with simple code in hibernate
LVL 9
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?
 
Pramod KumarCommented:
Nothing special here:


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.pk.Insurance" table="Insurance">
        <meta attribute="class-description">This class contains Insurance details.</meta>
        <id name="id" type="int" length="11" column="id">
            <generator class="native" />
        </id>
        <property name="name" type="string" not-null="true" length="100" column="name" />
    </class>
</hibernate-mapping>

Open in new window

0
 
Pramod KumarCommented:
Criteria is a simplified interface for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like "search" screens where there is a variable number of conditions to be placed upon the ResultSet.

The org.hibernate.Criteria interface represents a query against a persistent class. The Session is a factory for Criteria instances.
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Criteria crit = session.createCriteria(Contact.class);
List cntctList = crit.list();

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Manikandan ThiagarajanSenior consultantAuthor Commented:
Criteria crit = session.createCriteria(Contact.class);

this line what it would do?

List contains what?
0
 
Pramod KumarCommented:
Below is the better example for you:


import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;

public class HibernateCriteriaQueryExample {
  public static void main(String[] args) {
  Session session = null;
  try {
  // This step will read 
hibernate.cfg.xml and prepare hibernate for
  // use
  SessionFactory 
sessionFactory = new Configuration().configure()
  .buildSessionFactory();
  session = 
sessionFactory.openSession();
  //Criteria Query Example
  Criteria crit = 
session.createCriteria(Insurance.class);
  List insurances = 
crit.list();
  for(Iterator it = 
insurances.iterator();it.hasNext();){
  Insurance insurance = 
(Insurance) it.next();
  System.out.println("
ID: " + insurance.getLngInsuranceId());
  System.out.println("
Name: " + insurance.getInsuranceName());
  
  }
  session.close();
  } catch (Exception e) {
  System.out.println(e.getMessage());
  } finally {
  }  
  }
}

Open in new window

0
 
Pramod KumarCommented:
The above Criteria Query example selects all the records from the table and displays on the console. In the above code the following code creates a new Criteria instance, for the class Insurance:
0
 
Pramod KumarCommented:
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
Criteria crit =
session.createCriteria(Insurance.class);

could you give me the example for applicationhbm.xml how it would map to that.

application hbm.xml what it would contains
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.