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

The following examples show how to use org.hibernate.type.CompositeType#getPropertyValues() . 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: 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 3
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 4
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 5
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 6
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 7
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 8
Source File: ForeignKeys.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 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 propertyName An entity attribute name
 * @param type An entity attribute type
 *
 * @return {@code null} if the argument is an unsaved entity; otherwise return the argument.
 */
private CompletionStage<Object> nullifyTransientReferences(Object value, String propertyName, Type type) {
	final CompletionStage<Object> result;
	if ( value == null ) {
		return null;
	}
	else if ( type.isEntityType() ) {
		final EntityType entityType = (EntityType) type;
		if ( entityType.isOneToOne() ) {
			return null;
		}
		else {
			// if we're dealing with a lazy property, it may need to be
			// initialized to determine if the value is nullifiable
			CompletionStage<Object> fetcher;
			if ( isDelete
					&& value == LazyPropertyInitializer.UNFETCHED_PROPERTY
					&& !session.getPersistenceContextInternal().isNullifiableEntityKeysEmpty() ) {
				throw new UnsupportedOperationException("lazy property initialization not supported");
//				fetcher = ( (LazyPropertyInitializer) persister ).initializeLazyProperty( propertyName, self, session );
			}
			else {
				fetcher = CompletionStages.completedFuture( value );
			}
			result = fetcher.thenCompose( fetchedValue -> {
				if ( fetchedValue == null ) {
					// The uninitialized value was initialized to null
					return CompletionStages.nullFuture();
				}
				else {
					// If the value is not nullifiable, make sure that the
					// possibly initialized value is returned.
					return isNullifiable( entityType.getAssociatedEntityName(), fetchedValue )
							.thenApply( trans -> trans ? null : fetchedValue );
				}
			} );
		}
	}
	else if ( type.isAnyType() ) {
		result = isNullifiable( null, value ).thenApply( trans -> trans  ? null : value );
	}
	else if ( type.isComponentType() ) {
		final CompositeType actype = (CompositeType) type;
		final Object[] values = actype.getPropertyValues( value, session );
		CompletionStage<Void> nullifier = nullifyTransientReferences(
				propertyName,
				values,
				actype.getSubtypes(),
				actype.getPropertyNames()
		);
		if ( nullifier == null ) {
			return null;
		}
		else {
			result = nullifier.thenAccept( v -> actype.setPropertyValues( value, values, EntityMode.POJO ) )
					.thenApply( v -> value );
		}
	}
	else {
		return null;
	}

	return result.thenApply( returnedValue -> {
		trackDirt( value, propertyName, returnedValue );
		return returnedValue;
	} );
}
 
Example 9
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
				);
			}
		}
	}
}