Java Code Examples for org.hibernate.type.CompositeType#getSubtypes()

The following examples show how to use org.hibernate.type.CompositeType#getSubtypes() . 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: Example.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected void addComponentTypedValues(
		String path, 
		Object component, 
		CompositeType type,
		List<TypedValue> list,
		Criteria criteria, 
		CriteriaQuery criteriaQuery) {
	if ( component != null ) {
		final String[] propertyNames = type.getPropertyNames();
		final Type[] subtypes = type.getSubtypes();
		final Object[] values = type.getPropertyValues( component, getEntityMode( criteria, criteriaQuery ) );
		for ( int i=0; i<propertyNames.length; i++ ) {
			final Object value = values[i];
			final Type subtype = subtypes[i];
			final String subpath = StringHelper.qualify( path, propertyNames[i] );
			if ( isPropertyIncluded( value, subpath, subtype ) ) {
				if ( subtype.isComponentType() ) {
					addComponentTypedValues( subpath, value, (CompositeType) subtype, list, criteria, criteriaQuery );
				}
				else {
					addPropertyTypedValue( value, subtype, list );
				}
			}
		}
	}
}
 
Example 2
Source File: EntityJoinWalker.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void findKeyManyToOneTargetIndices(
		ArrayList<Integer> keyManyToOneTargetIndices,
		OuterJoinableAssociation joinWithCompositeId,
		CompositeType componentType) {
	for ( Type subType : componentType.getSubtypes() ) {
		if ( subType.isEntityType() ) {
			Integer index = locateKeyManyToOneTargetIndex( joinWithCompositeId, (EntityType) subType );
			if ( index != null ) {
				keyManyToOneTargetIndices.add( index );
			}
		}
		else if ( subType.isComponentType() ) {
			findKeyManyToOneTargetIndices(
					keyManyToOneTargetIndices,
					joinWithCompositeId,
					(CompositeType) subType
			);
		}
	}
}
 
Example 3
Source File: WrapVisitor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
Object processComponent(Object component, CompositeType componentType) throws HibernateException {
	if ( component != null ) {
		Object[] values = componentType.getPropertyValues( component, getSession() );
		Type[] types = componentType.getSubtypes();
		boolean substituteComponent = false;
		for ( int i = 0; i < types.length; i++ ) {
			Object result = processValue( values[i], types[i] );
			if ( result != null ) {
				values[i] = result;
				substituteComponent = true;
			}
		}
		if ( substituteComponent ) {
			componentType.setPropertyValues( component, values, EntityMode.POJO );
		}
	}

	return null;
}
 
Example 4
Source File: InExpressionIgnoringCase.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
	List<TypedValue> list = new ArrayList<>();
	Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName);
	if (type.isComponentType()) {
		CompositeType actype = (CompositeType) type;
		Type[] types = actype.getSubtypes();
		for (int j = 0; j < values.length; j++) {
			for (int i = 0; i < types.length; i++) {
				Object subval = values[j] == null ? null : actype.getPropertyValues(values[j], EntityMode.POJO)[i];
				list.add(new TypedValue(types[i], subval, EntityMode.POJO));
			}
		}
	} else {
		for (int j = 0; j < values.length; j++) {
			list.add(new TypedValue(type, values[j], EntityMode.POJO));
		}
	}
	return list.toArray(new TypedValue[list.size()]);
}
 
Example 5
Source File: Property.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static CascadeStyle getCompositeCascadeStyle(CompositeType compositeType, String cascade) {
	if ( compositeType.isAnyType() ) {
		return getCascadeStyle( cascade );
	}
	int length = compositeType.getSubtypes().length;
	for ( int i=0; i<length; i++ ) {
		if ( compositeType.getCascadeStyle(i) != CascadeStyles.NONE ) {
			return CascadeStyles.ALL;
		}
	}
	return getCascadeStyle( cascade );
}
 
