zoukankan      html  css  js  c++  java
  • java-jpa-criteriaBuilder使用入门

    jpa

    概念 
    创建使用Java Persistence API的存储库是一个繁琐的过程,需要大量时间并需要大量样板代码。一种推荐的方式是使用

    元模型

    概念 
    在JPA中,标准查询是以元模型的概念为基础的,元模型是为具体持久化单元的受管实体定义的.这些实体可以是实体类,嵌入类或者映射的父类.提供受管实体元信息的类就是元模型类. 
    简单的说就是元模型是实体类对应的一个“受管实体 
    举个例子: 
    实体类 Employee(com.demo.entities包中定义)

    @Entity
    @Table
    public class Employee{  
        private int id;   
        private String name;
        private int age;
        @OneToMany
        private List<Address> addresses;
        // Other code…
    }

    Employee类的标准元模型类的名字是 Employee_

    import javax.annotation.Generated;
    import javax.persistence.metamodel.SingularAttribute;
    import javax.persistence.metamodel.ListAttribute;
    import javax.persistence.metamodel.StaticMetamodel;
    @StaticMetamodel(Employee.class)
    public class Employee_ {     
        public static volatile SingularAttribute<Employee, Integer> id;   
        public static volatile SingularAttribute<Employee, Integer> age;   
        public static volatile SingularAttribute<Employee, String> name;    
        public static volatile ListAttribute<Employee, Address> addresses;
    }

    Employee的每一个属性都会使用在JPA2规范中描述的以下规则在相应的元模型类中映射:

    • 元模型类的属性全部是static和public的。
    • 元模型类的属性全部是static和public的。Employee的每一个属性都会使用在JPA2规范中描述的以下规则在相应的元模型类中映射:

    • 对于Addess这样的集合类型,会定义静态属性ListAttribute< A, B> b,这里List对象b是定义在类A中类型B的对象。其它集合类型可以是SetAttribute, MapAttribute 或 CollectionAttribute 类型。

    看到这应该会有个疑问,这麻烦,为什么要使用这个元模型?有啥好处? 
    好处肯定是有的,毕竟是标准jpa定义的东西。我这网上查了下,好处很多:

    • 查询更加类型安全

    好吧,我暂时就查到这个。

    criteria 查询

    为了更好的理解criteria 查询,考虑拥有Employee实例集合的Dept实体,Employee和Dept的元模型类的代码如下:

    //All Necessary Imports
    @StaticMetamodel(Dept.class)
    public class Dept_ {    
        public static volatile SingularAttribute<Dept, Integer> id;   
        public static volatile ListAttribute<Dept, Employee> employeeCollection;    
        public static volatile SingularAttribute<Dept, String> name;
    }
    //All Necessary Imports
    @StaticMetamodel(Employee.class)
    public class Employee_ {     
        public static volatile SingularAttribute<Employee, Integer> id;    
        public static volatile SingularAttribute<Employee, Integer> age;    
        public static volatile SingularAttribute<Employee, String> name;    
        public static volatile SingularAttribute<Employee, Dept> deptId;
    }

    下面的代码片段展示了一个criteria 查询,它用于获取所有年龄大于24岁的员工:

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
    Root<Employee> employee = criteriaQuery.from(Employee.class);
    Predicate condition = criteriaBuilder.gt(employee.get(Employee_.age), 24);
    criteriaQuery.where(condition);
    TypedQuery<Employee> typedQuery = em.createQuery(criteriaQuery);
    List<Employee> result = typedQuery.getResultList();

    对应的SQL: SELECT * FROM employee WHERE age > 24

    CriteriaBuilder 安全查询创建工厂

    CriteriaBuilder 安全查询创建工厂,,创建CriteriaQuery,创建查询具体具体条件Predicate 等。 
    CriteriaBuilder是一个工厂对象,安全查询的开始.用于构建JPA安全查询.可以从EntityManager 或 EntityManagerFactory类中获得CriteriaBuilder。 
    比如: 

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();

    CriteriaQuery 安全查询主语句

      1. 它通过调用 CriteriaBuilder, createQuery 或CriteriaBuilder.createTupleQuery 获得。
      2. CriteriaBuilder就像CriteriaQuery 的工厂一样。
      3. CriteriaQuery对象必须在实体类型或嵌入式类型上的Criteria 查询上起作用。
      4. Employee实体的 CriteriaQuery 对象以下面的方式创建:
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);

    过Employee_元模型类age属性,称之为路径表达式。若age属性与String文本比较,编译器会抛出错误,这在JPQL中是不可能的。这就是元模型的作用吗??

    Predicate[] 多个过滤条件

    引用原文:http://blog.csdn.net/id_kong/article/details/70225032
    List<Predicate> predicatesList = new ArrayList<Predicate>();
    
    predicatesList.add(.....Pridicate....)
    
    criteriaQuery.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
  • 相关阅读:
    实现单台测试机6万websocket长连接
    关于ArcGIS10.0中的栅格计算中的函数
    .NET破解之迅捷PDF转换器(续)
    ArcEngine 0x8004023C
    VS2010中重命名项目
    地理数据库本身不能加密
    ArcEngine尝试读取或写入受保护的内存
    利用路由器连接别人家的网络
    ArcGIS的许可文件问题
    MapGIS转Shp文件的单位问题
  • 原文地址:https://www.cnblogs.com/mzdljgz/p/11387168.html
Copyright © 2011-2022 走看看