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()]));


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM