java-jpa-criteriaBuilder使用入门
jpa
概念?
創(chuàng)建使用Java Persistence API的存儲(chǔ)庫(kù)是一個(gè)繁瑣的過(guò)程,需要大量時(shí)間并需要大量樣板代碼。一種推薦的方式是使用元
元模型
概念?
在JPA中,標(biāo)準(zhǔn)查詢是以元模型的概念為基礎(chǔ)的,元模型是為具體持久化單元的受管實(shí)體定義的.這些實(shí)體可以是實(shí)體類,嵌入類或者映射的父類.提供受管實(shí)體元信息的類就是元模型類.?
簡(jiǎn)單的說(shuō)就是元模型是實(shí)體類對(duì)應(yīng)的一個(gè)“受管實(shí)體?
-?舉個(gè)例子:?
實(shí)體類?Employee(com.demo.entities包中定義)
Employee類的標(biāo)準(zhǔn)元模型類的名字是 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的每一個(gè)屬性都會(huì)使用在JPA2規(guī)范中描述的以下規(guī)則在相應(yīng)的元模型類中映射:
- 元模型類的屬性全部是static和public的。
-
元模型類的屬性全部是static和public的。Employee的每一個(gè)屬性都會(huì)使用在JPA2規(guī)范中描述的以下規(guī)則在相應(yīng)的元模型類中映射:
-
對(duì)于Addess這樣的集合類型,會(huì)定義靜態(tài)屬性ListAttribute< A, B> b,這里L(fēng)ist對(duì)象b是定義在類A中類型B的對(duì)象。其它集合類型可以是SetAttribute, MapAttribute 或 CollectionAttribute 類型。
看到這應(yīng)該會(huì)有個(gè)疑問(wèn),這麻煩,為什么要使用這個(gè)元模型?有啥好處??
好處肯定是有的,畢竟是標(biāo)準(zhǔn)jpa定義的東西。我這網(wǎng)上查了下,好處很多:
- 查詢更加類型安全
好吧,我暫時(shí)就查到這個(gè)。
criteria 查詢
為了更好的理解criteria 查詢,考慮擁有Employee實(shí)例集合的Dept實(shí)體,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; }下面的代碼片段展示了一個(gè)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();對(duì)應(yīng)的SQL: SELECT * FROM employee WHERE age > 24
CriteriaBuilder 安全查詢創(chuàng)建工廠
CriteriaBuilder 安全查詢創(chuàng)建工廠,,創(chuàng)建CriteriaQuery,創(chuàng)建查詢具體具體條件Predicate?等。?
CriteriaBuilder是一個(gè)工廠對(duì)象,安全查詢的開(kāi)始.用于構(gòu)建JPA安全查詢.可以從EntityManager 或 EntityManagerFactory類中獲得CriteriaBuilder。?
比如:?
CriteriaQuery 安全查詢主語(yǔ)句
?
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);過(guò)Employee_元模型類age屬性,稱之為路徑表達(dá)式。若age屬性與String文本比較,編譯器會(huì)拋出錯(cuò)誤,這在JPQL中是不可能的。這就是元模型的作用嗎??
Predicate[] 多個(gè)過(guò)濾條件
引用原文: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()]));轉(zhuǎn)載于:https://www.cnblogs.com/mzdljgz/p/11387168.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的java-jpa-criteriaBuilder使用入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 单链表的实现:增删改查
- 下一篇: oracle创建表空间 扩展表空间文件