如何使用NamedEntityGraph

时间:2018-07-20 09:11:19

标签: java jpa spring-data-jpa entitygraph

我在应用程序中使用JPA实体图 我有一个CustomerEnity,其中包含其他实体“ Alarms”的集合。这是定义的EntityGraph。

@NamedEntityGraph(
            name = "graph.CustomerSite", 
            attributeNodes = {@NamedAttributeNode(value = "alarms", subgraph = "subgraph.customersite.idalarms")}, 
            subgraphs = {@NamedSubgraph(@NamedSubgraph(
                            name = "subgraph.customersite.idalarms", 
                            attributeNodes = {@NamedAttributeNode(value = "id")}
                        )
            }),

public class CustomerSite {
       @Cloumn
       @LazyCollection(LazyCollectionOption.TRUE)
       @OneToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.LAZY)
       @JoinTable(name = ConstantsDB.CustomerSiteAlarmsTableName, joinColumns = @JoinColumn(name = "customersite_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "alarms_id",
                        referencedColumnName = "id"))
        private Set<AlarmsEntity> alarms;

    }

在存储库中,我有以下方法

@EntityGraph(value = "graph.CustomerSite", type = EntityGraphType.LOAD)
repository.findBy(Long id);

使用子图,我仅获得所有ID为ID的警报,并找到类似的大小

customerEntity.getAlarms().size()

JPA EntityGraphs中是否有更好的方法可以提供特定实体的数量?在这种情况下,AlarmsEntity

0 个答案:

没有答案