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
}
}
}
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();
}
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;
}
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()));
}
Aggregations