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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations