public static AssociationKeyMetadata getInverseAssociationKeyMetadata()

in core/src/main/java/org/hibernate/ogm/persister/impl/BiDirectionalAssociationHelper.java [41:89]


	public static AssociationKeyMetadata getInverseAssociationKeyMetadata(OgmEntityPersister mainSidePersister, int 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 = ( (AssociationType) propertyType ).getAssociatedJoinable( factory );
		OgmEntityPersister inverseSidePersister = null;

		// to-many association
		if ( mainSideJoinable.isCollection() ) {
			inverseSidePersister = (OgmEntityPersister) ( (OgmCollectionPersister) mainSideJoinable ).getElementPersister();
		}
		// to-one
		else {
			inverseSidePersister = (OgmEntityPersister) mainSideJoinable;
			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.getInverseOneToOneAssociationKeyMetadata( mainSideProperty );
		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.getPropertyType( candidateProperty );

			// candidate is a *-to-many association
			if ( type.isCollectionType() ) {
				OgmCollectionPersister inverseCollectionPersister = getPersister( factory, (CollectionType) type );
				String mappedByProperty = inverseCollectionPersister.getMappedByProperty();
				if ( mainSideProperty.equals( mappedByProperty ) ) {
					if ( isCollectionMatching( mainSideJoinable, inverseCollectionPersister ) ) {
						return inverseCollectionPersister.getAssociationKeyMetadata();
					}
				}
			}
		}

		return null;
	}