use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class SessionImpl method getFilterQueryPlan.
private FilterQueryPlan getFilterQueryPlan(Object collection, String filter, QueryParameters parameters, boolean shallow) throws HibernateException {
if (collection == null) {
throw new NullPointerException("null collection passed to filter");
}
CollectionEntry entry = persistenceContext.getCollectionEntryOrNull(collection);
final CollectionPersister roleBeforeFlush = (entry == null) ? null : entry.getLoadedPersister();
FilterQueryPlan plan = null;
if (roleBeforeFlush == null) {
// if it was previously unreferenced, we need to flush in order to
// get its state into the database in order to execute query
flush();
entry = persistenceContext.getCollectionEntryOrNull(collection);
CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
if (roleAfterFlush == null) {
throw new QueryException("The collection was unreferenced");
}
plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleAfterFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
} else {
// otherwise, we only need to flush if there are in-memory changes
// to the queried tables
plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleBeforeFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
if (autoFlushIfRequired(plan.getQuerySpaces())) {
// might need to run a different filter entirely afterQuery the flush
// because the collection role may have changed
entry = persistenceContext.getCollectionEntryOrNull(collection);
CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
if (roleBeforeFlush != roleAfterFlush) {
if (roleAfterFlush == null) {
throw new QueryException("The collection was dereferenced");
}
plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleAfterFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
}
}
}
if (parameters != null) {
parameters.getPositionalParameterValues()[0] = entry.getLoadedKey();
parameters.getPositionalParameterTypes()[0] = entry.getLoadedPersister().getKeyType();
}
return plan;
}
use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class BasicLoader method postInstantiate.
protected void postInstantiate() {
Loadable[] persisters = getEntityPersisters();
String[] suffixes = getSuffixes();
descriptors = new EntityAliases[persisters.length];
for (int i = 0; i < descriptors.length; i++) {
descriptors[i] = new DefaultEntityAliases(persisters[i], suffixes[i]);
}
CollectionPersister[] collectionPersisters = getCollectionPersisters();
List bagRoles = null;
if (collectionPersisters != null) {
String[] collectionSuffixes = getCollectionSuffixes();
collectionDescriptors = new CollectionAliases[collectionPersisters.length];
for (int i = 0; i < collectionPersisters.length; i++) {
if (isBag(collectionPersisters[i])) {
if (bagRoles == null) {
bagRoles = new ArrayList();
}
bagRoles.add(collectionPersisters[i].getRole());
}
collectionDescriptors[i] = new GeneratedCollectionAliases(collectionPersisters[i], collectionSuffixes[i]);
}
} else {
collectionDescriptors = null;
}
if (bagRoles != null && bagRoles.size() > 1) {
throw new MultipleBagFetchException(bagRoles);
}
}
use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class AbstractCollectionReference method buildElementGraph.
private CollectionFetchableElement buildElementGraph() {
final CollectionPersister persister = collectionQuerySpace.getCollectionPersister();
final Type type = persister.getElementType();
if (type.isAssociationType()) {
if (type.isEntityType()) {
final EntityPersister elementPersister = persister.getFactory().getEntityPersister(((EntityType) type).getAssociatedEntityName());
final ExpandingEntityQuerySpace entityQuerySpace = QuerySpaceHelper.INSTANCE.makeEntityQuerySpace(collectionQuerySpace, elementPersister, CollectionPropertyNames.COLLECTION_ELEMENTS, (EntityType) persister.getElementType(), collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(), collectionQuerySpace.canJoinsBeRequired(), allowElementJoin);
return new CollectionFetchableElementEntityGraph(this, entityQuerySpace);
} else if (type.isAnyType()) {
return new CollectionFetchableElementAnyGraph(this);
}
} else if (type.isComponentType()) {
final ExpandingCompositeQuerySpace compositeQuerySpace = QuerySpaceHelper.INSTANCE.makeCompositeQuerySpace(collectionQuerySpace, new CompositePropertyMapping((CompositeType) persister.getElementType(), (PropertyMapping) persister, ""), CollectionPropertyNames.COLLECTION_ELEMENTS, (CompositeType) persister.getElementType(), collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(), collectionQuerySpace.canJoinsBeRequired(), allowElementJoin);
return new CollectionFetchableElementCompositeGraph(this, compositeQuerySpace);
}
return null;
}
use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class CriteriaJoinWalker method generateTableAlias.
@Override
protected String generateTableAlias(int n, PropertyPath path, Joinable joinable) {
// TODO: deal with side-effects (changes to includeInResultRowList, userAliasList, resultTypeList)!!!
// for collection-of-entity, we are called twice for given "path"
// once for the collection Joinable, once for the entity Joinable.
// the second call will/must "consume" the alias + perform side effects according to consumesEntityAlias()
// for collection-of-other, however, there is only one call
// it must "consume" the alias + perform side effects, despite what consumeEntityAlias() return says
//
// note: the logic for adding to the userAliasList is still strictly based on consumesEntityAlias return value
boolean checkForSqlAlias = joinable.consumesEntityAlias();
if (!checkForSqlAlias && joinable.isCollection()) {
// is it a collection-of-other (component or value) ?
CollectionPersister collectionPersister = (CollectionPersister) joinable;
Type elementType = collectionPersister.getElementType();
if (elementType.isComponentType() || !elementType.isEntityType()) {
checkForSqlAlias = true;
}
}
String sqlAlias = null;
if (checkForSqlAlias) {
final Criteria subcriteria = translator.getCriteria(path.getFullPath());
sqlAlias = subcriteria == null ? null : translator.getSQLAlias(subcriteria);
if (joinable.consumesEntityAlias() && !translator.hasProjection()) {
includeInResultRowList.add(subcriteria != null && subcriteria.getAlias() != null);
if (sqlAlias != null) {
if (subcriteria.getAlias() != null) {
userAliasList.add(subcriteria.getAlias());
resultTypeList.add(translator.getResultType(subcriteria));
}
}
}
}
if (sqlAlias == null) {
sqlAlias = super.generateTableAlias(n + translator.getSQLAliasCount(), path, joinable);
}
return sqlAlias;
}
use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.
the class QuerySpaceHelper method makeCollectionQuerySpace.
public ExpandingCollectionQuerySpace makeCollectionQuerySpace(ExpandingQuerySpace lhsQuerySpace, AssociationAttributeDefinition attributeDefinition, String querySpaceUid, FetchStrategy fetchStrategy) {
final CollectionType fetchedType = (CollectionType) attributeDefinition.getType();
final CollectionPersister fetchedPersister = attributeDefinition.toCollectionDefinition().getCollectionPersister();
if (fetchedPersister == null) {
throw new WalkingException(String.format("Unable to locate CollectionPersister [%s] for fetch [%s]", fetchedType.getRole(), attributeDefinition.getName()));
}
final boolean required = lhsQuerySpace.canJoinsBeRequired() && !attributeDefinition.isNullable();
final ExpandingCollectionQuerySpace rhs = lhsQuerySpace.getExpandingQuerySpaces().makeCollectionQuerySpace(querySpaceUid, fetchedPersister, required);
if (shouldIncludeJoin(fetchStrategy)) {
final JoinDefinedByMetadata join = JoinHelper.INSTANCE.createCollectionJoin(lhsQuerySpace, attributeDefinition.getName(), rhs, required, (CollectionType) attributeDefinition.getType(), fetchedPersister.getFactory());
lhsQuerySpace.addJoin(join);
}
return rhs;
}
Aggregations