Example usage for javax.persistence Subgraph addSubgraph

List of usage examples for javax.persistence Subgraph addSubgraph

Introduction

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

Prototype

public <X> Subgraph<X> addSubgraph(String attributeName);

Source Link

Document

Add a node to the graph that corresponds to a managed type.

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  ww  w . j a v a  2 s .  c om

        // 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]);
    }
}