Created
April 28, 2011 11:15
-
-
Save gbadner/946170 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MetadataImpl constructor: | |
public MetadataImpl(MetadataSources metadataSources, ProcessingOrder preferredProcessingOrder) { | |
this.serviceRegistry = metadataSources.getServiceRegistry(); | |
this.namingStrategy = metadataSources.getNamingStrategy(); | |
final ArrayList<String> processedEntityNames = new ArrayList<String>(); | |
if ( preferredProcessingOrder == ProcessingOrder.HBM_FIRST ) { | |
applyHibernateMappings( metadataSources, processedEntityNames ); | |
applyAnnotationMappings( metadataSources, processedEntityNames ); | |
} | |
else { | |
applyAnnotationMappings( metadataSources, processedEntityNames ); | |
applyHibernateMappings( metadataSources, processedEntityNames ); | |
} | |
new EntityReferenceResolver( this ).resolve(); | |
} | |
---------------------------------------------------- | |
class EntityReferenceResolver { | |
private final MetadataImplementor metadata; | |
EntityReferenceResolver(MetadataImplementor metadata) { | |
this.metadata = metadata; | |
} | |
void resolve() { | |
for ( EntityBinding entityBinding : metadata.getEntityBindings() ) { | |
for ( EntityReferencingAttributeBinding attributeBinding : entityBinding.getEntityReferencingAttributeBindings() ) { | |
resolve( attributeBinding ); | |
} | |
} | |
} | |
private void resolve(EntityReferencingAttributeBinding attributeBinding) { | |
if ( attributeBinding.getReferencedEntityName() == null ) { | |
throw new IllegalArgumentException( "attributeBinding has null entityName: " + attributeBinding.getAttribute().getName() ); | |
} | |
EntityBinding entityBinding = metadata.getEntityBinding( attributeBinding.getReferencedEntityName() ); | |
if ( entityBinding == null ) { | |
throw new org.hibernate.MappingException( | |
"Attribute [" + attributeBinding.getAttribute().getName() + | |
"] refers to unknown entity: [" + attributeBinding.getReferencedEntityName() + "]" ); | |
} | |
AttributeBinding referencedAttributeBinding = | |
attributeBinding.getReferencedAttributeName() == null ? | |
entityBinding.getEntityIdentifier().getValueBinding() : | |
entityBinding.getAttributeBinding( attributeBinding.getReferencedAttributeName() ); | |
if ( referencedAttributeBinding == null ) { | |
// TODO: does attribute name include path w/ entity name? | |
throw new MappingException( | |
"Attribute [" + attributeBinding.getAttribute().getName() + | |
"] refers to unknown attribute: [" + attributeBinding.getReferencedEntityName() + "]" | |
); | |
} | |
attributeBinding.resolveReference( referencedAttributeBinding ); | |
referencedAttributeBinding.addEntityReferencingAttributeBinding( attributeBinding ); | |
} | |
} | |
-------------------------------------------------------------------------------- | |
ManyToOneAttributeBinding implements EntityReferencingAttributeBinding : | |
public final void resolveReference(AttributeBinding referencedAttributeBinding) { | |
if ( ! referencedEntityName.equals( referencedAttributeBinding.getEntityBinding().getEntity().getName() ) ) { | |
throw new IllegalStateException( | |
"attempt to set EntityBinding with name: [" + | |
referencedAttributeBinding.getEntityBinding().getEntity().getName() + | |
"; entity name should be: " + referencedEntityName | |
); | |
} | |
if ( referencedAttributeName == null ) { | |
referencedAttributeName = referencedAttributeBinding.getAttribute().getName(); | |
} | |
else if ( ! referencedAttributeName.equals( referencedAttributeBinding.getAttribute().getName() ) ) { | |
throw new IllegalStateException( | |
"Inconsistent attribute name; expected: " + referencedAttributeName + | |
"actual: " + referencedAttributeBinding.getAttribute().getName() | |
); | |
} | |
this.referencedAttributeBinding = referencedAttributeBinding; | |
buildForeignKey(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment