Criteria查询(Criteria Query),这种查询方式把查询条件封装为一个Criteria对象。在实际应用中,使用Session的createCriteria()方法构建一个org.hibernate.Criteria实例,然后把具体的查询条件通过Criteria的add()方法加入到Criteria实例中。这样,程序员可以不使用SQL甚至HQL的情况下进行数据查询。
以下以两个实体类User、Department为例:
User:
- import javax.validation.constraints.DecimalMax;
- import javax.validation.constraints.DecimalMin;
- import javax.validation.constraints.Email;
- import javax.validation.constraints.Max;
- import javax.validation.constraints.Min;
- import javax.validation.constraints.NotEmpty;
- import javax.validation.constraints.NotNull;
- import javax.validation.constraints.Past;
-
- import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-
-
- @JsonIgnoreProperties({"dpt"})
- public class User {
- private int id;
- @NotEmpty
- private String name;
- @Min(value =1)
- @Max(value =200)
- private int age;
- @NotEmpty
- private String pwd;
- @Past
- private Date birthday;
- @NotNull
- private Department dpt;
- @DecimalMin(value = "1")
- @DecimalMax(value = "3")
- private Double height;
- @Email
- private String email;
- setter............. getter...........
- }
Department:
- import java.util.HashSet;
- import java.util.Set;
-
- public class Department {
- private int id;
- private String name;
- private Set
users = new HashSet(); - setter...... getter........
- }
- public List<User> selectUserold(int gt, int lt, int dep_id) {
- //1、创建builder
- CriteriaBuilder builder=getSession().getCriteriaBuilder();
- //2、拿到criteriaQuery
- CriteriaQuery<User> criteriaQuery=builder.createQuery(User.class);
- //3、获取到根对像,构建查询条件
- Root<User> root=criteriaQuery.from(User.class);
- Predicate predicate1= (Predicate) builder.gt(root.get("id"), gt);
- Predicate predicate2= (Predicate) builder.lt(root.get("id"), lt);
- Predicate predicate3= (Predicate) builder.equal(root.get("dpt"),dep_id);
- Predicate predicate=builder.and(predicate1,predicate2,predicate3);
-
- /* 设置要获取数据的字段 */
- criteriaQuery.select(root.get("name"));
-
- /* 设置条件 */
- criteriaQuery.where(predicate);
-
- //4、获取query对象
- Query<User> query=getSession().createQuery(criteriaQuery);
-
- //5、执行查询
- List<User> users=query.list();
- return users;
- }
-
- /* CriteriaQuery根据条件查询多个字段的值 */
- public List<User> selectUser(int gt, int lt, int dep_id) {
- //1、创建builder
- CriteriaBuilder builder = getSession().getCriteriaBuilder();
- //2、拿到criteriaQuery
- CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
- //3、获取到根对像,构建查询条件
- Root<User> root = criteria.from(User.class);
-
- Predicate predicate1= (Predicate) builder.gt(root.get("id"), gt);
- Predicate predicate2= (Predicate) builder.lt(root.get("id"), lt);
- Predicate predicate3= (Predicate) builder.equal(root.get("dpt"),dep_id);
- Predicate predicate=builder.and(predicate1,predicate2,predicate3);
-
- /* 设置条件 */
- criteria.where(predicate);
-
- /* 设置要获取值的字段 */
- Path<Integer> qId = root.get("id");
- Path<String> qname = root.get("name");
-
- criteria.multiselect(qId,qname);
-
- /* 获取到的结果 */
- List<Tuple> tuples = getSession().createQuery(criteria).getResultList();
- for (Tuple tuple : tuples)
- {
- int id = tuple.get(qId);
- String name = tuple.get(qname);
- System.out.println(id+":"+name);
- }
- return null;
- }
需导入的包:
- import javax.persistence.Tuple;
- import javax.persistence.criteria.CriteriaBuilder;
- import javax.persistence.criteria.CriteriaQuery;
- import javax.persistence.criteria.Join;
- import javax.persistence.criteria.JoinType;
- import javax.persistence.criteria.Path;
- import javax.persistence.criteria.Predicate;
- import javax.persistence.criteria.Root;