use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class PathExpressionParser method addFromCollection.
public String addFromCollection(QueryTranslatorImpl q) throws QueryException {
Type collectionElementType = getPropertyType();
if (collectionElementType == null) {
throw new QueryException("must specify 'elements' for collection valued property in from clause: " + path);
}
if (collectionElementType.isEntityType()) {
// an association
QueryableCollection collectionPersister = q.getCollectionPersister(collectionRole);
Queryable entityPersister = (Queryable) collectionPersister.getElementPersister();
String clazz = entityPersister.getEntityName();
final String elementName;
if (collectionPersister.isOneToMany()) {
elementName = collectionName;
//allow index() function:
q.decoratePropertyMapping(elementName, collectionPersister);
} else {
//many-to-many
q.addCollection(collectionName, collectionRole);
elementName = q.createNameFor(clazz);
addJoin(elementName, (AssociationType) collectionElementType);
}
q.addFrom(elementName, clazz, joinSequence);
currentPropertyMapping = new CollectionPropertyMapping(collectionPersister);
return elementName;
} else {
// collections of values
q.addFromCollection(collectionName, collectionRole, joinSequence);
return collectionName;
}
}
use of org.hibernate.persister.collection.QueryableCollection 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.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class MethodNode method handleElements.
private void handleElements(FromReferenceNode collectionNode, String propertyName) {
FromElement collectionFromElement = collectionNode.getFromElement();
QueryableCollection queryableCollection = collectionFromElement.getQueryableCollection();
String path = collectionNode.getPath() + "[]." + propertyName;
LOG.debugf("Creating elements for %s", path);
fromElement = collectionFromElement;
if (!collectionFromElement.isCollectionOfValuesOrComponents()) {
getWalker().addQuerySpaces(queryableCollection.getElementPersister().getQuerySpaces());
}
setDataType(queryableCollection.getElementType());
selectColumns = collectionFromElement.toColumns(fromElement.getTableAlias(), propertyName, inSelect);
}
use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class JoinWalker method addAssociationToJoinTree.
/**
* Add on association (one-to-one, many-to-one, or a collection) to a list
* of associations to be fetched by outerjoin
*/
private void addAssociationToJoinTree(final AssociationType type, final String[] aliasedLhsColumns, final String alias, final PropertyPath path, final int currentDepth, final JoinType joinType) throws MappingException {
Joinable joinable = type.getAssociatedJoinable(getFactory());
// important to generate alias based on size of association collection
// *beforeQuery* adding this join to that collection
String subalias = generateTableAlias(associations.size() + 1, path, joinable);
// NOTE : it should be fine to continue to pass only filters below
// (instead of LoadQueryInfluencers) since "from that point on" we
// only need to worry about restrictions (and not say adding more
// joins)
OuterJoinableAssociation assoc = new OuterJoinableAssociation(path, type, alias, aliasedLhsColumns, subalias, joinType, joinable.consumesEntityAlias() ? getWithClause(path) : "", hasRestriction(path), getFactory(), loadQueryInfluencers.getEnabledFilters());
assoc.validateJoin(path.getFullPath());
associations.add(assoc);
int nextDepth = currentDepth + 1;
// path = "";
if (!joinable.isCollection()) {
if (joinable instanceof OuterJoinLoadable) {
walkEntityTree((OuterJoinLoadable) joinable, subalias, path, nextDepth);
}
} else {
if (joinable instanceof QueryableCollection) {
walkCollectionTree((QueryableCollection) joinable, subalias, path, nextDepth);
}
}
}
use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class AttributeNodeImpl method internalMakeKeySubgraph.
@SuppressWarnings("unchecked")
private <X> SubgraphImpl<X> internalMakeKeySubgraph(Class<X> type) {
if (!attribute.isCollection()) {
throw new IllegalArgumentException(String.format("Non-collection attribute [%s] cannot be target of key subgraph", getAttributeName()));
}
final PluralAttributeImpl pluralAttribute = (PluralAttributeImpl) attribute;
if (pluralAttribute.getCollectionType() != PluralAttribute.CollectionType.MAP) {
throw new IllegalArgumentException(String.format("Non-Map attribute [%s] cannot be target of key subgraph", getAttributeName()));
}
final AssociationType attributeType = (AssociationType) Helper.resolveType(sessionFactory(), attribute);
final QueryableCollection collectionPersister = (QueryableCollection) attributeType.getAssociatedJoinable(sessionFactory());
final Type indexType = collectionPersister.getIndexType();
if (!indexType.isAssociationType()) {
throw new IllegalArgumentException(String.format("Map index [%s] is not an entity; cannot be target of key subgraph", getAttributeName()));
}
if (keySubgraphMap == null) {
keySubgraphMap = new HashMap<>();
}
final AssociationType indexAssociationType = (AssociationType) indexType;
final EntityPersister indexEntityPersister = (EntityPersister) indexAssociationType.getAssociatedJoinable(sessionFactory());
if (type == null) {
type = indexEntityPersister.getMappedClass();
} else {
if (!isTreatableAs(indexEntityPersister, type)) {
throw new IllegalArgumentException(String.format("Map key [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), indexEntityPersister.getMappedClass().getName()));
}
}
final SubgraphImpl<X> subgraph = new SubgraphImpl<>(this.sessionFactory, attribute.getDeclaringType(), type);
keySubgraphMap.put(type, subgraph);
return subgraph;
}
Aggregations