Search in sources :

Example 6 with Select

use of org.hibernate.sql.Select in project hibernate-orm by hibernate.

the class AbstractEntityPersister method generateEntityIdByNaturalIdSql.

private String generateEntityIdByNaturalIdSql(boolean[] valueNullness) {
    EntityPersister rootPersister = getFactory().getEntityPersister(getRootEntityName());
    if (rootPersister != this) {
        if (rootPersister instanceof AbstractEntityPersister) {
            return ((AbstractEntityPersister) rootPersister).generateEntityIdByNaturalIdSql(valueNullness);
        }
    }
    Select select = new Select(getFactory().getDialect());
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment("get current natural-id->entity-id state " + getEntityName());
    }
    final String rootAlias = getRootAlias();
    select.setSelectClause(identifierSelectFragment(rootAlias, ""));
    select.setFromClause(fromTableFragment(rootAlias) + fromJoinFragment(rootAlias, true, false));
    final StringBuilder whereClause = new StringBuilder();
    final int[] propertyTableNumbers = getPropertyTableNumbers();
    final int[] naturalIdPropertyIndexes = this.getNaturalIdentifierProperties();
    int valuesIndex = -1;
    for (int propIdx = 0; propIdx < naturalIdPropertyIndexes.length; propIdx++) {
        valuesIndex++;
        if (propIdx > 0) {
            whereClause.append(" and ");
        }
        final int naturalIdIdx = naturalIdPropertyIndexes[propIdx];
        final String tableAlias = generateTableAlias(rootAlias, propertyTableNumbers[naturalIdIdx]);
        final String[] propertyColumnNames = getPropertyColumnNames(naturalIdIdx);
        final String[] aliasedPropertyColumns = StringHelper.qualify(tableAlias, propertyColumnNames);
        if (valueNullness != null && valueNullness[valuesIndex]) {
            whereClause.append(StringHelper.join(" is null and ", aliasedPropertyColumns)).append(" is null");
        } else {
            whereClause.append(StringHelper.join("=? and ", aliasedPropertyColumns)).append("=?");
        }
    }
    whereClause.append(whereJoinFragment(getRootAlias(), true, false));
    return select.setOuterJoins("", "").setWhereClause(whereClause.toString()).toStatementString();
}
Also used : SimpleSelect(org.hibernate.sql.SimpleSelect) Select(org.hibernate.sql.Select)

Example 7 with Select

use of org.hibernate.sql.Select in project hibernate-orm by hibernate.

the class OneToManyJoinWalker method initStatementString.

private void initStatementString(final OuterJoinLoadable elementPersister, final String alias, final int batchSize, final String subquery) throws MappingException {
    final int joins = countEntityPersisters(associations);
    suffixes = BasicLoader.generateSuffixes(joins + 1);
    final int collectionJoins = countCollectionPersisters(associations) + 1;
    collectionSuffixes = BasicLoader.generateSuffixes(joins + 1, collectionJoins);
    StringBuilder whereString = whereString(alias, oneToManyPersister.getKeyColumnNames(), subquery, batchSize);
    String filter = oneToManyPersister.filterFragment(alias, getLoadQueryInfluencers().getEnabledFilters());
    whereString.insert(0, StringHelper.moveAndToBeginning(filter));
    JoinFragment ojf = mergeOuterJoins(associations);
    Select select = new Select(getDialect()).setSelectClause(oneToManyPersister.selectFragment(null, null, alias, suffixes[joins], collectionSuffixes[0], true) + selectString(associations)).setFromClause(elementPersister.fromTableFragment(alias) + elementPersister.fromJoinFragment(alias, true, true)).setWhereClause(whereString.toString()).setOuterJoins(ojf.toFromFragmentString(), ojf.toWhereFragmentString() + elementPersister.whereJoinFragment(alias, true, true));
    select.setOrderByClause(orderBy(associations, oneToManyPersister.getSQLOrderByString(alias)));
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment("load one-to-many " + oneToManyPersister.getRole());
    }
    sql = select.toStatementString();
}
Also used : Select(org.hibernate.sql.Select) JoinFragment(org.hibernate.sql.JoinFragment)

Example 8 with Select

use of org.hibernate.sql.Select in project hibernate-orm by hibernate.

the class AbstractTableBasedBulkIdHandler method generateIdInsertSelect.

/**
	 * Generate the {@code INSERT}-{@code SELECT} statement for holding matching ids.  This is the
	 * {@code INSERT} used to populate the bulk-id table with ids matching the restrictions defined in the
	 * original {@code WHERE} clause
	 *
	 * @param tableAlias The table alias to use for the entity
	 * @param whereClause The processed representation for the user-defined {@code WHERE} clause.
	 *
	 * @return The {@code INSERT}-{@code SELECT} for populating the bulk-id table.
	 */
protected String generateIdInsertSelect(String tableAlias, IdTableInfo idTableInfo, ProcessedWhereClause whereClause) {
    final Dialect dialect = sessionFactory.getJdbcServices().getJdbcEnvironment().getDialect();
    final Select select = generateIdSelect(tableAlias, whereClause);
    InsertSelect insert = new InsertSelect(dialect);
    if (sessionFactory.getSessionFactoryOptions().isCommentsEnabled()) {
        insert.setComment("insert-select for " + getTargetedQueryable().getEntityName() + " ids");
    }
    insert.setTableName(idTableInfo.getQualifiedIdTableName());
    insert.setSelect(select);
    return insert.toStatementString();
}
Also used : InsertSelect(org.hibernate.sql.InsertSelect) Dialect(org.hibernate.dialect.Dialect) Select(org.hibernate.sql.Select) InsertSelect(org.hibernate.sql.InsertSelect)

