@Query("select e from Employee e")
@EntityGraph(value = "https://www.cnblogs.com/zany-hui/p/graph.Employee.assignments", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAllWithAssignments();
@Query("select e from Employee e")
@EntityGraph(value = "https://www.cnblogs.com/zany-hui/p/graph.Employee.absences", type = EntityGraph.EntityGraphType.FETCH)
List<Employee> findAllWithAbsences();
在很多情況下,我們使用Spring-Data-Jpa,一些簡單的查詢,我們都喜歡用定義方法查詢,而不是寫JPQL,JPA為我們提供了一組注解:使用Spring-Data-Jpa為我們提供的@EntityGraph,或@EntityGraph和@NamedEntityGraph進行解決,
@NamedEntityGraphs、@NamedEntityGraph、@EntityGraph
@NamedEntityGraphs:用于對@NamedEntityGraph注解進行分組,
@NamedEntityGraph:用于指定查找操作或查詢的路徑和邊界,
屬性name:(可選) 物體圖的名稱, 默認為根物體的物體名,
屬性attributeNodes:(可選) 包含在該圖中的物體屬性串列,
屬性:includeAllAttributes:(可選)將注釋物體類的所有屬性作為屬性節點包含在NamedEntityGraph中,而無需顯式列出它們,包含的屬性仍然可以由參考子圖的屬性節點完全指定,默認為false,一般不需要設定,
屬性subgraphs:(可選)包含在物體圖中的子圖串列,這些是從NamedAttributeNode定義中按名稱參考的,
屬性subclassSubgraphs:(可選) 子圖串列 這些子圖將向物體圖添加注釋物體類的子類的附加屬性,超類中的指定屬性包含在子類中,
@EntityGraph: 注解用于配置 JPA 2.1規范支持的javax.persistence.EntityGraph,應該使用在repository的方法上面,從1.9開始,我們支持動態EntityGraph定義,允許通過attributePaths()配置自定義fetch-graph,如果指定了attributePaths(),則忽略entity-graph的name(也就是配置的value()),并將EntityGraph視為動態的,
屬性value:要使用的名稱,如果為空,則回傳JpaQueryMethod.getNamedQueryName()作為value,一般為@NamedEntityGraph的name值,或者不填使用自己的attributePaths屬性,
屬性type:要使用的EntityGraphType,默認為EntityGraphType.FETCH,
屬性attributePaths:要使用的屬性路徑,默認為空,可以直接參考物體屬性,也可以通過roperty.nestedProperty參考嵌套屬性,
列舉EntityGraphType:
LOAD("javax.persistence.loadgraph"):當javax.persistence.loadgraph屬性用于指定物體圖時,由物體圖的attributePaths指定的屬性將被視為FetchType.EAGER,未指定的屬性,將根據其設定的或默認的FetchType來進行處理,
FETCH("javax.persistence.fetchgraph"):當javax.persistence.fetchgraph屬性用于指定物體圖時,由物體圖的attributePaths指定的屬性將被視為FetchType.EAGER,而未指定的屬性被視為FetchType.LAZY,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71622.html
標籤:Java
上一篇:Spring session、set-cookie失效、跨域、每次請求sessionid不一致,Cookie sameSite坑 跨域之坑
