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

The following examples show how to use org.hibernate.sql.Select#toStatementString() . 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: AbstractEntityJoinWalker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void initStatementString(
		final String projection,
		final String condition,
		final String orderBy,
		final String groupBy,
		final LockOptions lockOptions) throws MappingException {

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

	JoinFragment ojf = mergeOuterJoins( associations );

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

	if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
		select.setComment( getComment() );
	}
	sql = select.toStatementString();
}
 
Example 2
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 3
Source File: AbstractEntityJoinWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 4
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 5
Source File: BasicCollectionJoinWalker.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void initStatementString(
	final String alias,
	final int batchSize,
	final String subquery) throws MappingException {

	final int joins = countEntityPersisters( associations );
	final int collectionJoins = countCollectionPersisters( associations ) + 1;

	suffixes = BasicLoader.generateSuffixes( joins );
	collectionSuffixes = BasicLoader.generateSuffixes( joins, collectionJoins );

	StringBuilder whereString = whereString(
			alias, 
			collectionPersister.getKeyColumnNames(), 
			subquery,
			batchSize
		);

	String manyToManyOrderBy = "";
	String filter = collectionPersister.filterFragment( alias, getLoadQueryInfluencers().getEnabledFilters() );
	if ( collectionPersister.isManyToMany() ) {
		// from the collection of associations, locate OJA for the
		// ManyToOne corresponding to this persister to fully
		// define the many-to-many; we need that OJA so that we can
		// use its alias here
		// TODO : is there a better way here?
		Iterator itr = associations.iterator();
		AssociationType associationType = ( AssociationType ) collectionPersister.getElementType();
		while ( itr.hasNext() ) {
			OuterJoinableAssociation oja = ( OuterJoinableAssociation ) itr.next();
			if ( oja.getJoinableType() == associationType ) {
				// we found it
				filter += collectionPersister.getManyToManyFilterFragment( 
						oja.getRHSAlias(), 
						getLoadQueryInfluencers().getEnabledFilters() 
					);
				manyToManyOrderBy += collectionPersister.getManyToManyOrderByString( oja.getRHSAlias() );
			}
		}
	}
	whereString.insert( 0, StringHelper.moveAndToBeginning( filter ) );

	JoinFragment ojf = mergeOuterJoins(associations);
	Select select = new Select( getDialect() )
		.setSelectClause(
			collectionPersister.selectFragment(alias, collectionSuffixes[0] ) +
			selectString(associations)
		)
		.setFromClause( collectionPersister.getTableName(), alias )
		.setWhereClause( whereString.toString()	)
		.setOuterJoins(
			ojf.toFromFragmentString(),
			ojf.toWhereFragmentString()
		);

	select.setOrderByClause( orderBy( associations, mergeOrderings( collectionPersister.getSQLOrderByString(alias), manyToManyOrderBy ) ) );

	if ( getFactory().getSettings().isCommentsEnabled() ) {
		select.setComment( "load collection " + collectionPersister.getRole() );
	}

	sql = select.toStatementString();
}
 
Example 6
Source File: OneToManyJoinWalker.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
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();
}
 
Example 7
Source File: BasicCollectionJoinWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void initStatementString(
	final String alias,
	final int batchSize,
	final String subquery)
throws MappingException {

	final int joins = countEntityPersisters( associations );
	final int collectionJoins = countCollectionPersisters( associations ) + 1;

	suffixes = BasicLoader.generateSuffixes( joins );
	collectionSuffixes = BasicLoader.generateSuffixes( joins, collectionJoins );

	StringBuffer whereString = whereString(
			alias, 
			collectionPersister.getKeyColumnNames(), 
			subquery,
			batchSize
		);

	String manyToManyOrderBy = "";
	String filter = collectionPersister.filterFragment( alias, getEnabledFilters() );
	if ( collectionPersister.isManyToMany() ) {
		// from the collection of associations, locate OJA for the
		// ManyToOne corresponding to this persister to fully
		// define the many-to-many; we need that OJA so that we can
		// use its alias here
		// TODO : is there a better way here?
		Iterator itr = associations.iterator();
		AssociationType associationType = ( AssociationType ) collectionPersister.getElementType();
		while ( itr.hasNext() ) {
			OuterJoinableAssociation oja = ( OuterJoinableAssociation ) itr.next();
			if ( oja.getJoinableType() == associationType ) {
				// we found it
				filter += collectionPersister.getManyToManyFilterFragment( 
						oja.getRHSAlias(), 
						getEnabledFilters() 
					);
					manyToManyOrderBy += collectionPersister.getManyToManyOrderByString( oja.getRHSAlias() );
			}
		}
	}
	whereString.insert( 0, StringHelper.moveAndToBeginning( filter ) );

	JoinFragment ojf = mergeOuterJoins(associations);
	Select select = new Select( getDialect() )
		.setSelectClause(
			collectionPersister.selectFragment(alias, collectionSuffixes[0] ) +
			selectString(associations)
		)
		.setFromClause( collectionPersister.getTableName(), alias )
		.setWhereClause( whereString.toString()	)
		.setOuterJoins(
			ojf.toFromFragmentString(),
			ojf.toWhereFragmentString()
		);

	select.setOrderByClause( orderBy( associations, mergeOrderings( collectionPersister.getSQLOrderByString(alias), manyToManyOrderBy ) ) );

	if ( getFactory().getSettings().isCommentsEnabled() ) {
		select.setComment( "load collection " + collectionPersister.getRole() );
	}

	sql = select.toStatementString();
}
 
Example 8
Source File: OneToManyJoinWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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 );

	StringBuffer whereString = whereString(
			alias, 
			oneToManyPersister.getKeyColumnNames(), 
			subquery,
			batchSize
		);
	String filter = oneToManyPersister.filterFragment( alias, 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().getSettings().isCommentsEnabled() ) {
		select.setComment( "load one-to-many " + oneToManyPersister.getRole() );
	}

	sql = select.toStatementString();
}