use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class JoinWalker method orderBy.
/**
* Get the order by string required for collection fetching
*/
protected static String orderBy(List associations) throws MappingException {
StringBuilder buf = new StringBuilder();
Iterator iter = associations.iterator();
OuterJoinableAssociation last = null;
while (iter.hasNext()) {
OuterJoinableAssociation oj = (OuterJoinableAssociation) iter.next();
if (oj.getJoinType() == JoinType.LEFT_OUTER_JOIN) {
// why does this matter?
if (oj.getJoinable().isCollection()) {
final QueryableCollection queryableCollection = (QueryableCollection) oj.getJoinable();
if (queryableCollection.hasOrdering()) {
final String orderByString = queryableCollection.getSQLOrderByString(oj.getRHSAlias());
buf.append(orderByString).append(", ");
}
} else {
// many-to-many defined order-by...
if (last != null && last.getJoinable().isCollection()) {
final QueryableCollection queryableCollection = (QueryableCollection) last.getJoinable();
if (queryableCollection.isManyToMany() && last.isManyToManyWith(oj)) {
if (queryableCollection.hasManyToManyOrdering()) {
final String orderByString = queryableCollection.getManyToManyOrderByString(oj.getRHSAlias());
buf.append(orderByString).append(", ");
}
}
}
}
}
last = oj;
}
if (buf.length() > 0) {
buf.setLength(buf.length() - 2);
}
return buf.toString();
}
use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class JoinWalker method initPersisters.
protected void initPersisters(final List associations, final LockOptions lockOptions, final AssociationInitCallback callback) throws MappingException {
final int joins = countEntityPersisters(associations);
final int collections = countCollectionPersisters(associations);
collectionOwners = collections == 0 ? null : new int[collections];
collectionPersisters = collections == 0 ? null : new CollectionPersister[collections];
collectionSuffixes = BasicLoader.generateSuffixes(joins + 1, collections);
this.lockOptions = lockOptions;
persisters = new Loadable[joins];
aliases = new String[joins];
owners = new int[joins];
ownerAssociationTypes = new EntityType[joins];
lockModeArray = ArrayHelper.fillArray(lockOptions.getLockMode(), joins);
int i = 0;
int j = 0;
for (Object association : associations) {
final OuterJoinableAssociation oj = (OuterJoinableAssociation) association;
if (!oj.isCollection()) {
persisters[i] = (Loadable) oj.getJoinable();
aliases[i] = oj.getRHSAlias();
owners[i] = oj.getOwner(associations);
ownerAssociationTypes[i] = (EntityType) oj.getJoinableType();
callback.associationProcessed(oj, i);
i++;
} else {
QueryableCollection collPersister = (QueryableCollection) oj.getJoinable();
if (oj.getJoinType() == JoinType.LEFT_OUTER_JOIN && !oj.hasRestriction()) {
//it must be a collection fetch
collectionPersisters[j] = collPersister;
collectionOwners[j] = oj.getOwner(associations);
j++;
}
if (collPersister.isOneToMany()) {
persisters[i] = (Loadable) collPersister.getElementPersister();
aliases[i] = oj.getRHSAlias();
callback.associationProcessed(oj, i);
i++;
}
}
}
if (ArrayHelper.isAllNegative(owners)) {
owners = null;
}
if (collectionOwners != null && ArrayHelper.isAllNegative(collectionOwners)) {
collectionOwners = null;
}
}
use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class SizeExpression method toSqlString.
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
final String entityName = criteriaQuery.getEntityName(criteria, propertyName);
final String role = entityName + '.' + criteriaQuery.getPropertyName(propertyName);
final QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory().getCollectionPersister(role);
final String[] fk = cp.getKeyColumnNames();
final String[] pk = ((Loadable) cp.getOwnerEntityPersister()).getIdentifierColumnNames();
final ConditionFragment subQueryRestriction = new ConditionFragment().setTableAlias(criteriaQuery.getSQLAlias(criteria, propertyName)).setCondition(pk, fk);
return String.format(Locale.ROOT, "? %s (select count(*) from %s where %s)", op, cp.getTableName(), subQueryRestriction.toFragmentString());
}
use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.
the class AbstractEmptinessExpression method toSqlString.
@Override
public final String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
final String entityName = criteriaQuery.getEntityName(criteria, propertyName);
final String actualPropertyName = criteriaQuery.getPropertyName(propertyName);
final String sqlAlias = criteriaQuery.getSQLAlias(criteria, propertyName);
final SessionFactoryImplementor factory = criteriaQuery.getFactory();
final QueryableCollection collectionPersister = getQueryableCollection(entityName, actualPropertyName, factory);
final String[] collectionKeys = collectionPersister.getKeyColumnNames();
final String[] ownerKeys = ((Loadable) factory.getEntityPersister(entityName)).getIdentifierColumnNames();
final String innerSelect = "(select 1 from " + collectionPersister.getTableName() + " where " + new ConditionFragment().setTableAlias(sqlAlias).setCondition(ownerKeys, collectionKeys).toFragmentString() + ")";
return excludeEmpty() ? "exists " + innerSelect : "not exists " + innerSelect;
}
use of org.hibernate.persister.collection.QueryableCollection 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);
}
Aggregations