Java Code Examples for org.hibernate.util.ArrayHelper

The following examples show how to use org.hibernate.util.ArrayHelper. These examples are extracted from open source projects. 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
public static UniqueEntityLoader createBatchingEntityLoader(
	final OuterJoinLoadable persister,
	final int maxBatchSize,
	final LockMode lockMode,
	final SessionFactoryImplementor factory,
	final Map enabledFilters)
throws MappingException {

	if ( maxBatchSize>1 ) {
		int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize);
		Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
		for ( int i=0; i<batchSizesToCreate.length; i++ ) {
			loadersToCreate[i] = new EntityLoader(persister, batchSizesToCreate[i], lockMode, factory, enabledFilters);
		}
		return new BatchingEntityLoader(persister, batchSizesToCreate, loadersToCreate);
	}
	else {
		return new EntityLoader(persister, lockMode, factory, enabledFilters);
	}
}
 
Example 2
protected void autoDiscoverTypes(ResultSet rs) {
	try {
		Metadata metadata = new Metadata( getFactory(), rs );
		List aliases = new ArrayList();
		List types = new ArrayList();

		rowProcessor.prepareForAutoDiscovery( metadata );

		for ( int i = 0; i < rowProcessor.columnProcessors.length; i++ ) {
			rowProcessor.columnProcessors[i].performDiscovery( metadata, types, aliases );
		}

		resultTypes = ArrayHelper.toTypeArray( types );
		transformerAliases = ArrayHelper.toStringArray( aliases );
	}
	catch ( SQLException e ) {
		throw new HibernateException( "Exception while trying to autodiscover types.", e );
	}
}
 
Example 3
public static CollectionInitializer createBatchingOneToManyInitializer(
	final QueryableCollection persister,
	final int maxBatchSize,
	final SessionFactoryImplementor factory,
	final Map enabledFilters)
throws MappingException {

	if ( maxBatchSize>1 ) {
		int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize);
		Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
		for ( int i=0; i<batchSizesToCreate.length; i++ ) {
			loadersToCreate[i] = new OneToManyLoader(persister, batchSizesToCreate[i], factory, enabledFilters);
		}
		return new BatchingCollectionInitializer(persister, batchSizesToCreate, loadersToCreate);
	}
	else {
		return new OneToManyLoader(persister, factory, enabledFilters);
	}
}
 
Example 4
public static CollectionInitializer createBatchingCollectionInitializer(
	final QueryableCollection persister,
	final int maxBatchSize,
	final SessionFactoryImplementor factory,
	final Map enabledFilters)
throws MappingException {

	if ( maxBatchSize>1 ) {
		int[] batchSizesToCreate = ArrayHelper.getBatchSizes(maxBatchSize);
		Loader[] loadersToCreate = new Loader[ batchSizesToCreate.length ];
		for ( int i=0; i<batchSizesToCreate.length; i++ ) {
			loadersToCreate[i] = new BasicCollectionLoader(persister, batchSizesToCreate[i], factory, enabledFilters);
		}
		return new BatchingCollectionInitializer(persister, batchSizesToCreate, loadersToCreate);
	}
	else {
		return new BasicCollectionLoader(persister, factory, enabledFilters);
	}
}
 
Example 5
/**
 * Warning:
 * When there are duplicated property names in the subclasses
 * of the class, this method may return the wrong table
 * number for the duplicated subclass property (note that
 * SingleTableEntityPersister defines an overloaded form
 * which takes the entity name.
 */
public int getSubclassPropertyTableNumber(String propertyPath) {
	String rootPropertyName = StringHelper.root(propertyPath);
	Type type = propertyMapping.toType(rootPropertyName);
	if ( type.isAssociationType() ) {
		AssociationType assocType = ( AssociationType ) type;
		if ( assocType.useLHSPrimaryKey() ) {
			// performance op to avoid the array search
			return 0;
		}
		else if ( type.isCollectionType() ) {
			// properly handle property-ref-based associations
			rootPropertyName = assocType.getLHSPropertyName();
		}
	}
	//Enable for HHH-440, which we don't like:
	/*if ( type.isComponentType() && !propertyName.equals(rootPropertyName) ) {
		String unrooted = StringHelper.unroot(propertyName);
		int idx = ArrayHelper.indexOf( getSubclassColumnClosure(), unrooted );
		if ( idx != -1 ) {
			return getSubclassColumnTableNumberClosure()[idx];
		}
	}*/
	int index = ArrayHelper.indexOf( getSubclassPropertyNameClosure(), rootPropertyName); //TODO: optimize this better!
	return index==-1 ? 0 : getSubclassPropertyTableNumber(index);
}
 
