/** * Add on association (one-to-one, many-to-one, or a collection) to a list * of associations to be fetched by outerjoin (if necessary) */ private void addAssociationToJoinTreeIfNecessary( final AssociationType type, final String[] aliasedLhsColumns, final String alias, final PropertyPath path, int currentDepth, final JoinType joinType) throws MappingException { if ( joinType != JoinType.NONE ) { addAssociationToJoinTree( type, aliasedLhsColumns, alias, path, currentDepth, joinType ); } }
/** * Determine the appropriate type of join (if any) to use to fetch the * given association. * * @param persister The owner of the association. * @param path The path to the association * @param propertyNumber The property number representing the association. * @param associationType The association type. * @param metadataFetchMode The metadata-defined fetch mode. * @param metadataCascadeStyle The metadata-defined cascade style. * @param lhsTable The owner table * @param lhsColumns The owner join columns * @param nullable Is the association nullable. * @param currentDepth Current join depth * @return type of join to use ({@link org.hibernate.sql.JoinType#INNER_JOIN}, * {@link org.hibernate.sql.JoinType#LEFT_OUTER_JOIN}, or -1 to indicate no joining. * @throws MappingException ?? */ protected JoinType getJoinType( OuterJoinLoadable persister, final PropertyPath path, int propertyNumber, AssociationType associationType, FetchMode metadataFetchMode, CascadeStyle metadataCascadeStyle, String lhsTable, String[] lhsColumns, final boolean nullable, final int currentDepth) throws MappingException { return getJoinType( associationType, metadataFetchMode, path, lhsTable, lhsColumns, nullable, currentDepth, metadataCascadeStyle ); }
/** * Determine the appropriate associationType of join (if any) to use to fetch the * given association. * * @param associationType The association associationType. * @param config The metadata-defined fetch mode. * @param path The path to the association * @param lhsTable The owner table * @param lhsColumns The owner join columns * @param nullable Is the association nullable. * @param currentDepth Current join depth * @param cascadeStyle The metadata-defined cascade style. * @return type of join to use ({@link org.hibernate.sql.JoinType#INNER_JOIN}, * {@link org.hibernate.sql.JoinType#LEFT_OUTER_JOIN}, or -1 to indicate no joining. * @throws MappingException ?? */ protected JoinType getJoinType( AssociationType associationType, FetchMode config, PropertyPath path, String lhsTable, String[] lhsColumns, boolean nullable, int currentDepth, CascadeStyle cascadeStyle) throws MappingException { if ( !isJoinedFetchEnabled( associationType, config, cascadeStyle ) ) { return JoinType.NONE; } if ( isTooDeep(currentDepth) || ( associationType.isCollectionType() && isTooManyCollections() ) ) { return JoinType.NONE; } if ( isDuplicateAssociation( lhsTable, lhsColumns, associationType ) ) { return JoinType.NONE; } return getJoinType( nullable, currentDepth ); }
/** * Does the mapping, and Hibernate default semantics, specify that * this association should be fetched by outer joining */ protected boolean isJoinedFetchEnabledInMapping(FetchMode config, AssociationType type) throws MappingException { if ( !type.isEntityType() && !type.isCollectionType() ) { return false; } else { if (config==FetchMode.JOIN) return true; if (config==FetchMode.SELECT) return false; if ( type.isEntityType() ) { //TODO: look at the owning property and check that it // isn't lazy (by instrumentation) EntityType entityType =(EntityType) type; EntityPersister persister = getFactory().getEntityPersister( entityType.getAssociatedEntityName() ); return !persister.hasProxy(); } else { return false; } } }
/** * Should we join this association? */ protected boolean isJoinable( final JoinType joinType, final Set visitedAssociationKeys, final String lhsTable, final String[] lhsColumnNames, final AssociationType type, final int depth) { if ( joinType == JoinType.NONE ) { return false; } if ( joinType == JoinType.INNER_JOIN ) { return true; } Integer maxFetchDepth = getFactory().getSettings().getMaximumFetchDepth(); final boolean tooDeep = maxFetchDepth!=null && depth >= maxFetchDepth; return !tooDeep && !isDuplicateAssociation(lhsTable, lhsColumnNames, type); }
@Override protected JoinType getJoinType( AssociationType associationType, FetchMode config, PropertyPath path, String lhsTable, String[] lhsColumns, boolean nullable, int currentDepth, CascadeStyle cascadeStyle) throws MappingException { return getJoinType( null, path, -1, associationType, config, cascadeStyle, lhsTable, lhsColumns, nullable, currentDepth ); }
public static OuterJoinableAssociation createRoot( AssociationType joinableType, String alias, SessionFactoryImplementor factory) { return new OuterJoinableAssociation( new PropertyPath(), joinableType, null, null, alias, JoinType.LEFT_OUTER_JOIN, null, false, factory, Collections.EMPTY_MAP ); }
public OuterJoinableAssociation( PropertyPath propertyPath, AssociationType joinableType, String lhsAlias, String[] lhsColumns, String rhsAlias, JoinType joinType, String withClause, boolean hasRestriction, SessionFactoryImplementor factory, Map enabledFilters) throws MappingException { this.propertyPath = propertyPath; this.joinableType = joinableType; this.lhsAlias = lhsAlias; this.lhsColumns = lhsColumns; this.rhsAlias = rhsAlias; this.joinType = joinType; this.joinable = joinableType.getAssociatedJoinable(factory); this.rhsColumns = JoinHelper.getRHSColumnNames(joinableType, factory); this.on = joinableType.getOnCondition(rhsAlias, factory, enabledFilters) + ( withClause == null || withClause.trim().length() == 0 ? "" : " and ( " + withClause + " )" ); this.hasRestriction = hasRestriction; this.enabledFilters = enabledFilters; // needed later for many-to-many/filter application }
private String resolveAdditionalJoinCondition(String rhsTableAlias, String withClause, Joinable joinable, AssociationType associationType) { // turns out that the call to AssociationType#getOnCondition in the initial code really just translates to // calls to the Joinable.filterFragment() method where the Joinable is either the entity or // collection persister final String filter = associationType!=null? associationType.getOnCondition( rhsTableAlias, factory, buildingParameters.getQueryInfluencers().getEnabledFilters() ): joinable.filterFragment( rhsTableAlias, buildingParameters.getQueryInfluencers().getEnabledFilters() ); if ( StringHelper.isEmpty( withClause ) && StringHelper.isEmpty( filter ) ) { return ""; } else if ( StringHelper.isNotEmpty( withClause ) && StringHelper.isNotEmpty( filter ) ) { return filter + " and " + withClause; } else { // only one is non-empty... return StringHelper.isNotEmpty( filter ) ? filter : withClause; } }
/** * Warning: * When there are duplicated property names in the subclasses * of the class, this method may return the wrong table * number for the duplicated subclass property (note that * SingleTableEntityPersister defines an overloaded form * which takes the entity name. */ public int getSubclassPropertyTableNumber(String propertyPath) { String rootPropertyName = StringHelper.root(propertyPath); Type type = propertyMapping.toType(rootPropertyName); if ( type.isAssociationType() ) { AssociationType assocType = ( AssociationType ) type; if ( assocType.useLHSPrimaryKey() ) { // performance op to avoid the array search return 0; } else if ( type.isCollectionType() ) { // properly handle property-ref-based associations rootPropertyName = assocType.getLHSPropertyName(); } } //Enable for HHH-440, which we don't like: /*if ( type.isComponentType() && !propertyName.equals(rootPropertyName) ) { String unrooted = StringHelper.unroot(propertyName); int idx = ArrayHelper.indexOf( getSubclassColumnClosure(), unrooted ); if ( idx != -1 ) { return getSubclassColumnTableNumberClosure()[idx]; } }*/ int index = ArrayHelper.indexOf( getSubclassPropertyNameClosure(), rootPropertyName); //TODO: optimize this better! return index==-1 ? 0 : getSubclassPropertyTableNumber(index); }
public String selectFragment( Joinable rhs, String rhsAlias, String lhsAlias, String entitySuffix, String collectionSuffix, boolean includeCollectionColumns) { // we need to determine the best way to know that two joinables // represent a single many-to-many... if ( rhs != null && isManyToMany() && !rhs.isCollection() ) { AssociationType elementType = ( ( AssociationType ) getElementType() ); if ( rhs.equals( elementType.getAssociatedJoinable( getFactory() ) ) ) { return manyToManySelectFragment( rhs, rhsAlias, lhsAlias, collectionSuffix ); } } return includeCollectionColumns ? selectFragment( lhsAlias, collectionSuffix ) : ""; }
public static FetchTiming determineFetchTiming( FetchStyle style, AssociationType type, SessionFactoryImplementor sessionFactory) { switch ( style ) { case JOIN: { return FetchTiming.IMMEDIATE; } case BATCH: case SUBSELECT: { return FetchTiming.DELAYED; } default: { // SELECT case, can be either return isSubsequentSelectDelayed( type, sessionFactory ) ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE; } } }
private static NonIdentifierAttributeNature decode(Type type) { if ( type.isAssociationType() ) { AssociationType associationType = (AssociationType) type; if ( type.isComponentType() ) { // an any type is both an association and a composite... return NonIdentifierAttributeNature.ANY; } return type.isCollectionType() ? NonIdentifierAttributeNature.COLLECTION : NonIdentifierAttributeNature.ENTITY; } else { if ( type.isComponentType() ) { return NonIdentifierAttributeNature.COMPOSITE; } return NonIdentifierAttributeNature.BASIC; } }
/** * Get the qualified (prefixed by alias) names of the columns of the owning entity which are to be used in the join * * @param associationType The association type for the association that represents the join * @param columnQualifier The left-hand side table alias * @param propertyIndex The index of the property that represents the association/join * @param begin The index for any nested (composites) attributes * @param lhsPersister The persister for the left-hand side of the association/join * @param mapping The mapping (typically the SessionFactory). * * @return The qualified column names. */ public static String[] getAliasedLHSColumnNames( AssociationType associationType, String columnQualifier, int propertyIndex, int begin, OuterJoinLoadable lhsPersister, Mapping mapping) { if ( associationType.useLHSPrimaryKey() ) { return StringHelper.qualify( columnQualifier, lhsPersister.getIdentifierColumnNames() ); } else { final String propertyName = associationType.getLHSPropertyName(); if ( propertyName == null ) { return ArrayHelper.slice( toColumns( lhsPersister, columnQualifier, propertyIndex ), begin, associationType.getColumnSpan( mapping ) ); } else { //bad cast return ( (PropertyMapping) lhsPersister ).toColumns( columnQualifier, propertyName ); } } }
/** * Add on association (one-to-one, many-to-one, or a collection) to a list * of associations to be fetched by outerjoin (if necessary) */ private void addAssociationToJoinTreeIfNecessary( final AssociationType type, final String[] aliasedLhsColumns, final String alias, final String path, int currentDepth, final int joinType) throws MappingException { if (joinType>=0) { addAssociationToJoinTree( type, aliasedLhsColumns, alias, path, currentDepth, joinType ); } }
/** * Get the join type (inner, outer, etc) or -1 if the * association should not be joined. Override on * subclasses. */ protected int getJoinType( AssociationType type, FetchMode config, String path, String lhsTable, String[] lhsColumns, boolean nullable, int currentDepth, CascadeStyle cascadeStyle) throws MappingException { if ( !isJoinedFetchEnabled(type, config, cascadeStyle) ) return -1; if ( isTooDeep(currentDepth) || ( type.isCollectionType() && isTooManyCollections() ) ) return -1; final boolean dupe = isDuplicateAssociation(lhsTable, lhsColumns, type); if (dupe) return -1; return getJoinType(nullable, currentDepth); }
/** * Used to detect circularities in the joined graph, note that * this method is side-effecty */ protected boolean isDuplicateAssociation( final String lhsTable, final String[] lhsColumnNames, final AssociationType type ) { final String foreignKeyTable; final String[] foreignKeyColumns; if ( type.getForeignKeyDirection()==ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT ) { foreignKeyTable = lhsTable; foreignKeyColumns = lhsColumnNames; } else { foreignKeyTable = type.getAssociatedJoinable( getFactory() ).getTableName(); foreignKeyColumns = JoinHelper.getRHSColumnNames( type, getFactory() ); } return isDuplicateAssociation(foreignKeyTable, foreignKeyColumns); }
/** * Should we join this association? */ protected boolean isJoinable( final int joinType, final Set visitedAssociationKeys, final String lhsTable, final String[] lhsColumnNames, final AssociationType type, final int depth ) { if (joinType<0) return false; if (joinType==JoinFragment.INNER_JOIN) return true; Integer maxFetchDepth = getFactory().getSettings().getMaximumFetchDepth(); final boolean tooDeep = maxFetchDepth!=null && depth >= maxFetchDepth.intValue(); return !tooDeep && !isDuplicateAssociation(lhsTable, lhsColumnNames, type); }
public OuterJoinableAssociation( AssociationType joinableType, String lhsAlias, String[] lhsColumns, String rhsAlias, int joinType, SessionFactoryImplementor factory, Map enabledFilters) throws MappingException { this.joinableType = joinableType; this.lhsAlias = lhsAlias; this.lhsColumns = lhsColumns; this.rhsAlias = rhsAlias; this.joinType = joinType; this.joinable = joinableType.getAssociatedJoinable(factory); this.rhsColumns = JoinHelper.getRHSColumnNames(joinableType, factory); this.on = joinableType.getOnCondition(rhsAlias, factory, enabledFilters); this.enabledFilters = enabledFilters; // needed later for many-to-many/filter application }
/** * Cascade an action to the child or children */ private void cascadeProperty( final Object child, final Type type, final CascadeStyle style, final Object anything, final boolean isCascadeDeleteEnabled) throws HibernateException { if (child!=null) { if ( type.isAssociationType() ) { AssociationType associationType = (AssociationType) type; if ( cascadeAssociationNow( associationType ) ) { cascadeAssociation( child, type, style, anything, isCascadeDeleteEnabled ); } } else if ( type.isComponentType() ) { cascadeComponent( child, (AbstractComponentType) type, anything ); } } }
/** * Get the aliased columns of the owning entity which are to * be used in the join */ public static String[] getAliasedLHSColumnNames( AssociationType type, String alias, int property, int begin, OuterJoinLoadable lhsPersister, Mapping mapping ) { if ( type.useLHSPrimaryKey() ) { return StringHelper.qualify( alias, lhsPersister.getIdentifierColumnNames() ); } else { String propertyName = type.getLHSPropertyName(); if (propertyName==null) { return ArrayHelper.slice( lhsPersister.toColumns(alias, property), begin, type.getColumnSpan(mapping) ); } else { return ( (PropertyMapping) lhsPersister ).toColumns(alias, propertyName); //bad cast } } }
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(), JoinFragment.INNER_JOIN, persister.getElementColumnNames( fkTableAlias ) ); } join.addCondition( fkTableAlias, keyColumnNames, " = ?" ); fromElement.setJoinSequence( join ); fromElement.setFilter( true ); if ( log.isDebugEnabled() ) { log.debug( "createFromFilterElement() : processed filter FROM element." ); } return fromElement; }
protected JoinType getJoinType( OuterJoinLoadable persister, PropertyPath path, int propertyNumber, AssociationType associationType, FetchMode metadataFetchMode, CascadeStyle metadataCascadeStyle, String lhsTable, String[] lhsColumns, boolean nullable, int currentDepth) throws MappingException { // NOTE : we override this form here specifically to account for // fetch profiles. // TODO : how to best handle criteria queries? if ( lockOptions.getLockMode().greaterThan( LockMode.READ ) ) { return JoinType.NONE; } if ( isTooDeep( currentDepth ) || ( associationType.isCollectionType() && isTooManyCollections() ) ) { return JoinType.NONE; } if ( !isJoinedFetchEnabledInMapping( metadataFetchMode, associationType ) && !isJoinFetchEnabledByProfile( persister, path, propertyNumber ) ) { return JoinType.NONE; } if ( isDuplicateAssociation( lhsTable, lhsColumns, associationType ) ) { return JoinType.NONE; } return getJoinType( nullable, currentDepth ); }
/** * Process a particular association owned by the entity * * @param associationType The type representing the association to be * processed. * @param persister The owner of the association to be processed. * @param propertyNumber The property number for the association * (relative to the persister). * @param alias The entity alias * @param path The path to the association * @param nullable is the association nullable (which I think is supposed * to indicate inner/outer join semantics). * @param currentDepth The current join depth * @throws org.hibernate.MappingException ??? */ private void walkEntityAssociationTree( final AssociationType associationType, final OuterJoinLoadable persister, final int propertyNumber, final String alias, final PropertyPath path, final boolean nullable, final int currentDepth) throws MappingException { String[] aliasedLhsColumns = JoinHelper.getAliasedLHSColumnNames( associationType, alias, propertyNumber, persister, getFactory() ); String[] lhsColumns = JoinHelper.getLHSColumnNames( associationType, propertyNumber, persister, getFactory() ); String lhsTable = JoinHelper.getLHSTableName(associationType, propertyNumber, persister); PropertyPath subPath = path.append( persister.getSubclassPropertyName(propertyNumber) ); JoinType joinType = getJoinType( persister, subPath, propertyNumber, associationType, persister.getFetchMode( propertyNumber ), persister.getCascadeStyle( propertyNumber ), lhsTable, lhsColumns, nullable, currentDepth ); addAssociationToJoinTreeIfNecessary( associationType, aliasedLhsColumns, alias, subPath, currentDepth, joinType ); }
/** * Used to detect circularities in the joined graph, note that * this method is side-effecty */ protected boolean isDuplicateAssociation(final String lhsTable, final String[] lhsColumnNames, final AssociationType type) { final String foreignKeyTable; final String[] foreignKeyColumns; if ( type.getForeignKeyDirection()==ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT ) { foreignKeyTable = lhsTable; foreignKeyColumns = lhsColumnNames; } else { foreignKeyTable = type.getAssociatedJoinable( getFactory() ).getTableName(); foreignKeyColumns = JoinHelper.getRHSColumnNames( type, getFactory() ); } return isDuplicateAssociation(foreignKeyTable, foreignKeyColumns); }
protected JoinType getJoinType( OuterJoinLoadable persister, PropertyPath path, int propertyNumber, AssociationType associationType, FetchMode metadataFetchMode, CascadeStyle metadataCascadeStyle, String lhsTable, String[] lhsColumns, boolean nullable, int currentDepth) throws MappingException { JoinType joinType = super.getJoinType( persister, path, propertyNumber, associationType, metadataFetchMode, metadataCascadeStyle, lhsTable, lhsColumns, nullable, currentDepth ); //we can use an inner join for the many-to-many if ( joinType==JoinType.LEFT_OUTER_JOIN && path.isRoot() ) { joinType=JoinType.INNER_JOIN; } return joinType; }
private AssociationType getJoinedAssociationTypeOrNull(Join join) { if ( !JoinDefinedByMetadata.class.isInstance( join ) ) { return null; } final Type joinedType = ( (JoinDefinedByMetadata) join ).getJoinedPropertyType(); return joinedType.isAssociationType() ? (AssociationType) joinedType : null; }
private static boolean isSubsequentSelectDelayed(AssociationType type, SessionFactoryImplementor sessionFactory) { if ( type.isAnyType() ) { // we'd need more context here. this is only kept as part of the property state on the owning entity return false; } else if ( type.isEntityType() ) { return ( (EntityPersister) type.getAssociatedJoinable( sessionFactory ) ).hasProxy(); } else { final CollectionPersister cp = ( (CollectionPersister) type.getAssociatedJoinable( sessionFactory ) ); return cp.isLazy() || cp.isExtraLazy(); } }
public EntityBasedAssociationAttribute( EntityPersister source, SessionFactoryImplementor sessionFactory, int attributeNumber, String attributeName, AssociationType attributeType, BaselineAttributeInformation baselineInfo) { super( source, sessionFactory, attributeNumber, attributeName, attributeType, baselineInfo ); }
@Override public AssociationKey getAssociationKey() { final AssociationType type = getType(); if ( type.isAnyType() ) { return new AssociationKey( JoinHelper.getLHSTableName( type, attributeNumber(), (OuterJoinLoadable) getSource() ), JoinHelper.getLHSColumnNames( type, attributeNumber(), 0, (OuterJoinLoadable) getSource(), sessionFactory() ) ); } final Joinable joinable = type.getAssociatedJoinable( sessionFactory() ); if ( type.getForeignKeyDirection() == ForeignKeyDirection.FOREIGN_KEY_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) source(); lhsTableName = getLHSTableName( type, attributeNumber(), entityPersister ); lhsColumnNames = getLHSColumnNames( type, attributeNumber(), entityPersister, sessionFactory() ); } return new AssociationKey( lhsTableName, lhsColumnNames ); } else { return new AssociationKey( joinable.getTableName(), getRHSColumnNames( type, sessionFactory() ) ); } }
public CompositeBasedAssociationAttribute( AbstractCompositionAttribute source, SessionFactoryImplementor factory, int entityBasedAttributeNumber, String attributeName, AssociationType attributeType, BaselineAttributeInformation baselineInfo, int subAttributeNumber, AssociationKey associationKey) { super( source, factory, entityBasedAttributeNumber, attributeName, attributeType, baselineInfo ); this.subAttributeNumber = subAttributeNumber; this.associationKey = associationKey; }
@Deprecated public static StandardProperty buildStandardProperty(Property property, boolean lazyAvailable) { final Type type = property.getValue().getType(); // we need to dirty check collections, since they can cause an owner // version number increment // we need to dirty check many-to-ones with not-found="ignore" in order // to update the cache (not the database), since in this case a null // entity reference can lose information boolean alwaysDirtyCheck = type.isAssociationType() && ( (AssociationType) type ).isAlwaysDirtyChecked(); return new StandardProperty( property.getName(), type, lazyAvailable && property.isLazy(), property.isInsertable(), property.isUpdateable(), property.getValueGenerationStrategy(), property.isOptional(), alwaysDirtyCheck || property.isUpdateable(), property.isOptimisticLocked(), property.getCascadeStyle(), property.getValue().getFetchMode() ); }
/** * Get the columns of the owning entity which are to be used in the join * * @param type The type representing the join * @param property The property index for the association type * @param begin ? * @param lhsPersister The persister for the left-hand-side of the join * @param mapping The mapping object (typically the SessionFactory) * * @return The columns for the left-hand-side of the join */ public static String[] getLHSColumnNames( AssociationType type, int property, int begin, OuterJoinLoadable lhsPersister, Mapping mapping) { if ( type.useLHSPrimaryKey() ) { //return lhsPersister.getSubclassPropertyColumnNames(property); return lhsPersister.getIdentifierColumnNames(); } else { final String propertyName = type.getLHSPropertyName(); if ( propertyName == null ) { //slice, to get the columns for this component //property return ArrayHelper.slice( property < 0 ? lhsPersister.getIdentifierColumnNames() : lhsPersister.getSubclassPropertyColumnNames( property ), begin, type.getColumnSpan( mapping ) ); } else { //property-refs for associations defined on a //component are not supported, so no need to slice return lhsPersister.getPropertyColumnNames( propertyName ); } } }
/** * Determine the name of the table that is the left-hand-side of the join. Usually this is the * name of the main table from the left-hand-side persister. But that is not the case with property-refs. * * @param type The type representing the join * @param propertyIndex The property index for the type * @param lhsPersister The persister for the left-hand-side of the join * * @return The table name */ public static String getLHSTableName( AssociationType type, int propertyIndex, OuterJoinLoadable lhsPersister) { if ( type.useLHSPrimaryKey() || propertyIndex < 0 ) { return lhsPersister.getTableName(); } else { final String propertyName = type.getLHSPropertyName(); if ( propertyName == null ) { //if there is no property-ref, assume the join //is to the subclass table (ie. the table of the //subclass that the association belongs to) return lhsPersister.getSubclassPropertyTableName( propertyIndex ); } else { //handle a property-ref String propertyRefTable = lhsPersister.getPropertyTableName( propertyName ); if ( propertyRefTable == null ) { //it is possible that the tree-walking in OuterJoinLoader can get to //an association defined by a subclass, in which case the property-ref //might refer to a property defined on a subclass of the current class //in this case, the table name is not known - this temporary solution //assumes that the property-ref refers to a property of the subclass //table that the association belongs to (a reasonable guess) //TODO: fix this, add: OuterJoinLoadable.getSubclassPropertyTableName(String propertyName) propertyRefTable = lhsPersister.getSubclassPropertyTableName( propertyIndex ); } return propertyRefTable; } } }
/** * Get the columns of the associated table which are to be used in the join * * @param type The type * @param factory The SessionFactory * * @return The columns for the right-hand-side of the join */ public static String[] getRHSColumnNames(AssociationType type, SessionFactoryImplementor factory) { final String uniqueKeyPropertyName = type.getRHSUniqueKeyPropertyName(); final Joinable joinable = type.getAssociatedJoinable( factory ); if ( uniqueKeyPropertyName == null ) { return joinable.getKeyColumnNames(); } else { return ( (OuterJoinLoadable) joinable ).getPropertyColumnNames( uniqueKeyPropertyName ); } }
Join( SessionFactoryImplementor factory, AssociationType associationType, String alias, JoinType joinType, String[] lhsColumns) throws MappingException { this.associationType = associationType; this.joinable = associationType.getAssociatedJoinable( factory ); this.alias = alias; this.joinType = joinType; this.lhsColumns = lhsColumns; }
@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; }