private void initStatementString(
      final String projection,
      final String condition,
      final String orderBy,
      final String groupBy,
      final LockMode lockMode)
      throws MappingException {

    final int joins = countEntityPersisters(associations);
    suffixes = BasicLoader.generateSuffixes(joins + 1);

    JoinFragment ojf = mergeOuterJoins(associations);

    Select select =
        new Select(getDialect())
            .setLockMode(lockMode)
            .setSelectClause(
                projection == null
                    ? persister.selectFragment(alias, suffixes[joins]) + selectString(associations)
                    : projection)
            .setFromClause(
                getDialect().appendLockHint(lockMode, persister.fromTableFragment(alias))
                    + persister.fromJoinFragment(alias, true, true))
            .setWhereClause(condition)
            .setOuterJoins(
                ojf.toFromFragmentString(), ojf.toWhereFragmentString() + getWhereFragment())
            .setOrderByClause(orderBy(associations, orderBy))
            .setGroupByClause(groupBy);

    if (getFactory().getSettings().isCommentsEnabled()) {
      select.setComment(getComment());
    }
    sql = select.toStatementString();
  }
Example #2
0
 /**
  * Walk the association tree for an entity, adding associations which should be join fetched to
  * the {@link #associations} inst var. This form is the entry point into the walking for a given
  * entity, starting the recursive calls into {@link
  * #walkEntityTree(org.hibernate.persister.entity.OuterJoinLoadable, String, PropertyPath ,int)}.
  *
  * @param persister The persister representing the entity to be walked.
  * @param alias The (root) alias to use for this entity/persister.
  * @param path The property path to the entity being walked
  * @param currentDepth The current join depth
  * @throws org.hibernate.MappingException ???
  */
 private void walkEntityTree(
     final OuterJoinLoadable persister,
     final String alias,
     final PropertyPath path,
     final int currentDepth)
     throws MappingException {
   int n = persister.countSubclassProperties();
   for (int i = 0; i < n; i++) {
     Type type = persister.getSubclassPropertyType(i);
     if (type.isAssociationType()) {
       walkEntityAssociationTree(
           (AssociationType) type,
           persister,
           i,
           alias,
           path,
           persister.isSubclassPropertyNullable(i),
           currentDepth);
     } else if (type.isComponentType()) {
       walkComponentTree(
           (CompositeType) type,
           i,
           0,
           persister,
           alias,
           path.append(persister.getSubclassPropertyName(i)),
           currentDepth);
     }
   }
 }
Example #3
0
  /**
   * 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);
  }
 public String selectFragment(
     Joinable rhs,
     String rhsAlias,
     String lhsAlias,
     String entitySuffix,
     String collectionSuffix,
     boolean includeCollectionColumns) {
   StringBuffer buf = new StringBuffer();
   if (includeCollectionColumns) {
     //			buf.append( selectFragment( lhsAlias, "" ) )//ignore suffix for collection columns!
     buf.append(selectFragment(lhsAlias, collectionSuffix)).append(", ");
   }
   OuterJoinLoadable ojl = (OuterJoinLoadable) getElementPersister();
   return buf.append(
           ojl.selectFragment(lhsAlias, entitySuffix)) // use suffix for the entity columns
       .toString();
 }
 public AbstractEntityJoinWalker(
     OuterJoinLoadable persister,
     SessionFactoryImplementor factory,
     Map enabledFilters,
     String alias) {
   super(factory, enabledFilters);
   this.persister = persister;
   this.alias = (alias == null) ? generateRootAlias(persister.getEntityName()) : alias;
 }
  protected final void initAll(
      final String whereString, final String orderByString, final LockMode lockMode)
      throws MappingException {
    walkEntityTree(persister, getAlias());
    List allAssociations = new ArrayList();
    allAssociations.addAll(associations);
    allAssociations.add(
        new OuterJoinableAssociation(
            persister.getEntityType(),
            null,
            null,
            alias,
            JoinFragment.LEFT_OUTER_JOIN,
            getFactory(),
            CollectionHelper.EMPTY_MAP));

    initPersisters(allAssociations, lockMode);
    initStatementString(whereString, orderByString, lockMode);
  }
Example #7
0
  /**
   * Walk the association tree for an entity, adding associations which should be join fetched to
   * the {@link #associations} inst var. This form is the entry point into the walking for a given
   * entity, starting the recursive calls into {@link
   * #walkEntityTree(org.hibernate.persister.entity.OuterJoinLoadable, String, PropertyPath ,int)}.
   *
   * @param persister The persister representing the entity to be walked.
   * @param alias The (root) alias to use for this entity/persister.
   * @param path The property path to the entity being walked
   * @param currentDepth The current join depth
   * @throws org.hibernate.MappingException ???
   */
  private void walkEntityTree(
      final OuterJoinLoadable persister,
      final String alias,
      final PropertyPath path,
      final int currentDepth)
      throws MappingException {
    int n = persister.countSubclassProperties();
    for (int i = 0; i < n; i++) {
      Type type = persister.getSubclassPropertyType(i);
      if (type.isAssociationType()) {
        walkEntityAssociationTree(
            (AssociationType) type,
            persister,
            i,
            alias,
            path,
            persister.isSubclassPropertyNullable(i),
            currentDepth);
      } else if (type.isComponentType()) {
        walkComponentTree(
            (CompositeType) type,
            i,
            0,
            persister,
            alias,
            path.append(persister.getSubclassPropertyName(i)),
            currentDepth);
      }
    }

    // if the entity has a composite identifier, see if we need to handle
    // its sub-properties separately
    final Type idType = persister.getIdentifierType();
    if (idType.isComponentType()) {
      final CompositeType cidType = (CompositeType) idType;
      if (cidType.isEmbedded()) {
        // we have an embedded composite identifier.  Most likely we need to process the composite
        // properties separately, although there is an edge case where the identifier is really
        // a simple identifier (single value) wrapped in a JPA @IdClass or even in the case of a
        // a simple identifier (single value) wrapped in a Hibernate composite type.
        //
        // We really do not have a built-in method to determine that.  However, generally the
        // persister would report that there is single, physical identifier property which is
        // explicitly at odds with the notion of "embedded composite".  So we use that for now
        if (persister.getEntityMetamodel().getIdentifierProperty().isEmbedded()) {
          walkComponentTree(cidType, -1, 0, persister, alias, path.append(""), currentDepth);
        }
      }
    }
  }
 protected String getWhereFragment() throws MappingException {
   // here we do not bother with the discriminator.
   return persister.whereJoinFragment(alias, true, true);
 }
// $Id: CascadeEntityJoinWalker.java,v 1.1 2005/07/26 05:51:47 oneovthafew Exp $