Example 6
/**
 * Generate the SQL UPDATE that updates a row
 */
protected String generateUpdateRowString() {
	
	Update update = new Update( getDialect() )
		.setTableName( qualifiedTableName );
	
	//if ( !elementIsFormula ) {
		update.addColumns( elementColumnNames, elementColumnIsSettable );
	//}
	
	if ( hasIdentifier ) {
		update.setPrimaryKeyColumnNames( new String[]{ identifierColumnName } );
	}
	else if ( hasIndex && !indexContainsFormula ) {
		update.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, indexColumnNames ) );
	}
	else {
		update.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, elementColumnNames, elementColumnIsInPrimaryKey ) );
	}
	
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		update.setComment( "update collection row " + getRole() );
	}
	
	return update.toStatementString();
}
 
Example 7
/**
 * Generate the SQL DELETE that deletes a particular row
 */
protected String generateDeleteRowString() {
	
	Delete delete = new Delete()
		.setTableName( qualifiedTableName );
	
	if ( hasIdentifier ) {
		delete.setPrimaryKeyColumnNames( new String[]{ identifierColumnName } );
	}
	else if ( hasIndex && !indexContainsFormula ) {
		delete.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, indexColumnNames ) );
	}
	else {
		delete.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, elementColumnNames, elementColumnIsInPrimaryKey ) );
	}
	
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		delete.setComment( "delete collection row " + getRole() );
	}
	
	return delete.toStatementString();
}
 
Example 8
/**
 * Generate the SQL UPDATE that updates a particular row's foreign
 * key to null
 */
protected String generateDeleteRowString() {
	
	Update update = new Update( getDialect() )
			.setTableName( qualifiedTableName )
			.addColumns( keyColumnNames, "null" );
	
	if ( hasIndex && !indexContainsFormula ) update.addColumns( indexColumnNames, "null" );
	
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		update.setComment( "delete one-to-many row " + getRole() );
	}
	
	//use a combination of foreign key columns and pk columns, since
	//the ordering of removal and addition is not guaranteed when
	//a child moves from one parent to another
	String[] rowSelectColumnNames = ArrayHelper.join(keyColumnNames, elementColumnNames);
	return update.setPrimaryKeyColumnNames( rowSelectColumnNames )
			.toStatementString();
}
 
Example 9
public NativeSQLQuerySpecification(
		String queryString,
        NativeSQLQueryReturn[] queryReturns,
        Collection querySpaces) {
	this.queryString = queryString;
	this.queryReturns = queryReturns;
	if ( querySpaces == null ) {
		this.querySpaces = Collections.EMPTY_SET;
	}
	else {
		Set tmp = new HashSet();
		tmp.addAll( querySpaces );
		this.querySpaces = Collections.unmodifiableSet( tmp );
	}

	// pre-determine and cache the hashcode
	int hashCode = queryString.hashCode();
	hashCode = 29 * hashCode + this.querySpaces.hashCode();
	if ( this.queryReturns != null ) {
		hashCode = 29 * hashCode + ArrayHelper.toList( this.queryReturns ).hashCode();
	}
	this.hashCode = hashCode;
}
 
Example 10
/**
 * Get the aliased columns of the owning entity which are to 
 * be used in the join
 */
public static String[] getAliasedLHSColumnNames(
		AssociationType type, 
		String alias, 
		int property, 
		int begin, 
		OuterJoinLoadable lhsPersister,
		Mapping mapping
) {
	if ( type.useLHSPrimaryKey() ) {
		return StringHelper.qualify( alias, lhsPersister.getIdentifierColumnNames() );
	}
	else {
		String propertyName = type.getLHSPropertyName();
		if (propertyName==null) {
			return ArrayHelper.slice( 
					lhsPersister.toColumns(alias, property), 
					begin, 
					type.getColumnSpan(mapping) 
				);
		}
		else {
			return ( (PropertyMapping) lhsPersister ).toColumns(alias, propertyName); //bad cast
		}
	}
}
 
Example 11
public boolean isUpdateable() {
	// if the property mapping consists of all formulas, 
	// make it non-updateable
	final boolean[] columnUpdateability = value.getColumnUpdateability();
	return updateable && ( 
			//columnUpdateability.length==0 ||
			!ArrayHelper.isAllFalse(columnUpdateability)
		);
}
 
Example 12
public boolean isInsertable() {
	// if the property mapping consists of all formulas, 
	// make it insertable
	final boolean[] columnInsertability = value.getColumnInsertability();
	return insertable && (
			columnInsertability.length==0 ||
			!ArrayHelper.isAllFalse(columnInsertability)
		);
}
 
Example 13
public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
	List types = new ArrayList( getLength() );
	for ( int i=0; i<getLength(); i++ ) {
		Type[] elemTypes = getProjection(i).getTypes(criteria, criteriaQuery);
		ArrayHelper.addAll(types, elemTypes);
	}
	return ArrayHelper.toTypeArray(types);
}
 
Example 14
public String[] getColumnAliases(int loc) {
	List result = new ArrayList( getLength() );
	for ( int i=0; i<getLength(); i++ ) {
		String[] colAliases = getProjection(i).getColumnAliases(loc);
		ArrayHelper.addAll(result, colAliases);
		loc+=colAliases.length;
	}
	return ArrayHelper.toStringArray(result);
}
 
Example 15
public String[] getAliases() {
	List result = new ArrayList( getLength() );
	for ( int i=0; i<getLength(); i++ ) {
		String[] aliases = getProjection(i).getAliases();
		ArrayHelper.addAll(result, aliases);
	}
	return ArrayHelper.toStringArray(result);

}
 
Example 16
public CollectionElementLoader(
		QueryableCollection collectionPersister,
		SessionFactoryImplementor factory, 
		Map enabledFilters) 
throws MappingException {
	super(factory, enabledFilters);

	this.keyType = collectionPersister.getKeyType();
	this.indexType = collectionPersister.getIndexType();
	this.persister = (OuterJoinLoadable) collectionPersister.getElementPersister();
	this.entityName = persister.getEntityName();
	
	JoinWalker walker = new EntityJoinWalker(
			persister, 
			ArrayHelper.join( 
					collectionPersister.getKeyColumnNames(), 
					collectionPersister.getIndexColumnNames()
				),
			1, 
			LockMode.NONE, 
			factory, 
			enabledFilters
		);
	initFromWalker( walker );

	postInstantiate();
	
	log.debug( "Static select for entity " + entityName + ": " + getSQLString() );

}
 
Example 17
public CriteriaJoinWalker(
		final OuterJoinLoadable persister, 
		final CriteriaQueryTranslator translator,
		final SessionFactoryImplementor factory, 
		final CriteriaImpl criteria, 
		final String rootEntityName,
		final Map enabledFilters)
throws HibernateException {
	super(persister, factory, enabledFilters);

	this.translator = translator;

	querySpaces = translator.getQuerySpaces();

	if ( translator.hasProjection() ) {
		resultTypes = translator.getProjectedTypes();
		
		initProjection( 
				translator.getSelect(), 
				translator.getWhereCondition(), 
				translator.getOrderBy(),
				translator.getGroupBy(),
				LockMode.NONE 
			);
	}
	else {
		resultTypes = new Type[] { TypeFactory.manyToOne( persister.getEntityName() ) };

		initAll( translator.getWhereCondition(), translator.getOrderBy(), LockMode.NONE );
	}
	
	userAliasList.add( criteria.getAlias() ); //root entity comes *last*
	userAliases = ArrayHelper.toStringArray(userAliasList);

}
 