Example 6
Source File: Example.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void appendComponentCondition(
		String path,
		Object component,
		CompositeType type,
		Criteria criteria,
		CriteriaQuery criteriaQuery,
		StringBuilder buf) {
	if ( component != null ) {
		final String[] propertyNames = type.getPropertyNames();
		final Object[] values = type.getPropertyValues( component, getEntityMode( criteria, criteriaQuery ) );
		final Type[] subtypes = type.getSubtypes();
		for ( int i=0; i<propertyNames.length; i++ ) {
			final String subPath = StringHelper.qualify( path, propertyNames[i] );
			final Object value = values[i];
			if ( isPropertyIncluded( value, subPath, subtypes[i] ) ) {
				final Type subtype = subtypes[i];
				if ( subtype.isComponentType() ) {
					appendComponentCondition(
							subPath,
							value,
							(CompositeType) subtype,
							criteria,
							criteriaQuery,
							buf
					);
				}
				else {
					appendPropertyCondition(
							subPath,
							value,
							criteria,
							criteriaQuery,
							buf
					);
				}
			}
		}
	}
}
 
Example 7
Source File: EntityJoinWalker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean hasAssociation(CompositeType componentType) {
	for ( Type subType : componentType.getSubtypes() ) {
		if ( subType.isEntityType() ) {
			return true;
		}
		else if ( subType.isComponentType() && hasAssociation( ( (CompositeType) subType ) ) ) {
			return true;
		}
	}
	return false;
}
 
Example 8
Source File: ComponentCollectionCriteriaInfoProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
ComponentCollectionCriteriaInfoProvider(QueryableCollection persister) {
	this.persister = persister;
	if ( !persister.getElementType().isComponentType() ) {
		throw new IllegalArgumentException( "persister for role " + persister.getRole() + " is not a collection-of-component" );
	}

	CompositeType componentType = (CompositeType) persister.getElementType();
	String[] names = componentType.getPropertyNames();
	Type[] types = componentType.getSubtypes();

	for ( int i = 0; i < names.length; i++ ) {
		subTypes.put( names[i], types[i] );
	}

}
 
Example 9
Source File: ForeignKeys.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return null if the argument is an "unsaved" entity (ie. one with no existing database row), or the
 * input argument otherwise.  This is how Hibernate avoids foreign key constraint violations.
 *
 * @param value An entity attribute value
 * @param type An entity attribute type
 *
 * @return {@code null} if the argument is an unsaved entity; otherwise return the argument.
 */
private Object nullifyTransientReferences(final Object value, final Type type) {
	if ( value == null ) {
		return null;
	}
	else if ( type.isEntityType() ) {
		final EntityType entityType = (EntityType) type;
		if ( entityType.isOneToOne() ) {
			return value;
		}
		else {
			final String entityName = entityType.getAssociatedEntityName();
			return isNullifiable( entityName, value ) ? null : value;
		}
	}
	else if ( type.isAnyType() ) {
		return isNullifiable( null, value ) ? null : value;
	}
	else if ( type.isComponentType() ) {
		final CompositeType actype = (CompositeType) type;
		final Object[] subvalues = actype.getPropertyValues( value, session );
		final Type[] subtypes = actype.getSubtypes();
		boolean substitute = false;
		for ( int i = 0; i < subvalues.length; i++ ) {
			final Object replacement = nullifyTransientReferences( subvalues[i], subtypes[i] );
			if ( replacement != subvalues[i] ) {
				substitute = true;
				subvalues[i] = replacement;
			}
		}
		if ( substitute ) {
			// todo : need to account for entity mode on the CompositeType interface :(
			actype.setPropertyValues( value, subvalues, EntityMode.POJO );
		}
		return value;
	}
	else {
		return value;
	}
}
 
