Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

Could you explain criteria interface with simple code

Could you explain criteria interface with simple code in hibernate
0
Manikandan Thiagarajan
Asked:
Manikandan Thiagarajan
  • 5
  • 2
1 Solution
 
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
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
Criteria crit = session.createCriteria(Contact.class);

this line what it would do?

List contains what?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now