use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.
the class HqlSqlWalker method createFromFilterElement.
@Override
protected AST createFromFilterElement(AST filterEntity, AST alias) throws SemanticException {
FromElement fromElement = currentFromClause.addFromElement(filterEntity.getText(), alias);
FromClause fromClause = fromElement.getFromClause();
QueryableCollection persister = sessionFactoryHelper.getCollectionPersister(collectionFilterRole);
// Get the names of the columns used to link between the collection
// owner and the collection elements.
String[] keyColumnNames = persister.getKeyColumnNames();
String fkTableAlias = persister.isOneToMany() ? fromElement.getTableAlias() : fromClause.getAliasGenerator().createName(collectionFilterRole);
JoinSequence join = sessionFactoryHelper.createJoinSequence();
join.setRoot(persister, fkTableAlias);
if (!persister.isOneToMany()) {
join.addJoin((AssociationType) persister.getElementType(), fromElement.getTableAlias(), JoinType.INNER_JOIN, persister.getElementColumnNames(fkTableAlias));
}
join.addCondition(fkTableAlias, keyColumnNames, " = ?");
fromElement.setJoinSequence(join);
fromElement.setFilter(true);
LOG.debug("createFromFilterElement() : processed filter FROM element.");
return fromElement;
}
use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.
the class FromElementFactory method createElementJoin.
FromElement createElementJoin(QueryableCollection queryableCollection) throws SemanticException {
FromElement elem;
//TODO: always true for now, but not if we later decide to support elements() in the from clause
implied = true;
inElementsFunction = true;
Type elementType = queryableCollection.getElementType();
if (!elementType.isEntityType()) {
throw new IllegalArgumentException("Cannot create element join for a collection of non-entities!");
}
this.queryableCollection = queryableCollection;
SessionFactoryHelper sfh = fromClause.getSessionFactoryHelper();
FromElement destination = null;
String tableAlias = null;
EntityPersister entityPersister = queryableCollection.getElementPersister();
tableAlias = fromClause.getAliasGenerator().createName(entityPersister.getEntityName());
String associatedEntityName = entityPersister.getEntityName();
EntityPersister targetEntityPersister = sfh.requireClassPersister(associatedEntityName);
// Create the FROM element for the target (the elements of the collection).
destination = createAndAddFromElement(associatedEntityName, classAlias, targetEntityPersister, (EntityType) queryableCollection.getElementType(), tableAlias);
// If the join is implied, then don't include sub-classes on the element.
if (implied) {
destination.setIncludeSubclasses(false);
}
fromClause.addCollectionJoinFromElementByPath(path, destination);
// origin.addDestination(destination);
// Add the query spaces.
fromClause.getWalker().addQuerySpaces(entityPersister.getQuerySpaces());
CollectionType type = queryableCollection.getCollectionType();
String role = type.getRole();
String roleAlias = origin.getTableAlias();
String[] targetColumns = sfh.getCollectionElementColumns(role, roleAlias);
AssociationType elementAssociationType = sfh.getElementAssociationType(type);
// Create the join element under the from element.
JoinType joinType = JoinType.INNER_JOIN;
JoinSequence joinSequence = sfh.createJoinSequence(implied, elementAssociationType, tableAlias, joinType, targetColumns);
elem = initializeJoin(path, destination, joinSequence, targetColumns, origin, false);
// The associated entity is implied, but it must be included in the FROM.
elem.setUseFromFragment(true);
// The collection alias is the role.
elem.setCollectionTableAlias(roleAlias);
return elem;
}
use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.
the class FromElementFactory method createEntityAssociation.
private FromElement createEntityAssociation(String role, String roleAlias, JoinType joinType) throws SemanticException {
FromElement elem;
Queryable entityPersister = (Queryable) queryableCollection.getElementPersister();
String associatedEntityName = entityPersister.getEntityName();
// Get the class name of the associated entity.
if (queryableCollection.isOneToMany()) {
LOG.debugf("createEntityAssociation() : One to many - path = %s role = %s associatedEntityName = %s", path, role, associatedEntityName);
JoinSequence joinSequence = createJoinSequence(roleAlias, joinType);
elem = createJoin(associatedEntityName, roleAlias, joinSequence, (EntityType) queryableCollection.getElementType(), false);
} else {
LOG.debugf("createManyToMany() : path = %s role = %s associatedEntityName = %s", path, role, associatedEntityName);
elem = createManyToMany(role, associatedEntityName, roleAlias, entityPersister, (EntityType) queryableCollection.getElementType(), joinType);
fromClause.getWalker().addQuerySpaces(queryableCollection.getCollectionSpaces());
}
elem.setCollectionTableAlias(roleAlias);
return elem;
}
use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.
the class FromElementFactory method createCollection.
public FromElement createCollection(QueryableCollection queryableCollection, String role, JoinType joinType, boolean fetchFlag, boolean indexed) throws SemanticException {
if (!collection) {
throw new IllegalStateException("FromElementFactory not initialized for collections!");
}
this.inElementsFunction = indexed;
FromElement elem;
this.queryableCollection = queryableCollection;
collectionType = queryableCollection.getCollectionType();
String roleAlias = fromClause.getAliasGenerator().createName(role);
// Correlated subqueries create 'special' implied from nodes
// because correlated subselects can't use an ANSI-style join
boolean explicitSubqueryFromElement = fromClause.isSubQuery() && !implied;
if (explicitSubqueryFromElement) {
String pathRoot = StringHelper.root(path);
FromElement origin = fromClause.getFromElement(pathRoot);
if (origin == null || origin.getFromClause() != fromClause) {
implied = true;
}
}
// super-duper-classic-parser-regression-testing-mojo-magic...
if (explicitSubqueryFromElement && DotNode.useThetaStyleImplicitJoins) {
implied = true;
}
Type elementType = queryableCollection.getElementType();
if (elementType.isEntityType()) {
// A collection of entities...
elem = createEntityAssociation(role, roleAlias, joinType);
} else if (elementType.isComponentType()) {
// A collection of components...
JoinSequence joinSequence = createJoinSequence(roleAlias, joinType);
elem = createCollectionJoin(joinSequence, roleAlias);
} else {
// A collection of scalar elements...
JoinSequence joinSequence = createJoinSequence(roleAlias, joinType);
elem = createCollectionJoin(joinSequence, roleAlias);
}
elem.setRole(role);
elem.setQueryableCollection(queryableCollection);
// Don't include sub-classes for implied collection joins or subquery joins.
if (implied) {
elem.setIncludeSubclasses(false);
}
if (explicitSubqueryFromElement) {
// Treat explict from elements in sub-queries properly.
elem.setInProjectionList(true);
}
if (fetchFlag) {
elem.setFetch(true);
}
return elem;
}
Aggregations