Example 10
Source File: Cascade.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static void cascadeComponent(
		final CascadingAction action,
		final CascadePoint cascadePoint,
		final EventSource eventSource,
		final int componentPathStackDepth,
		final Object parent,
		final Object child,
		final CompositeType componentType,
		final Object anything) {

	Object[] children = null;
	final Type[] types = componentType.getSubtypes();
	final String[] propertyNames = componentType.getPropertyNames();
	for ( int i = 0; i < types.length; i++ ) {
		final CascadeStyle componentPropertyStyle = componentType.getCascadeStyle( i );
		final String subPropertyName = propertyNames[i];
		if ( componentPropertyStyle.doCascade( action ) ) {
			if (children == null) {
				// Get children on demand.
				children = componentType.getPropertyValues( child, eventSource );
			}
			cascadeProperty(
					action,
					cascadePoint,
					eventSource,
					componentPathStackDepth + 1,
					parent,
					children[i],
					types[i],
					componentPropertyStyle,
					subPropertyName,
					anything,
					false
				);
		}
	}
}
 
Example 11
Source File: Nullability.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * check component nullability. Returns property path that break
 * nullability or null if none
 *
 * @param value component properties
 * @param compositeType component not-nullable type
 *
 * @return property path
 * @throws HibernateException error while getting subcomponent values
 */
private String checkComponentNullability(Object value, CompositeType compositeType) throws HibernateException {
	// IMPL NOTE : we currently skip checking "any" and "many to any" mappings.
	//
	// This is not the best solution.  But atm there is a mismatch between AnyType#getPropertyNullability
	// and the fact that cascaded-saves for "many to any" mappings are not performed until after this nullability
	// check.  So the nullability check fails for transient entity elements with generated identifiers because
	// the identifier is not yet generated/assigned (is null)
	//
	// The more correct fix would be to cascade saves of the many-to-any elements before the Nullability checking

	if ( compositeType.isAnyType() ) {
		return null;
	}

	final boolean[] nullability = compositeType.getPropertyNullability();
	if ( nullability != null ) {
		//do the test
		final Object[] subValues = compositeType.getPropertyValues( value, session );
		final Type[] propertyTypes = compositeType.getSubtypes();
		for ( int i = 0; i < subValues.length; i++ ) {
			final Object subValue = subValues[i];
			if ( !nullability[i] && subValue==null ) {
				return compositeType.getPropertyNames()[i];
			}
			else if ( subValue != null ) {
				final String breakProperties = checkSubElementsNullability( propertyTypes[i], subValue );
				if ( breakProperties != null ) {
					return buildPropertyPath( compositeType.getPropertyNames()[i], breakProperties );
				}
			}
		}
	}
	return null;
}
 
Example 12
Source File: ReattachVisitor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
Object processComponent(Object component, CompositeType componentType) throws HibernateException {
	Type[] types = componentType.getSubtypes();
	if ( component == null ) {
		processValues( new Object[types.length], types );
	}
	else {
		super.processComponent( component, componentType );
	}

	return null;
}
 
Example 13
Source File: ForeignKeys.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private static void collectNonNullableTransientEntities(
		Nullifier nullifier,
		Object value,
		String propertyName,
		Type type,
		boolean isNullable,
		SharedSessionContractImplementor session,
		NonNullableTransientDependencies nonNullableTransientEntities) {
	if ( value == null ) {
		return;
	}

	if ( type.isEntityType() ) {
		final EntityType entityType = (EntityType) type;
		if ( !isNullable
				&& !entityType.isOneToOne()
				&& nullifier.isNullifiable( entityType.getAssociatedEntityName(), value ) ) {
			nonNullableTransientEntities.add( propertyName, value );
		}
	}
	else if ( type.isAnyType() ) {
		if ( !isNullable && nullifier.isNullifiable( null, value ) ) {
			nonNullableTransientEntities.add( propertyName, value );
		}
	}
	else if ( type.isComponentType() ) {
		final CompositeType actype = (CompositeType) type;
		final boolean[] subValueNullability = actype.getPropertyNullability();
		if ( subValueNullability != null ) {
			final String[] subPropertyNames = actype.getPropertyNames();
			final Object[] subvalues = actype.getPropertyValues( value, session );
			final Type[] subtypes = actype.getSubtypes();
			for ( int j = 0; j < subvalues.length; j++ ) {
				collectNonNullableTransientEntities(
						nullifier,
						subvalues[j],
						subPropertyNames[j],
						subtypes[j],
						subValueNullability[j],
						session,
						nonNullableTransientEntities
				);
			}
		}
	}
}