Example 18
public int[] getNamedParameterLocs(String name) throws QueryException {
	Object loc = namedParameterBindPoints.get( name );
	if ( loc == null ) {
		throw new QueryException(
				"Named parameter does not appear in Query: " + name,
				sql
		);
	}
	if ( loc instanceof Integer ) {
		return new int[] { ( ( Integer ) loc ).intValue() };
	}
	else {
		return ArrayHelper.toIntArray( ( List ) loc );
	}
}
 
Example 19
/**
 * Marshall the fields of a persistent instance to a prepared statement
 */
protected int dehydrate(
		final Serializable id,
        final Object[] fields,
        final Object rowId,
        final boolean[] includeProperty,
        final boolean[][] includeColumns,
        final int j,
        final PreparedStatement ps,
        final SessionImplementor session,
        int index) throws SQLException, HibernateException {

	if ( log.isTraceEnabled() ) {
		log.trace( "Dehydrating entity: " + MessageHelper.infoString( this, id, getFactory() ) );
	}

	for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {
		if ( includeProperty[i] && isPropertyOfTable( i, j ) ) {
			getPropertyTypes()[i].nullSafeSet( ps, fields[i], index, includeColumns[i], session );
			//index += getPropertyColumnSpan( i );
			index += ArrayHelper.countTrue( includeColumns[i] ); //TODO:  this is kinda slow...
		}
	}

	if ( rowId != null ) {
		ps.setObject( index, rowId );
		index += 1;
	}
	else if ( id != null ) {
		getIdentifierType().nullSafeSet( ps, id, index, session );
		index += getIdentifierColumnSpan();
	}

	return index;

}
 
Example 20
/**
 * Write the element to a JDBC <tt>PreparedStatement</tt>
 */
protected int writeElement(PreparedStatement st, Object elt, int i, SessionImplementor session)
		throws HibernateException, SQLException {
	getElementType().nullSafeSet(st, elt, i, elementColumnIsSettable, session);
	return i + ArrayHelper.countTrue(elementColumnIsSettable);

}
 
Example 21
private int[] getNamedParameterLocs(String name) throws QueryException {
	Object loc = customQuery.getNamedParameterBindPoints().get( name );
	if ( loc == null ) {
		throw new QueryException(
				"Named parameter does not appear in Query: " + name,
				customQuery.getSQL() );
	}
	if ( loc instanceof Integer ) {
		return new int[] { ((Integer) loc ).intValue() };
	}
	else {
		return ArrayHelper.toIntArray( (List) loc );
	}
}
 
Example 22
/**
 * Get the columns of the owning entity which are to 
 * be used in the join
 */
public static String[] getLHSColumnNames(
		AssociationType type, 
		int property, 
		int begin, 
		OuterJoinLoadable lhsPersister,
		Mapping mapping
) {
	if ( type.useLHSPrimaryKey() ) {
		//return lhsPersister.getSubclassPropertyColumnNames(property);
		return lhsPersister.getIdentifierColumnNames();
	}
	else {
		String propertyName = type.getLHSPropertyName();
		if (propertyName==null) {
			//slice, to get the columns for this component
			//property
			return ArrayHelper.slice( 
					lhsPersister.getSubclassPropertyColumnNames(property),
					begin, 
					type.getColumnSpan(mapping) 
				);
		}
		else {
			//property-refs for associations defined on a
			//component are not supported, so no need to slice
			return lhsPersister.getPropertyColumnNames(propertyName);
		}
	}
}
 
Example 23
/**
 * <p>Creates a compact stack trace for the root cause of the supplied
 * <code>Throwable</code>.</p>
 *
 * @param throwable the throwable to examine, may be null
 * @return an array of stack trace frames, never null
 * @since 2.0
 */