Example 9 with Select

use of org.hibernate.sql.Select in project hibernate-orm by hibernate.

the class AbstractEntityPersister method generateGeneratedValuesSelectString.

private String generateGeneratedValuesSelectString(final GenerationTiming generationTimingToMatch) {
    Select select = new Select(getFactory().getDialect());
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment("get generated state " + getEntityName());
    }
    String[] aliasedIdColumns = StringHelper.qualify(getRootAlias(), getIdentifierColumnNames());
    // Here we render the select column list based on the properties defined as being generated.
    // For partial component generation, we currently just re-select the whole component
    // rather than trying to handle the individual generated portions.
    String selectClause = concretePropertySelectFragment(getRootAlias(), new InclusionChecker() {

        @Override
        public boolean includeProperty(int propertyNumber) {
            final InDatabaseValueGenerationStrategy generationStrategy = entityMetamodel.getInDatabaseValueGenerationStrategies()[propertyNumber];
            return generationStrategy != null && timingsMatch(generationStrategy.getGenerationTiming(), generationTimingToMatch);
        }
    });
    selectClause = selectClause.substring(2);
    String fromClause = fromTableFragment(getRootAlias()) + fromJoinFragment(getRootAlias(), true, false);
    String whereClause = new StringBuilder().append(StringHelper.join("=? and ", aliasedIdColumns)).append("=?").append(whereJoinFragment(getRootAlias(), true, false)).toString();
    return select.setSelectClause(selectClause).setFromClause(fromClause).setOuterJoins("", "").setWhereClause(whereClause).toStatementString();
}
Also used : InDatabaseValueGenerationStrategy(org.hibernate.tuple.InDatabaseValueGenerationStrategy) SimpleSelect(org.hibernate.sql.SimpleSelect) Select(org.hibernate.sql.Select)

Example 10 with Select

use of org.hibernate.sql.Select in project hibernate-orm by hibernate.

the class AbstractEntityPersister method generateIdByUniqueKeySelectString.

protected String generateIdByUniqueKeySelectString(String uniquePropertyName) {
    Select select = new Select(getFactory().getDialect());
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment("resolve id by unique property [" + getEntityName() + "." + uniquePropertyName + "]");
    }
    final String rooAlias = getRootAlias();
    select.setFromClause(fromTableFragment(rooAlias) + fromJoinFragment(rooAlias, true, false));
    SelectFragment selectFragment = new SelectFragment();
    selectFragment.addColumns(rooAlias, getIdentifierColumnNames(), getIdentifierAliases());
    select.setSelectClause(selectFragment);
    StringBuilder whereClauseBuffer = new StringBuilder();
    final int uniquePropertyIndex = getSubclassPropertyIndex(uniquePropertyName);
    final String uniquePropertyTableAlias = generateTableAlias(rooAlias, getSubclassPropertyTableNumber(uniquePropertyIndex));
    String sep = "";
    for (String columnTemplate : getSubclassPropertyColumnReaderTemplateClosure()[uniquePropertyIndex]) {
        if (columnTemplate == null) {
            continue;
        }
        final String columnReference = StringHelper.replace(columnTemplate, Template.TEMPLATE, uniquePropertyTableAlias);
        whereClauseBuffer.append(sep).append(columnReference).append("=?");
        sep = " and ";
    }
    for (String formulaTemplate : getSubclassPropertyFormulaTemplateClosure()[uniquePropertyIndex]) {
        if (formulaTemplate == null) {
            continue;
        }
        final String formulaReference = StringHelper.replace(formulaTemplate, Template.TEMPLATE, uniquePropertyTableAlias);
        whereClauseBuffer.append(sep).append(formulaReference).append("=?");
        sep = " and ";
    }
    whereClauseBuffer.append(whereJoinFragment(rooAlias, true, false));
    select.setWhereClause(whereClauseBuffer.toString());
    return select.setOuterJoins("", "").toStatementString();
}
Also used : SelectFragment(org.hibernate.sql.SelectFragment) SimpleSelect(org.hibernate.sql.SimpleSelect) Select(org.hibernate.sql.Select)

Aggregations

Select (org.hibernate.sql.Select)11 SimpleSelect (org.hibernate.sql.SimpleSelect)6 JoinFragment (org.hibernate.sql.JoinFragment)4 Dialect (org.hibernate.dialect.Dialect)2 InsertSelect (org.hibernate.sql.InsertSelect)2 SelectFragment (org.hibernate.sql.SelectFragment)2 AssociationType (org.hibernate.type.AssociationType)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 MappingException (org.hibernate.MappingException)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 OuterJoinableAssociation (org.hibernate.loader.OuterJoinableAssociation)1 JoinType (org.hibernate.sql.JoinType)1 SelectValues (org.hibernate.sql.SelectValues)1 InDatabaseValueGenerationStrategy (org.hibernate.tuple.InDatabaseValueGenerationStrategy)1 CollectionType (org.hibernate.type.CollectionType)1 ComponentType (org.hibernate.type.ComponentType)1 CompositeType (org.hibernate.type.CompositeType)1