@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() ); }
final int currentDepth) throws MappingException { if ( persister.isOneToMany() ) { walkEntityTree( (OuterJoinLoadable) persister.getElementPersister(), alias, path, Type type = persister.getElementType(); if ( type.isAssociationType() ) { String[] aliasedLhsColumns = persister.getElementColumnNames( alias ); String[] lhsColumns = persister.getElementColumnNames(); final JoinType joinType = getJoinType( associationType, persister.getFetchMode(), path, persister.getTableName(), lhsColumns, !useInnerJoin, walkCompositeElementTree( (CompositeType) type, persister.getElementColumnNames(), persister, alias,
private void prepareForIndex(QueryTranslatorImpl q) throws QueryException { QueryableCollection collPersister = q.getCollectionPersister( collectionRole ); if ( !collPersister.hasIndex() ) { throw new QueryException( "unindexed collection before []: " + 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() ); } joinSequence.addCondition( collectionName + '.' + indexCols[0] + " = " ); //TODO: get SQL rendering out of here 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 ); }
@Override protected boolean isDuplicateAssociation( final String foreignKeyTable, final String[] foreignKeyColumns) { //disable a join back to this same association final boolean isSameJoin = oneToManyPersister.getTableName().equals( foreignKeyTable ) && Arrays.equals( foreignKeyColumns, oneToManyPersister.getKeyColumnNames() ); return isSameJoin || super.isDuplicateAssociation( foreignKeyTable, foreignKeyColumns ); }
@Override @SuppressWarnings("unchecked") protected Type resolveType(QueryableCollection collectionPersister) { final Type keyType = collectionPersister.getIndexType(); final Type valueType = collectionPersister.getElementType(); types.add( keyType ); types.add( valueType ); mapEntryBuilder = new MapEntryBuilder(); // an entry (as an aggregated select expression) does not have a type... return null; }
public String[] toAliasedColumns(String alias, String propertyName) { final QueryableCollection queryableCollection = (QueryableCollection) persister; if ( propertyName.equals( CollectionPropertyNames.COLLECTION_ELEMENTS ) ) { return queryableCollection.getElementColumnNames( alias ); } else if ( propertyName.equals( CollectionPropertyNames.COLLECTION_INDICES ) ) { return queryableCollection.getIndexColumnNames( alias ); } else { throw new IllegalArgumentException( String.format( "Collection propertyName must be either %s or %s; instead it was %s.", CollectionPropertyNames.COLLECTION_ELEMENTS, CollectionPropertyNames.COLLECTION_INDICES, propertyName ) ); } }
collectionPersister.getKeyColumnNames(), subquery, batchSize String filter = collectionPersister.filterFragment( alias, getLoadQueryInfluencers().getEnabledFilters() ); if ( collectionPersister.isManyToMany() ) { AssociationType associationType = ( AssociationType ) collectionPersister.getElementType(); while ( itr.hasNext() ) { OuterJoinableAssociation oja = ( OuterJoinableAssociation ) itr.next(); if ( oja.getJoinableType() == associationType ) { filter += collectionPersister.getManyToManyFilterFragment( oja.getRHSAlias(), getLoadQueryInfluencers().getEnabledFilters() ); manyToManyOrderBy += collectionPersister.getManyToManyOrderByString( oja.getRHSAlias() ); Select select = new Select( getDialect() ) .setSelectClause( collectionPersister.selectFragment(alias, collectionSuffixes[0] ) + selectString(associations) .setFromClause( collectionPersister.getTableName(), alias ) .setWhereClause( whereString.toString() ) .setOuterJoins( select.setOrderByClause( orderBy( associations, mergeOrderings( collectionPersister.getSQLOrderByString(alias), manyToManyOrderBy ) ) );
private JoinTable createJoinTable(EntityType<?> ownerType, QueryableCollection queryableCollection, Map<String, String> targetColumnMapping, Set<String> targetIdAttributeNames, String attributeName) { String[] indexColumnNames = queryableCollection.getIndexColumnNames(); Map<String, String> keyColumnMapping = null; Map<String, String> keyColumnTypes = null; keyColumnMapping = new HashMap<>(indexColumnNames.length); keyColumnTypes = new HashMap<>(indexColumnNames.length); if (queryableCollection.getKeyType().isEntityType()) { throw new IllegalArgumentException("Determining the join table key foreign key mappings is not yet supported!"); } else { keyColumnMapping.put(indexColumnNames[0], indexColumnNames[0]); keyColumnTypes.put(indexColumnNames[0], getColumnTypeForPropertyType(ownerType, attributeName, queryableCollection.getFactory(), queryableCollection.getIndexType())[0]); AbstractEntityPersister ownerEntityPersister = (AbstractEntityPersister) queryableCollection.getOwnerEntityPersister(); String[] primaryKeyColumnMetaData = ownerEntityPersister.getKeyColumnNames(); String[] foreignKeyColumnMetaData = queryableCollection.getKeyColumnNames(); Map<String, String> idColumnMapping = new HashMap<>(primaryKeyColumnMetaData.length); for (int i = 0; i < foreignKeyColumnMetaData.length; i++) { Type elementType = queryableCollection.getElementType(); if (elementType instanceof ComponentType) { targetIdAttributeNames = new HashSet<>(); collectPropertyNames(targetIdAttributeNames, null, elementType, queryableCollection.getFactory()); queryableCollection.getTableName(), idAttributeNames, idColumnMapping,
if ( queryableCollection.isManyToMany() && queryableCollection.hasIndex() && SPECIAL_MANY2MANY_TREATMENT_FUNCTION_NAMES.contains( propertyName ) ) { return new SpecialManyToManyCollectionPropertyMapping(); if ( queryableCollection.getElementType().isAnyType() ) { if ( queryableCollection.getElementType().isComponentType() ) { return (PropertyMapping) queryableCollection.getOwnerEntityPersister();
/** * Get sub-attribute definitions for a composite collection element. * @param compositionElementDefinition - composite collection element definition. * @return sub-attribute definitions for a composite collection element. */ public static Iterable<AttributeDefinition> getCompositeCollectionElementSubAttributes( CompositeCollectionElementDefinition compositionElementDefinition) { final QueryableCollection collectionPersister = (QueryableCollection) compositionElementDefinition.getCollectionDefinition().getCollectionPersister(); return getSingularSubAttributes( compositionElementDefinition.getSource(), (OuterJoinLoadable) collectionPersister.getOwnerEntityPersister(), (CompositeType) collectionPersister.getElementType(), collectionPersister.getTableName(), collectionPersister.getElementColumnNames() ); }
public CollectionElementLoader( QueryableCollection collectionPersister, SessionFactoryImplementor factory, LoadQueryInfluencers loadQueryInfluencers) throws MappingException { super( factory, loadQueryInfluencers ); this.keyType = collectionPersister.getKeyType(); this.indexType = collectionPersister.getIndexType(); this.persister = (OuterJoinLoadable) collectionPersister.getElementPersister(); this.entityName = persister.getEntityName(); JoinWalker walker = new EntityJoinWalker( persister, ArrayHelper.join( collectionPersister.getKeyColumnNames(), collectionPersister.toColumns( "index" ) ), 1, LockMode.NONE, factory, loadQueryInfluencers ); initFromWalker( walker ); postInstantiate(); if ( LOG.isDebugEnabled() ) { LOG.debugf( "Static select for entity %s: %s", entityName, getSQLString() ); } }
Type collectionElementType = persister.getElementType(); if ( !collectionElementType.isEntityType() ) { throw new QueryException( "collection of values in filter: " + elementName ); String[] keyColumnNames = persister.getKeyColumnNames(); collectionName = persister.isOneToMany() ? elementName : createNameForCollection( collectionRole ); join.setRoot( persister, collectionName ); if ( !persister.isOneToMany() ) { (AssociationType) persister.getElementType(), elementName, JoinType.INNER_JOIN, persister.getElementColumnNames( collectionName ) );
public static Iterable<AttributeDefinition> getCompositeCollectionIndexSubAttributes(CompositeCollectionElementDefinition compositionElementDefinition){ final QueryableCollection collectionPersister = (QueryableCollection) compositionElementDefinition.getCollectionDefinition().getCollectionPersister(); return getSingularSubAttributes( compositionElementDefinition.getSource(), (OuterJoinLoadable) collectionPersister.getOwnerEntityPersister(), (CompositeType) collectionPersister.getIndexType(), collectionPersister.getTableName(), collectionPersister.toColumns( "index" ) ); }
public String[] toColumns(String alias, String propertyName) throws QueryException { if ( propertyName.equals(CollectionPropertyNames.COLLECTION_ELEMENTS) ) { return memberPersister.getElementColumnNames(alias); if ( !memberPersister.hasIndex() ) { throw new QueryException("unindexed collection in indices()"); return memberPersister.getIndexColumnNames(alias); String[] cols = memberPersister.getKeyColumnNames(); return new String[] { "count(" + alias + '.' + cols[0] + ')' }; if ( !memberPersister.hasIndex() ) { throw new QueryException("unindexed collection in maxIndex()"); String[] cols = memberPersister.getIndexColumnNames(alias); if ( cols.length!=1 ) { throw new QueryException("composite collection index in maxIndex()"); if ( !memberPersister.hasIndex() ) { throw new QueryException("unindexed collection in minIndex()"); String[] cols = memberPersister.getIndexColumnNames(alias); if ( cols.length!=1 ) { throw new QueryException("composite collection index in minIndex()"); String[] cols = memberPersister.getElementColumnNames(alias); if ( cols.length!=1 ) { throw new QueryException("composite collection element in maxElement()");
oneToManyPersister.getKeyColumnNames(), subquery, batchSize ); String filter = oneToManyPersister.filterFragment( alias, getLoadQueryInfluencers().getEnabledFilters() ); whereString.insert( 0, StringHelper.moveAndToBeginning( filter ) ); Select select = new Select( getDialect() ) .setSelectClause( oneToManyPersister.selectFragment( null, null, ); select.setOrderByClause( orderBy( associations, oneToManyPersister.getSQLOrderByString( alias ) ) ); select.setComment( "load one-to-many " + oneToManyPersister.getRole() );
JoinType joinType) throws SemanticException { FromElement elem; Queryable entityPersister = (Queryable) queryableCollection.getElementPersister(); String associatedEntityName = entityPersister.getEntityName(); if ( queryableCollection.isOneToMany() ) { LOG.debugf( "createEntityAssociation() : One to many - path = %s role = %s associatedEntityName = %s", roleAlias, joinSequence, (EntityType) queryableCollection.getElementType(), false ); elem = createManyToMany( role, associatedEntityName, roleAlias, entityPersister, (EntityType) queryableCollection.getElementType(), joinType ); fromClause.getWalker().addQuerySpaces( queryableCollection.getCollectionSpaces() );
@Override public String getAssociatedEntityName(SessionFactoryImplementor factory) throws MappingException { try { QueryableCollection collectionPersister = (QueryableCollection) factory .getCollectionPersister( role ); if ( !collectionPersister.getElementType().isEntityType() ) { throw new MappingException( "collection was not an association: " + collectionPersister.getRole() ); } return collectionPersister.getElementPersister().getEntityName(); } catch (ClassCastException cce) { throw new MappingException( "collection role is not queryable " + role ); } }
private void determineKeySelectExpressions(QueryableCollection collectionPersister, List selections) { AliasGenerator aliasGenerator = new LocalAliasGenerator( 0 ); appendSelectExpressions( collectionPersister.getIndexColumnNames(), selections, aliasGenerator ); Type keyType = collectionPersister.getIndexType(); if ( keyType.isEntityType() ) { MapKeyEntityFromElement mapKeyEntityFromElement = findOrAddMapKeyEntityFromElement( collectionPersister ); Queryable keyEntityPersister = mapKeyEntityFromElement.getQueryable(); SelectFragment fragment = keyEntityPersister.propertySelectFragmentFragment( mapKeyEntityFromElement.getTableAlias(), null, false ); appendSelectExpressions( fragment, selections, aliasGenerator ); } }
if ( !queryableCollection.hasIndex() ) { throw new QueryException( "unindexed fromElement before []: " + collectionNode.getPath() ); String[] indexCols = queryableCollection.getIndexColumnNames(); if ( indexCols.length != 1 ) { throw new QueryException( "composite-index appears in []: " + collectionNode.getPath() ); case 1: ParameterSpecification paramSpec = paramSpecs.get( 0 ); paramSpec.setExpectedType( queryableCollection.getIndexType() ); fromElement.setIndexCollectionSelectorParamSpec( paramSpec ); break; String[] elementColumns = queryableCollection.getElementColumnNames( elementTable ); setText( elementColumns[0] ); setResolved();
public Type toType(String propertyName) throws QueryException { if ( propertyName.equals(CollectionPropertyNames.COLLECTION_ELEMENTS) ) { return memberPersister.getElementType(); } else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_INDICES) ) { if ( !memberPersister.hasIndex() ) { throw new QueryException("unindexed collection before indices()"); } return memberPersister.getIndexType(); } else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_SIZE) ) { return StandardBasicTypes.INTEGER; } else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MAX_INDEX) ) { return memberPersister.getIndexType(); } else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MIN_INDEX) ) { return memberPersister.getIndexType(); } else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MAX_ELEMENT) ) { return memberPersister.getElementType(); } else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MIN_ELEMENT) ) { return memberPersister.getElementType(); } else { //return memberPersister.getPropertyType(propertyName); throw new QueryException("illegal syntax near collection: " + propertyName); } }