Java Tutorial/Hibernate/Criteria

Материал из Java эксперт
Перейти к: навигация, поиск

Add Restrictions.sqlRestriction to Criteria

File: Main.java



import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    survey.setPurchaseDate(new Date());
    session.save(survey);
    
    survey = new Survey();
    survey.setName("Survey1");
    survey.setPurchaseDate(new Date());    
    session.save(survey);
    
    session.flush();
    
    Criteria crit = session.createCriteria(Survey.class);
    crit.add(Restrictions.sqlRestriction("name like "S%""));

    
    List results = crit.list();
    
    Iterator iter = results.iterator();
    while (iter.hasNext()) {
      Survey cd = (Survey) iter.next();
      System.out.println("Result Name:"+cd.getName());
    }
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}





Combine Two Criteria

File: Main.java



import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    survey.setPurchaseDate(new Date());
    session.save(survey);
    
    survey = new Survey();
    survey.setName("Survey1");
    survey.setPurchaseDate(new Date());    
    session.save(survey);
    
    session.flush();
    
    Criteria crit = session.createCriteria(Survey.class);
    crit.add(Restrictions.gt("Id",new Integer(0)));
    crit.add(Restrictions.like("name","S%"));
    
    List results = crit.list();
    
    Iterator iter = results.iterator();
    while (iter.hasNext()) {
      Survey cd = (Survey) iter.next();
      System.out.println("Result Name:"+cd.getName());
    }
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}





Criteria and Restrictions.ne

File: Main.java



import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    survey.setPurchaseDate(new Date());
    session.save(survey);
    
    survey = new Survey();
    survey.setName("Survey1");
    survey.setPurchaseDate(new Date());    
    session.save(survey);
    
    session.flush();
    
    Criteria crit = session.createCriteria(Survey.class);
    crit.add(Restrictions.ne("name","Survey"));
    
    
    List results = crit.list();
    
    Iterator iter = results.iterator();
    while (iter.hasNext()) {
      Survey cd = (Survey) iter.next();
      System.out.println("Result Name:"+cd.getName());
    }
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}





Criteria Transformer

File: Main.java



import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    survey.setPurchaseDate(new Date());
    session.save(survey);
    
    survey = new Survey();
    survey.setName("Survey1");
    survey.setPurchaseDate(new Date());    
    session.save(survey);
    
    session.flush();
    
    Criteria crit = session.createCriteria(Survey.class);
    crit.add(Restrictions.gt("Id",new Integer(0)));
    crit.add(Restrictions.like("name","S%"));
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    
    
    List results = crit.list();
    System.out.println(results);
    
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}





Descending Order

File: Main.java



import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    survey.setPurchaseDate(new Date());
    session.save(survey);
    
    survey = new Survey();
    survey.setName("Survey1");
    survey.setPurchaseDate(new Date());    
    session.save(survey);
    
    session.flush();
    
    Criteria crit = session.createCriteria(Survey.class);
    crit.add(Restrictions.gt("Id",new Integer(0)));
    crit.addOrder(Order.desc("Id"));
    List results = crit.list();
    
    
    Iterator iter = results.iterator();
    while (iter.hasNext()) {
      Survey cd = (Survey) iter.next();
      System.out.println("Result Name:"+cd.getName());
    }
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}





Pageable result: First Result and Max Result

File: Main.java



import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    survey.setPurchaseDate(new Date());
    session.save(survey);
    
    survey = new Survey();
    survey.setName("Survey1");
    survey.setPurchaseDate(new Date());    
    session.save(survey);
    
    session.flush();
    
    Criteria crit = session.createCriteria(Survey.class);
    crit.setFirstResult(1);
    crit.setMaxResults(2);

    
    List results = crit.list();
    
    Iterator iter = results.iterator();
    while (iter.hasNext()) {
      Survey cd = (Survey) iter.next();
      System.out.println("Result Name:"+cd.getName());
    }
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}





Return Unique Result

File: Main.java



import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil
        .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    survey.setPurchaseDate(new Date());
    session.save(survey);
    
    survey = new Survey();
    survey.setName("Survey1");
    survey.setPurchaseDate(new Date());    
    session.save(survey);
    
    session.flush();
    
    Criteria crit = session.createCriteria(Survey.class);
    crit.setFirstResult(1);
    crit.setMaxResults(2);

    Criterion price = Restrictions.gt("Id",new Integer(0));
    crit.setMaxResults(1);
    Survey product = (Survey) crit.uniqueResult();
    //test for null here if needed
    
    List results = new ArrayList();
    results.add(product);
    
    
    Iterator iter = results.iterator();
    while (iter.hasNext()) {
      Survey cd = (Survey) iter.next();
      System.out.println("Result Name:"+cd.getName());
    }
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}