Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Hibernate OGM, Domain model persistence for NoSQL datastores
   *
   * License: GNU Lesser General Public License (LGPL), version 2.1 or later
   * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
   */
  package org.hibernate.ogm.persister.impl;
  
  import java.util.Arrays;
 
Provides utility methods for dealing with bi-directional associations.

Author(s):
Emmanuel Bernard
Gunnar Morling
 
 
 	}

Returns the meta-data for the inverse side of the association represented by the given property on the given persister in case it represents the main side of a bi-directional one-to-many or many-to-many association.

Parameters:
mainSidePersister persister of the entity hosting the property of interest
propertyIndex index of the property of interest
Returns:
the meta-data of the inverse side of the specified association or null if no such meta-data exists
 
 	public static AssociationKeyMetadata getInverseAssociationKeyMetadata(OgmEntityPersister mainSidePersisterint propertyIndex) {
 		Type propertyType = mainSidePersister.getPropertyTypes()[propertyIndex];
 		SessionFactoryImplementor factory = mainSidePersister.getFactory();
 
 		// property represents no association, so no inverse meta-data can exist
 		if ( !propertyType.isAssociationType() ) {
 			return null;
 		}
 
 		Joinable mainSideJoinable = ( (AssociationTypepropertyType ).getAssociatedJoinablefactory );
 		OgmEntityPersister inverseSidePersister = null;
 
 		// to-many association
 		if ( mainSideJoinable.isCollection() ) {
 			inverseSidePersister = (OgmEntityPersister) ( (OgmCollectionPersistermainSideJoinable ).getElementPersister();
 		}
 		// to-one
 		else {
 			inverseSidePersister = (OgmEntityPersistermainSideJoinable;
 			mainSideJoinable = mainSidePersister;
 		}
 
 		String mainSideProperty = mainSidePersister.getPropertyNames()[propertyIndex];
 
 		// property is a one-to-one association (a many-to-one cannot be on the inverse side) -> get the meta-data
 		// straight from the main-side persister
 		AssociationKeyMetadata inverseOneToOneMetadata = mainSidePersister.getInverseOneToOneAssociationKeyMetadatamainSideProperty );
 		if ( inverseOneToOneMetadata != null ) {
 			return inverseOneToOneMetadata;
 		}
 
 		// process properties of inverse side and try to find association back to main side
 		for ( String candidateProperty : inverseSidePersister.getPropertyNames() ) {
 			Type type = inverseSidePersister.getPropertyTypecandidateProperty );
 
 			// candidate is a *-to-many association
 			if ( type.isCollectionType() ) {
 				OgmCollectionPersister inverseCollectionPersister = getPersisterfactory, (CollectionTypetype );
 				if ( isCollectionMatchingmainSideJoinableinverseCollectionPersister ) ) {
 					return inverseCollectionPersister.getAssociationKeyMetadata();
 				}
 			}
 		}
 
 		return null;
 	}

Returns the given collection persister for the inverse side in case the given persister represents the main side of a bi-directional many-to-many association.

Parameters:
mainSidePersister the collection persister on the main side of a bi-directional many-to-many association
Returns:
the collection persister for the inverse side of the given persister or null in case it represents the inverse side itself or the association is uni-directional
 
 		if ( mainSidePersister.isInverse() || !mainSidePersister.isManyToMany() || !mainSidePersister.getElementType().isEntityType() ) {
 			return null;
 		}
		EntityPersister inverseSidePersister = mainSidePersister.getElementPersister();
		// process collection-typed properties of inverse side and try to find association back to main side
		for ( Type type : inverseSidePersister.getPropertyTypes() ) {
			if ( type.isCollectionType() ) {
				OgmCollectionPersister inverseCollectionPersister = getPersistermainSidePersister.getFactory(), (CollectionTypetype );
				if ( isCollectionMatchingmainSidePersisterinverseCollectionPersister ) ) {
					return inverseCollectionPersister;
				}
			}
		}
		return null;
	}

Returns the role on the main side of the given association. If the collection is on the main side itself (or if it is an uni-directional association), its own unqualified role will be returned. If the collection is on the inverse side of a one-to-many or many-to-many association, the corresponding property on the main side will be determined and its name returned.

Parameters:
collectionPersister the collection of interest
Returns:
the name of the property on the main side of the given association
	public static String getMainSidePropertyName(OgmCollectionPersister collectionPersister) {
		if ( !collectionPersister.isInverse() ) {
			return collectionPersister.getUnqualifiedRole();
		}
		Loadable mainSidePersister = (LoadablecollectionPersister.getElementPersister();
		for ( int i = 0; i < mainSidePersister.getPropertyNames().lengthi++ ) {
			String candidateProperty = mainSidePersister.getPropertyNames()[i];
			Type type = mainSidePersister.getPropertyTypecandidateProperty );
			// candidate is to-one association
			if ( type.isEntityType() ) {
				if ( Arrays.equalscollectionPersister.getKeyColumnNames(), mainSidePersister.getPropertyColumnNamesi ) ) ) {
					return candidateProperty;
				}
			}
			// candidate is to-many association
			else if ( type.isCollectionType() ) {
				OgmCollectionPersister mainSideCollectionPersister = getPersistercollectionPersister.getFactory(), (CollectionTypetype );
				if ( isCollectionMatchingmainSideCollectionPersistercollectionPersister ) ) {
					return candidateProperty;
				}
			}
		}
		throw new HibernateException"Couldn't determine main side role for collection " + collectionPersister.getRole() );
	}

Checks whether table name and key column names of the given joinable and inverse collection persister match.
	private static boolean isCollectionMatching(Joinable mainSideJoinableOgmCollectionPersister inverseSidePersister) {
		boolean isSameTable = mainSideJoinable.getTableName().equalsinverseSidePersister.getTableName() );
		if ( !isSameTable ) {
			return false;
		}
		return Arrays.equalsmainSideJoinable.getKeyColumnNames(), inverseSidePersister.getElementColumnNames() );
	}
	}
New to GrepCode? Check out our FAQ X