use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class DotNode method dereferenceCollection.
private void dereferenceCollection(CollectionType collectionType, boolean implicitJoin, boolean indexed, String classAlias, AST parent) throws SemanticException {
dereferenceType = DereferenceType.COLLECTION;
String role = collectionType.getRole();
//foo.bars.size (also handles deprecated stuff like foo.bars.maxelement for backwardness)
boolean isSizeProperty = getNextSibling() != null && CollectionProperties.isAnyCollectionProperty(getNextSibling().getText());
if (isSizeProperty) {
//yuck!
indexed = true;
}
QueryableCollection queryableCollection = getSessionFactoryHelper().requireQueryableCollection(role);
String propName = getPath();
FromClause currentFromClause = getWalker().getCurrentFromClause();
// If the lhs of the join is a "component join", we need to go back to the
// first non-component-join as the origin to properly link aliases and
// join columns
FromElement lhsFromElement = getLhs().getFromElement();
while (lhsFromElement != null && ComponentJoin.class.isInstance(lhsFromElement)) {
lhsFromElement = lhsFromElement.getOrigin();
}
if (lhsFromElement == null) {
throw new QueryException("Unable to locate appropriate lhs");
}
// in all other cases, we should use the table alias
if (getWalker().getStatementType() != SqlTokenTypes.SELECT) {
if (isFromElementUpdateOrDeleteRoot(lhsFromElement)) {
// at this point we know we have the 2 conditions above,
// lets see if we have the mentioned "multi-table" caveat...
boolean useAlias = false;
if (getWalker().getStatementType() != SqlTokenTypes.INSERT) {
final Queryable persister = lhsFromElement.getQueryable();
if (persister.isMultiTable()) {
useAlias = true;
}
}
if (!useAlias) {
final String lhsTableName = lhsFromElement.getQueryable().getTableName();
columns = getFromElement().toColumns(lhsTableName, propertyPath, false, true);
}
}
}
// We do not look for an existing join on the same path, because
// it makes sense to join twice on the same collection role
FromElementFactory factory = new FromElementFactory(currentFromClause, lhsFromElement, propName, classAlias, getColumns(), implicitJoin);
FromElement elem = factory.createCollection(queryableCollection, role, joinType, fetch, indexed);
LOG.debugf("dereferenceCollection() : Created new FROM element for %s : %s", propName, elem);
setImpliedJoin(elem);
// This 'dot' expression now refers to the resulting from element.
setFromElement(elem);
if (isSizeProperty) {
elem.setText("");
elem.setUseWhereFragment(false);
}
if (!implicitJoin) {
EntityPersister entityPersister = elem.getEntityPersister();
if (entityPersister != null) {
getWalker().addQuerySpaces(entityPersister.getQuerySpaces());
}
}
// Always add the collection's query spaces.
getWalker().addQuerySpaces(queryableCollection.getCollectionSpaces());
}
use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class FromElement method buildTypeDiscriminatorMetadata.
private TypeDiscriminatorMetadata buildTypeDiscriminatorMetadata() {
final String aliasToUse = getTableAlias();
Queryable queryable = getQueryable();
if (queryable == null) {
QueryableCollection collection = getQueryableCollection();
if (!collection.getElementType().isEntityType()) {
throw new QueryException("type discrimination cannot be applied to value collection [" + collection.getRole() + "]");
}
queryable = (Queryable) collection.getElementPersister();
}
handlePropertyBeingDereferenced(getDataType(), DISCRIMINATOR_PROPERTY_NAME);
return new TypeDiscriminatorMetadataImpl(queryable.getTypeDiscriminatorMetadata(), aliasToUse);
}
use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class HqlSqlWalker method validateMapPropertyExpression.
@Override
protected void validateMapPropertyExpression(AST node) throws SemanticException {
try {
FromReferenceNode fromReferenceNode = (FromReferenceNode) node;
QueryableCollection collectionPersister = fromReferenceNode.getFromElement().getQueryableCollection();
if (!Map.class.isAssignableFrom(collectionPersister.getCollectionType().getReturnedClass())) {
throw new SemanticException("node did not reference a map");
}
} catch (SemanticException se) {
throw se;
} catch (Throwable t) {
throw new SemanticException("node did not reference a map");
}
}
use of org.hibernate.persister.collection.QueryableCollection 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.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class AbstractCompositionAttribute method getAttributes.
@Override
public Iterable<AttributeDefinition> getAttributes() {
return new Iterable<AttributeDefinition>() {
@Override
public Iterator<AttributeDefinition> iterator() {
return new Iterator<AttributeDefinition>() {
private final int numberOfAttributes = getType().getSubtypes().length;
private int currentSubAttributeNumber;
private int currentColumnPosition = columnStartPosition;
@Override
public boolean hasNext() {
return currentSubAttributeNumber < numberOfAttributes;
}
@Override
public AttributeDefinition next() {
final int subAttributeNumber = currentSubAttributeNumber;
currentSubAttributeNumber++;
final String name = getType().getPropertyNames()[subAttributeNumber];
final Type type = getType().getSubtypes()[subAttributeNumber];
int columnPosition = currentColumnPosition;
currentColumnPosition += type.getColumnSpan(sessionFactory());
if (type.isAssociationType()) {
// we build the association-key here because of the "goofiness" with 'currentColumnPosition'
final AssociationKey associationKey;
final AssociationType aType = (AssociationType) type;
final Joinable joinable = aType.getAssociatedJoinable(sessionFactory());
if (aType.isAnyType()) {
associationKey = new AssociationKey(JoinHelper.getLHSTableName(aType, attributeNumber(), (OuterJoinLoadable) locateOwningPersister()), JoinHelper.getLHSColumnNames(aType, attributeNumber(), columnPosition, (OuterJoinLoadable) locateOwningPersister(), sessionFactory()));
} else if (aType.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT) {
final String lhsTableName;
final String[] lhsColumnNames;
if (joinable.isCollection()) {
final QueryableCollection collectionPersister = (QueryableCollection) joinable;
lhsTableName = collectionPersister.getTableName();
lhsColumnNames = collectionPersister.getElementColumnNames();
} else {
final OuterJoinLoadable entityPersister = (OuterJoinLoadable) locateOwningPersister();
lhsTableName = getLHSTableName(aType, attributeNumber(), entityPersister);
lhsColumnNames = getLHSColumnNames(aType, attributeNumber(), columnPosition, entityPersister, sessionFactory());
}
associationKey = new AssociationKey(lhsTableName, lhsColumnNames);
} else {
associationKey = new AssociationKey(joinable.getTableName(), getRHSColumnNames(aType, sessionFactory()));
}
final CompositeType cType = getType();
final boolean nullable = cType.getPropertyNullability() == null || cType.getPropertyNullability()[subAttributeNumber];
return new CompositeBasedAssociationAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (AssociationType) type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation(), subAttributeNumber, associationKey);
} else if (type.isComponentType()) {
return new CompositionBasedCompositionAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (CompositeType) type, columnPosition, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(getType().getPropertyNullability()[subAttributeNumber]).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
} else {
final CompositeType cType = getType();
final boolean nullable = cType.getPropertyNullability() == null || cType.getPropertyNullability()[subAttributeNumber];
return new CompositeBasedBasicAttribute(AbstractCompositionAttribute.this, sessionFactory(), subAttributeNumber, name, type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove operation not supported here");
}
};
}
};
}
Aggregations