Search in sources :

Example 1 with Subgraph

use of javax.persistence.Subgraph in project hibernate-orm by hibernate.

the class AbstractEntityGraphVisitationStrategy method startingCollectionIndex.

@Override
public void startingCollectionIndex(final CollectionIndexDefinition indexDefinition) {
    AttributeNodeImplementor attributeNode = attributeStack.peekLast();
    GraphNodeImplementor subGraphNode = NON_EXIST_SUBGRAPH_NODE;
    Map<Class, Subgraph> subGraphs = attributeNode.getKeySubgraphs();
    Class javaType = indexDefinition.getType().getReturnedClass();
    if (!subGraphs.isEmpty() && subGraphs.containsKey(javaType)) {
        subGraphNode = (GraphNodeImplementor) subGraphs.get(javaType);
    }
    graphStack.addLast(subGraphNode);
    super.startingCollectionIndex(indexDefinition);
}
Also used : AttributeNodeImplementor(org.hibernate.graph.spi.AttributeNodeImplementor) Subgraph(javax.persistence.Subgraph) GraphNodeImplementor(org.hibernate.graph.spi.GraphNodeImplementor)

Example 2 with Subgraph

use of javax.persistence.Subgraph in project hibernate-orm by hibernate.

the class BasicEntityGraphTests method testBasicGraphImmutability.

@Test
@SuppressWarnings("unchecked")
public void testBasicGraphImmutability() {
    EntityManager em = getOrCreateEntityManager();
    EntityGraph<Entity1> graphRoot = em.createEntityGraph(Entity1.class);
    graphRoot.addSubgraph("parent");
    graphRoot.addSubgraph("children");
    em.getEntityManagerFactory().addNamedEntityGraph("immutable", graphRoot);
    graphRoot = (EntityGraph<Entity1>) em.getEntityGraph("immutable");
    assertEquals("immutable", graphRoot.getName());
    assertEquals(2, graphRoot.getAttributeNodes().size());
    try {
        graphRoot.addAttributeNodes("parent");
        fail("Should have failed");
    } catch (IllegalStateException ignore) {
    // expected outcome
    }
    for (AttributeNode attrNode : graphRoot.getAttributeNodes()) {
        assertEquals(1, attrNode.getSubgraphs().size());
        Subgraph subgraph = (Subgraph) attrNode.getSubgraphs().values().iterator().next();
        try {
            graphRoot.addAttributeNodes("parent");
            fail("Should have failed");
        } catch (IllegalStateException ignore) {
        // expected outcome
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) AttributeNode(javax.persistence.AttributeNode) Subgraph(javax.persistence.Subgraph) Test(org.junit.Test)

Example 3 with Subgraph

use of javax.persistence.Subgraph in project hibernate-orm by hibernate.

the class AbstractEntityGraphVisitationStrategy method startingCollectionElements.

@Override
public void startingCollectionElements(final CollectionElementDefinition elementDefinition) {
    AttributeNodeImplementor attributeNode = attributeStack.peekLast();
    GraphNodeImplementor subGraphNode = NON_EXIST_SUBGRAPH_NODE;
    Map<Class, Subgraph> subGraphs = attributeNode.getSubgraphs();
    Class javaType = elementDefinition.getType().getReturnedClass();
    if (!subGraphs.isEmpty() && subGraphs.containsKey(javaType)) {
        subGraphNode = (GraphNodeImplementor) subGraphs.get(javaType);
    }
    graphStack.addLast(subGraphNode);
    super.startingCollectionElements(elementDefinition);
}
Also used : AttributeNodeImplementor(org.hibernate.graph.spi.AttributeNodeImplementor) Subgraph(javax.persistence.Subgraph) GraphNodeImplementor(org.hibernate.graph.spi.GraphNodeImplementor)

Example 4 with Subgraph

use of javax.persistence.Subgraph in project hibernate-orm by hibernate.

the class AbstractEntityGraphVisitationStrategy method startingAttribute.

/**
	 * I'm using NULL-OBJECT pattern here, for attributes that not existing in the EntityGraph,
	 * a predefined NULL-ATTRIBUTE-NODE is pushed to the stack.
	 *
	 * and for an not existing sub graph, a predefined NULL-SUBGRAPH is pushed to the stack.
	 *
	 * So, whenever we're start visiting an attribute, there will be a attribute node pushed to the attribute stack,
	 * and a subgraph node pushed to the graph stack.
	 *
	 * when we're finish visiting an attribute, these two will be poped from each stack.
	 */
@Override
public boolean startingAttribute(AttributeDefinition attributeDefinition) {
    Map<String, AttributeNodeImplementor> attributeMap = attributeMapStack.peekLast();
    final String attrName = attributeDefinition.getName();
    AttributeNodeImplementor attributeNode = NON_EXIST_ATTRIBUTE_NODE;
    GraphNodeImplementor subGraphNode = NON_EXIST_SUBGRAPH_NODE;
    //the attribute is in the EntityGraph, so, let's continue
    if (attributeMap.containsKey(attrName)) {
        attributeNode = attributeMap.get(attrName);
        //here we need to check if there is a subgraph (or sub key graph if it is an indexed attribute )
        Map<Class, Subgraph> subGraphs = attributeNode.getSubgraphs();
        Class javaType = attributeDefinition.getType().getReturnedClass();
        if (!subGraphs.isEmpty() && subGraphs.containsKey(javaType)) {
            subGraphNode = (GraphNodeImplementor) subGraphs.get(javaType);
        }
    }
    attributeStack.addLast(attributeNode);
    graphStack.addLast(subGraphNode);
    return super.startingAttribute(attributeDefinition);
}
Also used : AttributeNodeImplementor(org.hibernate.graph.spi.AttributeNodeImplementor) Subgraph(javax.persistence.Subgraph) GraphNodeImplementor(org.hibernate.graph.spi.GraphNodeImplementor)

Aggregations

Subgraph (javax.persistence.Subgraph)4 AttributeNodeImplementor (org.hibernate.graph.spi.AttributeNodeImplementor)3 GraphNodeImplementor (org.hibernate.graph.spi.GraphNodeImplementor)3 AttributeNode (javax.persistence.AttributeNode)1 EntityManager (javax.persistence.EntityManager)1 Test (org.junit.Test)1