public static String[] getRootCauseStackTrace(Throwable throwable) {
	if ( throwable == null ) {
		return ArrayHelper.EMPTY_STRING_ARRAY;
	}
	Throwable throwables[] = getThrowables( throwable );
	int count = throwables.length;
	ArrayList frames = new ArrayList();
	List nextTrace = getStackFrameList( throwables[count - 1] );
	for ( int i = count; --i >= 0; ) {
		List trace = nextTrace;
		if ( i != 0 ) {
			nextTrace = getStackFrameList( throwables[i - 1] );
			removeCommonFrames( trace, nextTrace );
		}
		if ( i == count - 1 ) {
			frames.add( throwables[i].toString() );
		}
		else {
			frames.add( WRAPPED_MARKER + throwables[i].toString() );
		}
		for ( int j = 0; j < trace.size(); j++ ) {
			frames.add( trace.get( j ) );
		}
	}
	return ( String[] ) frames.toArray( new String[0] );
}
 
Example 24
/**
 * Get the names of all entities
 */
public String[] getEntityNames() {
	if (sessionFactory==null) {
		return ArrayHelper.toStringArray( entityStatistics.keySet() );
	}
	else {
		return ArrayHelper.toStringArray( sessionFactory.getAllClassMetadata().keySet() );
	}
}
 
Example 25
/**
 * Get the names of all collection roles
 */
public String[] getCollectionRoleNames() {
	if (sessionFactory==null) {
		return ArrayHelper.toStringArray( collectionStatistics.keySet() );
	}
	else {
		return ArrayHelper.toStringArray( sessionFactory.getAllCollectionMetadata().keySet() );
	}
}
 
Example 26
/**
 * Get all second-level cache region names
 */
public String[] getSecondLevelCacheRegionNames() {
	if (sessionFactory==null) {
		return ArrayHelper.toStringArray( secondLevelCacheStatistics.keySet() );
	}
	else {
		return ArrayHelper.toStringArray( sessionFactory.getAllSecondLevelCacheRegions().keySet() );
	}
}
 
Example 27
private void initializeColumns() {
	AST propertySpec = getFirstChild();
	List types = new ArrayList();
	visitPropertySpecNodes( propertySpec.getFirstChild(), types );
	this.types = ArrayHelper.toTypeArray( types );
	columnSpec = columnSpec.substring( 0, columnSpec.length() - 2 );
}
 
Example 28
/**
 * Returns the locations of all occurrences of the named parameter.
 */
public int[] getNamedParameterLocations(String name) throws QueryException {
	Object o = namedParameters.get( name );
	if ( o == null ) {
		QueryException qe = new QueryException( QueryTranslator.ERROR_NAMED_PARAMETER_DOES_NOT_APPEAR + name );
		qe.setQueryString( queryTranslatorImpl.getQueryString() );
		throw qe;
	}
	if ( o instanceof Integer ) {
		return new int[]{( ( Integer ) o ).intValue()};
	}
	else {
		return ArrayHelper.toIntArray( ( ArrayList ) o );
	}
}
 
Example 29
public int[] getNamedParameterLocs(String name) throws QueryException {
	Object o = namedParameters.get( name );
	if ( o == null ) {
		QueryException qe = new QueryException( ERROR_NAMED_PARAMETER_DOES_NOT_APPEAR + name );
		qe.setQueryString( queryString );
		throw qe;
	}
	if ( o instanceof Integer ) {
		return new int[]{ ( ( Integer ) o ).intValue() };
	}
	else {
		return ArrayHelper.toIntArray( ( ArrayList ) o );
	}
}
 
Example 30
public void nullSafeSet(
		PreparedStatement st,
		Object value,
		int begin,
		boolean[] settable,
		SessionImplementor session)
		throws HibernateException, SQLException {

	Object[] subvalues = nullSafeGetValues( value, session.getEntityMode() );

	int loc = 0;
	for ( int i = 0; i < propertySpan; i++ ) {
		int len = propertyTypes[i].getColumnSpan( session.getFactory() );
		if ( len == 0 ) {
			//noop
		}
		else if ( len == 1 ) {
			if ( settable[loc] ) {
				propertyTypes[i].nullSafeSet( st, subvalues[i], begin, session );
				begin++;
			}
		}
		else {
			boolean[] subsettable = new boolean[len];
			System.arraycopy( settable, loc, subsettable, 0, len );
			propertyTypes[i].nullSafeSet( st, subvalues[i], begin, subsettable, session );
			begin += ArrayHelper.countTrue( subsettable );
		}
		loc += len;
	}
}