Java Code Examples for org.hibernate.type.EntityType#isOneToOne()

The following examples show how to use org.hibernate.type.EntityType#isOneToOne() . 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: ResultSetProcessingContextImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void registerNonExists(EntityFetch fetch) {
	final EntityType fetchedType = fetch.getFetchedType();
	if ( ! fetchedType.isOneToOne() ) {
		return;
	}

	final EntityReferenceProcessingState fetchOwnerState = getOwnerProcessingState( fetch );
	if ( fetchOwnerState == null ) {
		throw new IllegalStateException( "Could not locate fetch owner state" );
	}

	final EntityKey ownerEntityKey = fetchOwnerState.getEntityKey();
	if ( ownerEntityKey == null ) {
		throw new IllegalStateException( "Could not locate fetch owner EntityKey" );
	}

	session.getPersistenceContext().addNullProperty(
			ownerEntityKey,
			fetchedType.getPropertyName()
	);
}
 
Example 2
Source File: EntityReferenceInitializerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void handleMissingIdentifier(ResultSetProcessingContext context) {
	if ( EntityFetch.class.isInstance( entityReference ) ) {
		final EntityFetch fetch = (EntityFetch) entityReference;
		final EntityType fetchedType = fetch.getFetchedType();
		if ( ! fetchedType.isOneToOne() ) {
			return;
		}

		final EntityReferenceProcessingState fetchOwnerState = context.getOwnerProcessingState( fetch );
		if ( fetchOwnerState == null ) {
			throw new IllegalStateException( "Could not locate fetch owner state" );
		}

		final EntityKey ownerEntityKey = fetchOwnerState.getEntityKey();
		if ( ownerEntityKey != null ) {
			context.getSession().getPersistenceContext().addNullProperty(
					ownerEntityKey,
					fetchedType.getPropertyName()
			);
		}
	}
}
 
Example 3
Source File: OuterJoinableAssociation.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean isOneToOne() {
	if ( joinableType.isEntityType() ) {
		EntityType etype = (EntityType) joinableType;
		return etype.isOneToOne() /*&& etype.isReferenceToPrimaryKey()*/;
	}
	else {
		return false;
	}
}
 
Example 4
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 5
Source File: OuterJoinableAssociation.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private boolean isOneToOne() {
	if ( joinableType.isEntityType() )  {
		EntityType etype = (EntityType) joinableType;
		return etype.isOneToOne() /*&& etype.isReferenceToPrimaryKey()*/;
	}
	else {
		return false;
	}
		
}
 
Example 6
Source File: ForeignKeys.java    From cacheonix-core with GNU Lesser General Public License v2.1 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.
 */
private Object nullifyTransientReferences(final Object value, final Type type) 
throws HibernateException {
	if ( value == null ) {
		return null;
	}
	else if ( type.isEntityType() ) {
		EntityType entityType = (EntityType) type;
		if ( entityType.isOneToOne() ) {
			return value;
		}
		else {
			String entityName = entityType.getAssociatedEntityName();
			return isNullifiable(entityName, value) ? null : value;
		}
	}
	else if ( type.isAnyType() ) {
		return isNullifiable(null, value) ? null : value;
	}
	else if ( type.isComponentType() ) {
		AbstractComponentType actype = (AbstractComponentType) type;
		Object[] subvalues = actype.getPropertyValues(value, session);
		Type[] subtypes = actype.getSubtypes();
		boolean substitute = false;
		for ( int i = 0; i < subvalues.length; i++ ) {
			Object replacement = nullifyTransientReferences( subvalues[i], subtypes[i] );
			if ( replacement != subvalues[i] ) {
				substitute = true;
				subvalues[i] = replacement;
			}
		}
		if (substitute) actype.setPropertyValues( value, subvalues, session.getEntityMode() );
		return value;
	}
	else {
		return value;
	}
}
 
Example 7
Source File: PathExpressionParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void dereferenceEntity(String propertyName, EntityType propertyType, QueryTranslatorImpl q)
		throws QueryException {
	//NOTE: we avoid joining to the next table if the named property is just the foreign key value

	//if its "id"
	boolean isIdShortcut = EntityPersister.ENTITY_ID.equals( propertyName ) &&
			propertyType.isReferenceToPrimaryKey();

	//or its the id property name
	final String idPropertyName;
	try {
		idPropertyName = propertyType.getIdentifierOrUniqueKeyPropertyName( q.getFactory() );
	}
	catch ( MappingException me ) {
		throw new QueryException( me );
	}
	boolean isNamedIdPropertyShortcut = idPropertyName != null
			&& idPropertyName.equals( propertyName )
			&& propertyType.isReferenceToPrimaryKey();

	if ( isIdShortcut || isNamedIdPropertyShortcut ) {
		// special shortcut for id properties, skip the join!
		// this must only occur at the _end_ of a path expression
		if ( componentPath.length() > 0 ) componentPath.append( '.' );
		componentPath.append( propertyName );
	}
	else {
		String entityClass = propertyType.getAssociatedEntityName();
		String name = q.createNameFor( entityClass );
		q.addType( name, entityClass );
		addJoin( name, propertyType );
		if ( propertyType.isOneToOne() ) oneToOneOwnerName = currentName;
		ownerAssociationType = propertyType;
		currentName = name;
		currentProperty = propertyName;
		q.addPathAliasAndJoin( path.substring( 0, path.toString().lastIndexOf( '.' ) ), name, joinSequence.copy() );
		componentPath.setLength( 0 );
		currentPropertyMapping = q.getEntityPersister( entityClass );
	}
}
 
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
				);
			}
		}
	}
}
 
Example 10
Source File: PathExpressionParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void dereferenceEntity(String propertyName, EntityType propertyType, QueryTranslatorImpl q)
		throws QueryException {
	//NOTE: we avoid joining to the next table if the named property is just the foreign key value

	//if its "id"
	boolean isIdShortcut = EntityPersister.ENTITY_ID.equals( propertyName )
			&& propertyType.isReferenceToPrimaryKey();

	//or its the id property name
	final String idPropertyName;
	try {
		idPropertyName = propertyType.getIdentifierOrUniqueKeyPropertyName( q.getFactory() );
	}
	catch ( MappingException me ) {
		throw new QueryException( me );
	}
	boolean isNamedIdPropertyShortcut = idPropertyName != null
			&& idPropertyName.equals( propertyName )
			&& propertyType.isReferenceToPrimaryKey();


	if ( isIdShortcut || isNamedIdPropertyShortcut ) {
		// special shortcut for id properties, skip the join!
		// this must only occur at the _end_ of a path expression
		if ( componentPath.length() > 0 ) {
			componentPath.append( '.' );
		}
		componentPath.append( propertyName );
	}
	else {
		String entityClass = propertyType.getAssociatedEntityName();
		String name = q.createNameFor( entityClass );
		q.addType( name, entityClass );
		addJoin( name, propertyType );
		if ( propertyType.isOneToOne() ) {
			oneToOneOwnerName = currentName;
		}
		ownerAssociationType = propertyType;
		currentName = name;
		currentProperty = propertyName;
		q.addPathAliasAndJoin( path.substring( 0, path.toString().lastIndexOf( '.' ) ), name, joinSequence.copy() );
		componentPath.setLength( 0 );
		currentPropertyMapping = q.getEntityPersister( entityClass );
	}
}