Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Could you explain query interface with simple code

ould you explain query interface with simple code in Hibernate
0
Manikandan Thiagarajan
Asked:
Manikandan Thiagarajan
1 Solution
 
ldavis07Commented:
The Criteria interface allows to create and execute object-oriented queries. It is powerful alternative to the HQL but has own limitations. Criteria Query is used mostly in case of multi criteria search screens, where HQL is not very effective.

The interface org.hibernate.Criteria is used to create the criterion for the search. The org.hibernate.Criteria interface represents a query against a persistent class. The Session is a factory for Criteria instances. Here is a simple example of Hibernate Criterial Query:

package roseindia.tutorial.hibernate;

import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
/**
 * @author Deepak Kumar
 *
 * http://www.roseindia.net 
Hibernate Criteria Query Example
 *  
 */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 {
  }  
  }
}
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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