Java Code Examples for org.hibernate.persister.collection.QueryableCollection#getKeyColumnNames()

The following examples show how to use org.hibernate.persister.collection.QueryableCollection#getKeyColumnNames() . 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: AbstractEmptinessExpression.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
	final String entityName = criteriaQuery.getEntityName( criteria, propertyName );
	final String actualPropertyName = criteriaQuery.getPropertyName( propertyName );
	final String sqlAlias = criteriaQuery.getSQLAlias( criteria, propertyName );

	final SessionFactoryImplementor factory = criteriaQuery.getFactory();
	final QueryableCollection collectionPersister = getQueryableCollection( entityName, actualPropertyName, factory );

	final String[] collectionKeys = collectionPersister.getKeyColumnNames();
	final String[] ownerKeys = ( (Loadable) factory.getEntityPersister( entityName ) ).getIdentifierColumnNames();

	final String innerSelect = "(select 1 from " + collectionPersister.getTableName() + " where "
			+ new ConditionFragment().setTableAlias( sqlAlias ).setCondition( ownerKeys, collectionKeys ).toFragmentString()
			+ ")";

	return excludeEmpty()
			? "exists " + innerSelect
			: "not exists " + innerSelect;
}
 
Example 2
Source File: SizeExpression.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
	final String entityName =criteriaQuery.getEntityName( criteria, propertyName );
	final String role = entityName + '.' + criteriaQuery.getPropertyName( propertyName );
	final QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory().getCollectionPersister( role );

	final String[] fk = cp.getKeyColumnNames();
	final String[] pk = ( (Loadable) cp.getOwnerEntityPersister() ).getIdentifierColumnNames();

	final ConditionFragment subQueryRestriction = new ConditionFragment()
			.setTableAlias( criteriaQuery.getSQLAlias( criteria, propertyName ) )
			.setCondition( pk, fk );

	return String.format(
			Locale.ROOT,
			"? %s (select count(*) from %s where %s)",
			op,
			cp.getTableName(),
			subQueryRestriction.toFragmentString()
	);
}
 
Example 3
Source File: AbstractEmptinessExpression.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public final String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
	String entityName = criteriaQuery.getEntityName( criteria, propertyName );
	String actualPropertyName = criteriaQuery.getPropertyName( propertyName );
	String sqlAlias = criteriaQuery.getSQLAlias( criteria, propertyName );

	SessionFactoryImplementor factory = criteriaQuery.getFactory();
	QueryableCollection collectionPersister = getQueryableCollection( entityName, actualPropertyName, factory );

	String[] collectionKeys = collectionPersister.getKeyColumnNames();
	String[] ownerKeys = ( ( Loadable ) factory.getEntityPersister( entityName ) ).getIdentifierColumnNames();

	String innerSelect = "(select 1 from " + collectionPersister.getTableName()
	        + " where "
	        + new ConditionFragment().setTableAlias( sqlAlias ).setCondition( ownerKeys, collectionKeys ).toFragmentString()
	        + ")";

	return excludeEmpty()
	        ? "exists " + innerSelect
	        : "not exists " + innerSelect;
}
 
Example 4
Source File: SizeExpression.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
	String role = criteriaQuery.getEntityName(criteria, propertyName) + 
			'.' +  
			criteriaQuery.getPropertyName(propertyName);
	QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory()
			.getCollectionPersister(role);
	//String[] fk = StringHelper.qualify( "collection_", cp.getKeyColumnNames() );
	String[] fk = cp.getKeyColumnNames();
	String[] pk = ( (Loadable) cp.getOwnerEntityPersister() ).getIdentifierColumnNames(); //TODO: handle property-ref
	return "? " + 
			op + 
			" (select count(*) from " +
			cp.getTableName() +
			//" collection_ where " +
			" where " +
			new ConditionFragment()
					.setTableAlias( criteriaQuery.getSQLAlias(criteria, propertyName) )
					.setCondition(pk, fk)
					.toFragmentString() +
			")";
}
 
Example 5
Source File: HqlSqlWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected AST createFromFilterElement(AST filterEntity, AST alias) throws SemanticException {
	FromElement fromElement = currentFromClause.addFromElement( filterEntity.getText(), alias );
	FromClause fromClause = fromElement.getFromClause();
	QueryableCollection persister = sessionFactoryHelper.getCollectionPersister( collectionFilterRole );
	// Get the names of the columns used to link between the collection
	// owner and the collection elements.
	String[] keyColumnNames = persister.getKeyColumnNames();
	String fkTableAlias = persister.isOneToMany()
			? fromElement.getTableAlias()
			: fromClause.getAliasGenerator().createName( collectionFilterRole );
	JoinSequence join = sessionFactoryHelper.createJoinSequence();
	join.setRoot( persister, fkTableAlias );
	if ( !persister.isOneToMany() ) {
		join.addJoin( ( AssociationType ) persister.getElementType(),
				fromElement.getTableAlias(),
				JoinFragment.INNER_JOIN,
				persister.getElementColumnNames( fkTableAlias ) );
	}
	join.addCondition( fkTableAlias, keyColumnNames, " = ?" );
	fromElement.setJoinSequence( join );
	fromElement.setFilter( true );
	if ( log.isDebugEnabled() ) {
		log.debug( "createFromFilterElement() : processed filter FROM element." );
	}
	return fromElement;
}
 
