Example usage for javax.persistence Subgraph addAttributeNodes

List of usage examples for javax.persistence Subgraph addAttributeNodes

Introduction

In this page you can find the example usage for javax.persistence Subgraph addAttributeNodes.

Prototype

public void addAttributeNodes(Attribute<T, ?>... attribute);

Source Link

Document

Add one or more attribute nodes to the entity graph.

Usage

From source file:org.mmonti.entitygraph.repository.CustomGenericJpaRepository.java

private void buildEntityGraph(EntityGraph<T> entityGraph, String[] attributeGraph) {
    List<String> attributePaths = Arrays.asList(attributeGraph);

    // Sort to ensure that the intermediate entity subgraphs are created accordingly.
    Collections.sort(attributePaths);
    Collections.reverse(attributePaths);

    // We build the entity graph based on the paths with highest depth first
    for (String path : attributePaths) {

        // Fast path - just single attribute
        if (!path.contains(".")) {
            entityGraph.addAttributeNodes(path);
            continue;
        }/*from   w w  w .ja  v  a2 s  .c  o m*/

        // We need to build nested sub fetch graphs
        String[] pathComponents = StringUtils.delimitedListToStringArray(path, ".");
        Subgraph<?> parent = null;

        for (int c = 0; c < pathComponents.length - 1; c++) {
            parent = c == 0 ? entityGraph.addSubgraph(pathComponents[c])
                    : parent.addSubgraph(pathComponents[c]);
        }

        parent.addAttributeNodes(pathComponents[pathComponents.length - 1]);
    }
}