Java Code Examples for org.hibernate.sql.Select#setWhereClause()

The following examples show how to use org.hibernate.sql.Select#setWhereClause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected String renderSelect(
		final int[] tableNumbers,
		final int[] columnNumbers,
		final int[] formulaNumbers) {

	Arrays.sort( tableNumbers ); //get 'em in the right order (not that it really matters)

	//render the where and from parts
	int drivingTable = tableNumbers[0];
	final String drivingAlias = generateTableAlias(
			getRootAlias(),
			drivingTable
	); //we *could* regerate this inside each called method!
	final String where = createWhereByKey( drivingTable, drivingAlias );
	final String from = createFrom( drivingTable, drivingAlias );

	//now render the joins
	JoinFragment jf = createJoin( tableNumbers, drivingAlias );

	//now render the select clause
	SelectFragment selectFragment = createSelect( columnNumbers, formulaNumbers );

	//now tie it all together
	Select select = new Select( getFactory().getDialect() );
	select.setSelectClause( selectFragment.toFragmentString().substring( 2 ) );
	select.setFromClause( from );
	select.setWhereClause( where );
	select.setOuterJoins( jf.toFromFragmentString(), jf.toWhereFragmentString() );
	if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
		select.setComment( "sequential select " + getEntityName() );
	}
	return select.toStatementString();
}
 
Example 2
Source File: AbstractTableBasedBulkIdHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Select generateIdSelect(
		String tableAlias,
		ProcessedWhereClause whereClause) {

	final Dialect dialect = sessionFactory.getJdbcServices().getJdbcEnvironment().getDialect();

	final Select select = new Select( dialect );
	final SelectValues selectClause = new SelectValues( dialect ).addColumns(
			tableAlias,
			getTargetedQueryable().getIdentifierColumnNames(),
			getTargetedQueryable().getIdentifierColumnNames()
	);
	addAnyExtraIdSelectValues( selectClause );
	select.setSelectClause( selectClause.render() );

	String rootTableName = getTargetedQueryable().getTableName();
	String fromJoinFragment = getTargetedQueryable().fromJoinFragment( tableAlias, true, false );
	String whereJoinFragment = getTargetedQueryable().whereJoinFragment( tableAlias, true, false );

	select.setFromClause( rootTableName + ' ' + tableAlias + fromJoinFragment );

	if ( whereJoinFragment == null ) {
		whereJoinFragment = "";
	}
	else {
		whereJoinFragment = whereJoinFragment.trim();
		if ( whereJoinFragment.startsWith( "and" ) ) {
			whereJoinFragment = whereJoinFragment.substring( 4 );
		}
	}

	if ( whereClause.getUserWhereClauseFragment().length() > 0 ) {
		if ( whereJoinFragment.length() > 0 ) {
			whereJoinFragment += " and ";
		}
	}
	select.setWhereClause( whereJoinFragment + whereClause.getUserWhereClauseFragment() );
	return select;
}
 
Example 3
Source File: AbstractEntityPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected String renderSelect(
		final int[] tableNumbers,
        final int[] columnNumbers,
        final int[] formulaNumbers) {

	Arrays.sort( tableNumbers ); //get 'em in the right order (not that it really matters)

	//render the where and from parts
	int drivingTable = tableNumbers[0];
	final String drivingAlias = generateTableAlias( getRootAlias(), drivingTable ); //we *could* regerate this inside each called method!
	final String where = createWhereByKey( drivingTable, drivingAlias );
	final String from = createFrom( drivingTable, drivingAlias );

	//now render the joins
	JoinFragment jf = createJoin( tableNumbers, drivingAlias );

	//now render the select clause
	SelectFragment selectFragment = createSelect( columnNumbers, formulaNumbers );

	//now tie it all together
	Select select = new Select( getFactory().getDialect() );
	select.setSelectClause( selectFragment.toFragmentString().substring( 2 ) );
	select.setFromClause( from );
	select.setWhereClause( where );
	select.setOuterJoins( jf.toFromFragmentString(), jf.toWhereFragmentString() );
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		select.setComment( "sequential select " + getEntityName() );
	}
	return select.toStatementString();
}
 
Example 4
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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();
}
 
Example 5
Source File: AbstractStatementExecutor.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected String generateIdInsertSelect(Queryable persister, String tableAlias, AST whereClause) {
	Select select = new Select( getFactory().getDialect() );
	SelectFragment selectFragment = new SelectFragment()
			.addColumns( tableAlias, persister.getIdentifierColumnNames(), persister.getIdentifierColumnNames() );
	select.setSelectClause( selectFragment.toFragmentString().substring( 2 ) );

	String rootTableName = persister.getTableName();
	String fromJoinFragment = persister.fromJoinFragment( tableAlias, true, false );
	String whereJoinFragment = persister.whereJoinFragment( tableAlias, true, false );

	select.setFromClause( rootTableName + ' ' + tableAlias + fromJoinFragment );

	if ( whereJoinFragment == null ) {
		whereJoinFragment = "";
	}
	else {
		whereJoinFragment = whereJoinFragment.trim();
		if ( whereJoinFragment.startsWith( "and" ) ) {
			whereJoinFragment = whereJoinFragment.substring( 4 );
		}
	}

	String userWhereClause = "";
	if ( whereClause.getNumberOfChildren() != 0 ) {
		// If a where clause was specified in the update/delete query, use it to limit the
		// returned ids here...
		try {
			SqlGenerator sqlGenerator = new SqlGenerator( getFactory() );
			sqlGenerator.whereClause( whereClause );
			userWhereClause = sqlGenerator.getSQL().substring( 7 );  // strip the " where "
		}
		catch ( RecognitionException e ) {
			throw new HibernateException( "Unable to generate id select for DML operation", e );
		}
		if ( whereJoinFragment.length() > 0 ) {
			whereJoinFragment += " and ";
		}
	}

	select.setWhereClause( whereJoinFragment + userWhereClause );

	InsertSelect insert = new InsertSelect( getFactory().getDialect() );
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		insert.setComment( "insert-select for " + persister.getEntityName() + " ids" );
	}
	insert.setTableName( persister.getTemporaryIdTableName() );
	insert.setSelect( select );
	return insert.toStatementString();
}