Example 6
Source File: HqlSqlWalker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected AST createFromFilterElement(AST filterEntity, AST alias) throws SemanticException {
	FromElement fromElement = currentFromClause.addFromElement( filterEntity.getText(), alias );
	FromClause fromClause = fromElement.getFromClause();
	QueryableCollection persister = sessionFactoryHelper.getCollectionPersister( collectionFilterRole );
	// Get the names of the columns used to link between the collection
	// owner and the collection elements.
	String[] keyColumnNames = persister.getKeyColumnNames();
	String fkTableAlias = persister.isOneToMany()
			? fromElement.getTableAlias()
			: fromClause.getAliasGenerator().createName( collectionFilterRole );
	JoinSequence join = sessionFactoryHelper.createJoinSequence();
	join.setRoot( persister, fkTableAlias );
	if ( !persister.isOneToMany() ) {
		join.addJoin(
				(AssociationType) persister.getElementType(),
				fromElement.getTableAlias(),
				JoinType.INNER_JOIN,
				persister.getElementColumnNames( fkTableAlias )
		);
	}
	join.addCondition( fkTableAlias, keyColumnNames, " = ?" );
	fromElement.setJoinSequence( join );
	fromElement.setFilter( true );
	LOG.debug( "createFromFilterElement() : processed filter FROM element." );
	return fromElement;
}
 
Example 7
Source File: QueryTranslatorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Used for collection filters
 */
private void addFromAssociation(final String elementName, final String collectionRole)
		throws QueryException {
	//q.addCollection(collectionName, collectionRole);
	QueryableCollection persister = getCollectionPersister( collectionRole );
	Type collectionElementType = persister.getElementType();
	if ( !collectionElementType.isEntityType() ) {
		throw new QueryException( "collection of values in filter: " + elementName );
	}

	String[] keyColumnNames = persister.getKeyColumnNames();
	//if (keyColumnNames.length!=1) throw new QueryException("composite-key collection in filter: " + collectionRole);

	String collectionName;
	JoinSequence join = new JoinSequence( getFactory() );
	collectionName = persister.isOneToMany() ?
			elementName :
			createNameForCollection( collectionRole );
	join.setRoot( persister, collectionName );
	if ( !persister.isOneToMany() ) {
		//many-to-many
		addCollection( collectionName, collectionRole );
		try {
			join.addJoin(
					(AssociationType) persister.getElementType(),
					elementName,
					JoinType.INNER_JOIN,
					persister.getElementColumnNames( collectionName )
			);
		}
		catch (MappingException me) {
			throw new QueryException( me );
		}
	}
	join.addCondition( collectionName, keyColumnNames, " = ?" );
	//if ( persister.hasWhere() ) join.addCondition( persister.getSQLWhereString(collectionName) );
	EntityType elemType = (EntityType) collectionElementType;
	addFrom( elementName, elemType.getAssociatedEntityName(), join );

}
 
Example 8
Source File: QueryTranslatorImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Used for collection filters
 */
private void addFromAssociation(final String elementName, final String collectionRole)
		throws QueryException {
	//q.addCollection(collectionName, collectionRole);
	QueryableCollection persister = getCollectionPersister( collectionRole );
	Type collectionElementType = persister.getElementType();
	if ( !collectionElementType.isEntityType() ) {
		throw new QueryException( "collection of values in filter: " + elementName );
	}

	String[] keyColumnNames = persister.getKeyColumnNames();
	//if (keyColumnNames.length!=1) throw new QueryException("composite-key collection in filter: " + collectionRole);

	String collectionName;
	JoinSequence join = new JoinSequence( getFactory() );
	collectionName = persister.isOneToMany() ?
			elementName :
			createNameForCollection( collectionRole );
	join.setRoot( persister, collectionName );
	if ( !persister.isOneToMany() ) {
		//many-to-many
		addCollection( collectionName, collectionRole );
		try {
			join.addJoin( ( AssociationType ) persister.getElementType(),
					elementName,
					JoinFragment.INNER_JOIN,
					persister.getElementColumnNames(collectionName) );
		}
		catch ( MappingException me ) {
			throw new QueryException( me );
		}
	}
	join.addCondition( collectionName, keyColumnNames, " = ?" );
	//if ( persister.hasWhere() ) join.addCondition( persister.getSQLWhereString(collectionName) );
	EntityType elemType = ( EntityType ) collectionElementType;
	addFrom( elementName, elemType.getAssociatedEntityName(), join );

}