@Override public void resolveToOneAttributeSource(AttributeSourceResolutionContext context) { if (getNature() != null && owner != null) { return; } if (owner == null) { owner = (ToOneAttributeSource) context.resolveAttributeSource( associationAttribute().getReferencedEntityType(), associationAttribute().getMappedBy()); owner.addMappedByAssociationSource(this); } if (getNature() == null) { final Nature nature; if (MappedAttribute.Nature.MANY_TO_ONE.equals(associationAttribute().getNature())) { nature = Nature.MANY_TO_ONE; } else if (MappedAttribute.Nature.ONE_TO_ONE.equals(associationAttribute().getNature())) { if (owner.getContainingTableName() != null) { nature = Nature.MANY_TO_ONE; } else { nature = Nature.ONE_TO_ONE; } } else { throw new AssertionError( String.format( "Wrong attribute nature[%s] for toOne attribute: %s", associationAttribute().getNature(), associationAttribute().getRole())); } setNature(nature); } }
@Override public PluralAttributeElementSource resolvePluralAttributeElementSource( AttributeSourceResolutionContext context) { if (elementSource == null) { // elementSource has not been initialized, so we need to resolve it using the // association owner. // Get the owner attribute source that maps the opposite side of the association. ownerAttributeSource = context.resolveAttributeSource( associationAttribute.getReferencedEntityType(), associationAttribute.getMappedBy()); // Initialize resolved entitySource. elementSource = determineElementSource( ownerAttributeSource, this, entityClass, ""); // TODO not sure what relativePath should be here if (!MappedByAssociationSource.class.isInstance(elementSource)) { throw new AssertionFailure("expected a mappedBy association."); } final AssociationSource ownerAssociationSource; if (ownerAttributeSource.isSingular()) { ownerAssociationSource = (ToOneAttributeSource) ownerAttributeSource; } else { final PluralAttributeSource pluralAttributeSource = (PluralAttributeSource) ownerAttributeSource; if (!AssociationSource.class.isInstance(pluralAttributeSource.getElementSource())) { throw new AssertionFailure("Owner is not an association."); } ownerAssociationSource = (AssociationSource) pluralAttributeSource.getElementSource(); } ownerAssociationSource.addMappedByAssociationSource( (MappedByAssociationSource) elementSource); } return elementSource; }