java-jpa-criteriaBuilder使用入门
jpa
概念?
創建使用Java Persistence API的存儲庫是一個繁瑣的過程,需要大量時間并需要大量樣板代碼。一種推薦的方式是使用元
元模型
概念?
在JPA中,標準查詢是以元模型的概念為基礎的,元模型是為具體持久化單元的受管實體定義的.這些實體可以是實體類,嵌入類或者映射的父類.提供受管實體元信息的類就是元模型類.?
簡單的說就是元模型是實體類對應的一個“受管實體?
-?舉個例子:?
實體類?Employee(com.demo.entities包中定義)
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。?
比如:?
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()]));轉載于:https://www.cnblogs.com/mzdljgz/p/11387168.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java-jpa-criteriaBuilder使用入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单链表的实现:增删改查
- 下一篇: 最新鲜最详细的Android SDK下载