Java Tutorial/Hibernate/Restrictions

Материал из Java эксперт
Версия от 05:02, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Logical Expression OR

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.LogicalExpression;
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);
    Criterion price = Restrictions.gt("Id",new Integer(1));
    Criterion name = Restrictions.like("name","S%");
    LogicalExpression orExp = Restrictions.or(price,name);
    crit.add(orExp);
    
    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");
  }
}





Restriction: EQ

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);
    session.flush();
    
    Criteria crit = session.createCriteria(Survey.class);
    crit.add(Restrictions.eq("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");
  }
}





Restriction: GT

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)));
    
    
    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");
  }
}





Restriction:ILIKE

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.ilike("name","Sur", MatchMode.END));
    
    
    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");
  }
}





Restriction:IS NULL

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.setPurchaseDate(new Date());    
    session.save(survey);
    
    session.flush();
    
    Criteria crit = session.createCriteria(Survey.class);
    crit.add(Restrictions.isNull("name"));
    
    
    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");
  }
}





Restriction: LIKE

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.like("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");
  }
}





Restrictions.Disjunction

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);
    Criterion price = Restrictions.gt("Id",new Integer(0));
    Criterion name = Restrictions.like("name","S%");
    Disjunction disjunction = Restrictions.disjunction();
    disjunction.add(price);
    disjunction.add(name);
    crit.add(disjunction);
    
    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");
  }
}