Search in sources :

Example 1 with InDatabaseValueGenerationStrategy

use of org.hibernate.tuple.InDatabaseValueGenerationStrategy 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 2 with InDatabaseValueGenerationStrategy

use of org.hibernate.tuple.InDatabaseValueGenerationStrategy in project hibernate-orm by hibernate.

the class AbstractEntityPersister method generateInsertString.

/**
	 * Generate the SQL that inserts a row
	 */
protected String generateInsertString(boolean identityInsert, boolean[] includeProperty, int j) {
    // todo : remove the identityInsert param and variations;
    //   identity-insert strings are now generated from generateIdentityInsertString()
    Insert insert = new Insert(getFactory().getDialect()).setTableName(getTableName(j));
    // add normal properties
    for (int i = 0; i < entityMetamodel.getPropertySpan(); i++) {
        // values
        if (isPropertyOfTable(i, j)) {
            if (!lobProperties.contains(i)) {
                final InDatabaseValueGenerationStrategy generationStrategy = entityMetamodel.getInDatabaseValueGenerationStrategies()[i];
                if (generationStrategy != null && generationStrategy.getGenerationTiming().includesInsert()) {
                    if (generationStrategy.referenceColumnsInSql()) {
                        final String[] values;
                        if (generationStrategy.getReferencedColumnValues() == null) {
                            values = propertyColumnWriters[i];
                        } else {
                            final int numberOfColumns = propertyColumnWriters[i].length;
                            values = new String[numberOfColumns];
                            for (int x = 0; x < numberOfColumns; x++) {
                                if (generationStrategy.getReferencedColumnValues()[x] != null) {
                                    values[x] = generationStrategy.getReferencedColumnValues()[x];
                                } else {
                                    values[x] = propertyColumnWriters[i][x];
                                }
                            }
                        }
                        insert.addColumns(getPropertyColumnNames(i), propertyColumnInsertable[i], values);
                    }
                } else if (includeProperty[i]) {
                    insert.addColumns(getPropertyColumnNames(i), propertyColumnInsertable[i], propertyColumnWriters[i]);
                }
            }
        }
    }
    // add the discriminator
    if (j == 0) {
        addDiscriminatorToInsert(insert);
    }
    // add the primary key
    if (j == 0 && identityInsert) {
        insert.addIdentityColumn(getKeyColumns(0)[0]);
    } else {
        insert.addColumns(getKeyColumns(j));
    }
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        insert.setComment("insert " + getEntityName());
    }
    // and updates.  Insert them at the end.
    for (int i : lobProperties) {
        if (includeProperty[i] && isPropertyOfTable(i, j)) {
            // this property belongs on the table and is to be inserted
            insert.addColumns(getPropertyColumnNames(i), propertyColumnInsertable[i], propertyColumnWriters[i]);
        }
    }
    String result = insert.toStatementString();
    // append the SQL to return the generated identifier
    if (j == 0 && identityInsert && useInsertSelectIdentity()) {
        //TODO: suck into Insert
        result = getFactory().getDialect().getIdentityColumnSupport().appendIdentitySelectToInsert(result);
    }
    return result;
}
Also used : InDatabaseValueGenerationStrategy(org.hibernate.tuple.InDatabaseValueGenerationStrategy) Insert(org.hibernate.sql.Insert)

Aggregations

InDatabaseValueGenerationStrategy (org.hibernate.tuple.InDatabaseValueGenerationStrategy)2 Insert (org.hibernate.sql.Insert)1 Select (org.hibernate.sql.Select)1 SimpleSelect (org.hibernate.sql.SimpleSelect)1