use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.
the class SessionFactoryHelper method createJoinSequence.
/**
* Generate a join sequence representing the given association type.
*
* @param implicit Should implicit joins (theta-style) or explicit joins (ANSI-style) be rendered
* @param associationType The type representing the thing to be joined into.
* @param tableAlias The table alias to use in qualifying the join conditions
* @param joinType The type of join to render (inner, outer, etc); see {@link org.hibernate.sql.JoinFragment}
* @param columns The columns making up the condition of the join.
*
* @return The generated join sequence.
*/
public JoinSequence createJoinSequence(boolean implicit, AssociationType associationType, String tableAlias, JoinType joinType, String[] columns) {
JoinSequence joinSequence = createJoinSequence();
// Implicit joins use theta style (WHERE pk = fk), explicit joins use JOIN (afterQuery from)
joinSequence.setUseThetaStyle(implicit);
joinSequence.addJoin(associationType, tableAlias, joinType, columns);
return joinSequence;
}
use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.
the class MapKeyEntityFromElement method buildKeyJoin.
public static MapKeyEntityFromElement buildKeyJoin(FromElement collectionFromElement) {
final HqlSqlWalker walker = collectionFromElement.getWalker();
final SessionFactoryHelper sfh = walker.getSessionFactoryHelper();
final SessionFactoryImplementor sf = sfh.getFactory();
final QueryableCollection collectionPersister = collectionFromElement.getQueryableCollection();
final Type indexType = collectionPersister.getIndexType();
if (indexType == null) {
throw new IllegalArgumentException("Given collection is not indexed");
}
if (!indexType.isEntityType()) {
throw new IllegalArgumentException("Given collection does not have an entity index");
}
final EntityType indexEntityType = (EntityType) indexType;
final EntityPersister indexEntityPersister = (EntityPersister) indexEntityType.getAssociatedJoinable(sf);
final String rhsAlias = walker.getAliasGenerator().createName(indexEntityPersister.getEntityName());
final boolean useThetaJoin = collectionFromElement.getJoinSequence().isThetaStyle();
MapKeyEntityFromElement join = new MapKeyEntityFromElement(useThetaJoin);
join.initialize(HqlSqlTokenTypes.JOIN_FRAGMENT, ((Joinable) indexEntityPersister).getTableName());
join.initialize(collectionFromElement.getWalker());
join.initializeEntity(collectionFromElement.getFromClause(), indexEntityPersister.getEntityName(), indexEntityPersister, indexEntityType, "<map-key-join-" + collectionFromElement.getClassAlias() + ">", rhsAlias);
// String[] joinColumns = determineJoinColuns( collectionPersister, joinTableAlias );
// todo : assumes columns, no formulas
String[] joinColumns = collectionPersister.getIndexColumnNames(collectionFromElement.getCollectionTableAlias());
JoinSequence joinSequence = sfh.createJoinSequence(useThetaJoin, indexEntityType, rhsAlias, // JoinType.INNER_JOIN,
collectionFromElement.getJoinSequence().getFirstJoin().getJoinType(), joinColumns);
join.setJoinSequence(joinSequence);
join.setOrigin(collectionFromElement, true);
join.setColumns(joinColumns);
join.setUseFromFragment(collectionFromElement.useFromFragment());
join.setUseWhereFragment(collectionFromElement.useWhereFragment());
walker.addQuerySpaces(indexEntityPersister.getQuerySpaces());
return join;
}
use of org.hibernate.engine.internal.JoinSequence 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 org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.
the class PathExpressionParser method prepareForIndex.
private void prepareForIndex(QueryTranslatorImpl q) throws QueryException {
QueryableCollection collPersister = q.getCollectionPersister(collectionRole);
if (!collPersister.hasIndex()) {
throw new QueryException("unindexed collection beforeQuery []: " + path);
}
String[] indexCols = collPersister.getIndexColumnNames();
if (indexCols.length != 1) {
throw new QueryException("composite-index appears in []: " + path);
}
//String[] keyCols = collPersister.getKeyColumnNames();
JoinSequence fromJoins = new JoinSequence(q.getFactory()).setUseThetaStyle(useThetaStyleJoin).setRoot(collPersister, collectionName).setNext(joinSequence.copy());
if (!continuation) {
addJoin(collectionName, collPersister.getCollectionType());
}
//TODO: get SQL rendering out of here
joinSequence.addCondition(collectionName + '.' + indexCols[0] + " = ");
CollectionElement elem = new CollectionElement();
elem.elementColumns = collPersister.getElementColumnNames(collectionName);
elem.elementType = collPersister.getElementType();
elem.isOneToMany = collPersister.isOneToMany();
elem.alias = collectionName;
elem.joinSequence = joinSequence;
collectionElements.addLast(elem);
setExpectingCollectionIndex();
q.addCollection(collectionName, collectionRole);
q.addFromJoinOnly(collectionName, fromJoins);
}
use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.
the class QueryTranslatorImpl method addFromClass.
void addFromClass(String name, Queryable classPersister) throws QueryException {
JoinSequence joinSequence = new JoinSequence(getFactory()).setRoot(classPersister, name);
//crossJoins.add(name);
addFrom(name, classPersister.getEntityName(), joinSequence);
}
Aggregations