Search in sources :

Example 1 with AttributeNode

use of javax.persistence.AttributeNode 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 2 with AttributeNode

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

the class SubgraphOrmNamedEntityGraphTest method testSubgraphsAreLoadededFromOrmXml.

@Test
@TestForIssue(jiraKey = "HHH-10633")
public void testSubgraphsAreLoadededFromOrmXml() throws Exception {
    EntityManager entityManager = getOrCreateEntityManager();
    List<EntityGraph<? super Book>> lneg = entityManager.getEntityGraphs(Book.class);
    assertNotNull(lneg);
    Assert.assertEquals(2, lneg.size());
    for (EntityGraph<? super Book> neg : lneg) {
        if (neg.getName().equalsIgnoreCase("full")) {
            assertNotNull(neg.getAttributeNodes());
            for (AttributeNode<?> n : neg.getAttributeNodes()) {
                if (n.getAttributeName().equalsIgnoreCase("authors")) {
                    Assert.assertEquals(1, n.getSubgraphs().size());
                    java.util.List<javax.persistence.AttributeNode<?>> attributeNodes = n.getSubgraphs().get(Author.class).getAttributeNodes();
                    assertNotNull("Subgraph attributes missing", attributeNodes);
                    Assert.assertEquals("Subgraph wrong number of attributes ", 3, attributeNodes.size());
                }
            }
        }
    }
    entityManager.close();
}
Also used : EntityGraph(javax.persistence.EntityGraph) EntityManager(javax.persistence.EntityManager) AttributeNode(javax.persistence.AttributeNode) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with AttributeNode

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

the class EntityGraphQueryHint method getFromElements.

private List<FromElement> getFromElements(List attributeNodes, FromElement origin, FromClause fromClause, HqlSqlWalker walker, Map<String, FromElement> explicitFetches) {
    final List<FromElement> fromElements = new ArrayList<FromElement>();
    for (Object obj : attributeNodes) {
        final AttributeNode<?> attributeNode = (AttributeNode<?>) obj;
        final String attributeName = attributeNode.getAttributeName();
        final String className = origin.getClassName();
        // TODO: This is ignored by collection types and probably wrong for entity types.  Presumably it screws
        // with inheritance.
        final String role = className + "." + attributeName;
        final String classAlias = origin.getClassAlias();
        final String originTableAlias = origin.getTableAlias();
        final Type propertyType = origin.getPropertyType(attributeName, attributeName);
        try {
            FromElement fromElement = explicitFetches.get(role);
            boolean explicitFromElement = false;
            if (fromElement == null) {
                if (propertyType.isEntityType()) {
                    final EntityType entityType = (EntityType) propertyType;
                    final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
                    final String tableAlias = walker.getAliasGenerator().createName(entityType.getAssociatedEntityName());
                    final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
                    final JoinSequence joinSequence = walker.getSessionFactoryHelper().createJoinSequence(false, entityType, tableAlias, JoinType.LEFT_OUTER_JOIN, columns);
                    fromElement = fromElementFactory.createEntityJoin(entityType.getAssociatedEntityName(), tableAlias, joinSequence, true, walker.isInFrom(), entityType, role, null);
                } else if (propertyType.isCollectionType()) {
                    CollectionType collectionType = (CollectionType) propertyType;
                    final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
                    final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
                    final QueryableCollection queryableCollection = walker.getSessionFactoryHelper().requireQueryableCollection(collectionType.getRole());
                    fromElement = fromElementFactory.createCollection(queryableCollection, collectionType.getRole(), JoinType.LEFT_OUTER_JOIN, true, false);
                }
            } else {
                explicitFromElement = true;
                fromElement.setInProjectionList(true);
                fromElement.setFetch(true);
            }
            if (fromElement != null) {
                if (!explicitFromElement) {
                    fromElements.add(fromElement);
                }
                // recurse into subgraphs
                for (Subgraph<?> subgraph : attributeNode.getSubgraphs().values()) {
                    fromElements.addAll(getFromElements(subgraph.getAttributeNodes(), fromElement, fromClause, walker, explicitFetches));
                }
            }
        } catch (Exception e) {
            throw new QueryException("Could not apply the EntityGraph to the Query!", e);
        }
    }
    return fromElements;
}
Also used : AttributeNode(javax.persistence.AttributeNode) ArrayList(java.util.ArrayList) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) QueryException(org.hibernate.QueryException) EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) ImpliedFromElement(org.hibernate.hql.internal.ast.tree.ImpliedFromElement) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) CollectionType(org.hibernate.type.CollectionType) FromElementFactory(org.hibernate.hql.internal.ast.tree.FromElementFactory) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 4 with AttributeNode

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

the class NamedEntityGraphsTest method testAttributeNodesAreAvailable.

@Test
public void testAttributeNodesAreAvailable() {
    EntityManager em = getOrCreateEntityManager();
    EntityGraph graph = em.getEntityGraph("name_salary_graph");
    assertNotNull(graph);
    List<AttributeNode<?>> list = graph.getAttributeNodes();
    assertNotNull(list);
    assertTrue("expected list.size() is two but actual list size is " + list.size(), 2 == list.size());
    AttributeNode attributeNode1 = list.get(0);
    AttributeNode attributeNode2 = list.get(1);
    assertNotNull(attributeNode1);
    assertNotNull(attributeNode2);
    assertTrue("node1 attribute name is expected to be either 'name' or 'salary' but actually is " + attributeNode1.getAttributeName(), "name".equals(attributeNode1.getAttributeName()) || "salary".equals(attributeNode1.getAttributeName()));
    assertTrue("node2 attribute name is expected to be either 'name' or 'salary' but actually is " + attributeNode2.getAttributeName(), "name".equals(attributeNode2.getAttributeName()) || "salary".equals(attributeNode2.getAttributeName()));
}
Also used : EntityGraph(javax.persistence.EntityGraph) EntityManager(javax.persistence.EntityManager) AttributeNode(javax.persistence.AttributeNode) Test(org.junit.Test)

Aggregations

AttributeNode (javax.persistence.AttributeNode)4 EntityManager (javax.persistence.EntityManager)3 Test (org.junit.Test)3 EntityGraph (javax.persistence.EntityGraph)2 ArrayList (java.util.ArrayList)1 Subgraph (javax.persistence.Subgraph)1 QueryException (org.hibernate.QueryException)1 JoinSequence (org.hibernate.engine.internal.JoinSequence)1 FromElement (org.hibernate.hql.internal.ast.tree.FromElement)1 FromElementFactory (org.hibernate.hql.internal.ast.tree.FromElementFactory)1 ImpliedFromElement (org.hibernate.hql.internal.ast.tree.ImpliedFromElement)1 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)1 JoinType (org.hibernate.sql.JoinType)1 TestForIssue (org.hibernate.testing.TestForIssue)1 CollectionType (org.hibernate.type.CollectionType)1 EntityType (org.hibernate.type.EntityType)1 Type (org.hibernate.type.Type)1