Search in sources :

Example 6 with LocalMetadataBuildingContext

use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.

the class ModelBinder method bindMapKey.

private void bindMapKey(final MappingDocument mappingDocument, final IndexedPluralAttributeSource pluralAttributeSource, final org.hibernate.mapping.Map collectionBinding) {
    if (pluralAttributeSource.getIndexSource() instanceof PluralAttributeMapKeySourceBasic) {
        final PluralAttributeMapKeySourceBasic mapKeySource = (PluralAttributeMapKeySourceBasic) pluralAttributeSource.getIndexSource();
        final SimpleValue value = new SimpleValue(mappingDocument.getMetadataCollector(), collectionBinding.getCollectionTable());
        bindSimpleValueType(mappingDocument, mapKeySource.getTypeInformation(), value);
        if (!value.isTypeSpecified()) {
            throw new MappingException("map index element must specify a type: " + pluralAttributeSource.getAttributeRole().getFullPath(), mappingDocument.getOrigin());
        }
        relationalObjectBinder.bindColumnsAndFormulas(mappingDocument, mapKeySource.getRelationalValueSources(), value, true, new RelationalObjectBinder.ColumnNamingDelegate() {

            @Override
            public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
                return database.toIdentifier(IndexedCollection.DEFAULT_INDEX_COLUMN_NAME);
            }
        });
        collectionBinding.setIndex(value);
    } else if (pluralAttributeSource.getIndexSource() instanceof PluralAttributeMapKeySourceEmbedded) {
        final PluralAttributeMapKeySourceEmbedded mapKeySource = (PluralAttributeMapKeySourceEmbedded) pluralAttributeSource.getIndexSource();
        final Component componentBinding = new Component(mappingDocument.getMetadataCollector(), collectionBinding);
        bindComponent(mappingDocument, mapKeySource.getEmbeddableSource(), componentBinding, null, pluralAttributeSource.getName(), mapKeySource.getXmlNodeName(), false);
        collectionBinding.setIndex(componentBinding);
    } else if (pluralAttributeSource.getIndexSource() instanceof PluralAttributeMapKeyManyToManySource) {
        final PluralAttributeMapKeyManyToManySource mapKeySource = (PluralAttributeMapKeyManyToManySource) pluralAttributeSource.getIndexSource();
        final ManyToOne mapKeyBinding = new ManyToOne(mappingDocument.getMetadataCollector(), collectionBinding.getCollectionTable());
        mapKeyBinding.setReferencedEntityName(mapKeySource.getReferencedEntityName());
        relationalObjectBinder.bindColumnsAndFormulas(mappingDocument, mapKeySource.getRelationalValueSources(), mapKeyBinding, true, new RelationalObjectBinder.ColumnNamingDelegate() {

            @Override
            public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
                return implicitNamingStrategy.determineMapKeyColumnName(new ImplicitMapKeyColumnNameSource() {

                    @Override
                    public AttributePath getPluralAttributePath() {
                        return pluralAttributeSource.getAttributePath();
                    }

                    @Override
                    public MetadataBuildingContext getBuildingContext() {
                        return context;
                    }
                });
            }
        });
        collectionBinding.setIndex(mapKeyBinding);
    } else if (pluralAttributeSource.getIndexSource() instanceof PluralAttributeMapKeyManyToAnySource) {
        final PluralAttributeMapKeyManyToAnySource mapKeySource = (PluralAttributeMapKeyManyToAnySource) pluralAttributeSource.getIndexSource();
        final Any mapKeyBinding = new Any(mappingDocument.getMetadataCollector(), collectionBinding.getCollectionTable());
        bindAny(mappingDocument, mapKeySource, mapKeyBinding, pluralAttributeSource.getAttributeRole().append("key"), pluralAttributeSource.getAttributePath().append("key"));
        collectionBinding.setIndex(mapKeyBinding);
    }
}
Also used : PluralAttributeMapKeyManyToAnySource(org.hibernate.boot.model.source.spi.PluralAttributeMapKeyManyToAnySource) PluralAttributeElementSourceManyToAny(org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny) Any(org.hibernate.mapping.Any) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) MappingException(org.hibernate.boot.MappingException) Identifier(org.hibernate.boot.model.naming.Identifier) PluralAttributeMapKeySourceEmbedded(org.hibernate.boot.model.source.spi.PluralAttributeMapKeySourceEmbedded) PluralAttributeMapKeyManyToManySource(org.hibernate.boot.model.source.spi.PluralAttributeMapKeyManyToManySource) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) Component(org.hibernate.mapping.Component) ImplicitMapKeyColumnNameSource(org.hibernate.boot.model.naming.ImplicitMapKeyColumnNameSource) PluralAttributeMapKeySourceBasic(org.hibernate.boot.model.source.spi.PluralAttributeMapKeySourceBasic)

Example 7 with LocalMetadataBuildingContext

use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.

the class ModelBinder method bindEntityVersion.

private void bindEntityVersion(MappingDocument sourceDocument, EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
    final VersionAttributeSource versionAttributeSource = hierarchySource.getVersionAttributeSource();
    final SimpleValue versionValue = new SimpleValue(sourceDocument.getMetadataCollector(), rootEntityDescriptor.getTable());
    versionValue.makeVersion();
    bindSimpleValueType(sourceDocument, versionAttributeSource.getTypeInformation(), versionValue);
    relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, versionAttributeSource.getRelationalValueSources(), versionValue, false, new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            return implicitNamingStrategy.determineBasicColumnName(versionAttributeSource);
        }
    });
    Property prop = new Property();
    prop.setValue(versionValue);
    bindProperty(sourceDocument, versionAttributeSource, prop);
    // but just to make sure...
    if (prop.getValueGenerationStrategy() != null) {
        if (prop.getValueGenerationStrategy().getGenerationTiming() == GenerationTiming.INSERT) {
            throw new MappingException("'generated' attribute cannot be 'insert' for version/timestamp property", sourceDocument.getOrigin());
        }
    }
    if (versionAttributeSource.getUnsavedValue() != null) {
        versionValue.setNullValue(versionAttributeSource.getUnsavedValue());
    } else {
        versionValue.setNullValue("undefined");
    }
    rootEntityDescriptor.setVersion(prop);
    rootEntityDescriptor.setDeclaredVersion(prop);
    rootEntityDescriptor.addProperty(prop);
}
Also used : VersionAttributeSource(org.hibernate.boot.model.source.spi.VersionAttributeSource) Identifier(org.hibernate.boot.model.naming.Identifier) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty) SimpleValue(org.hibernate.mapping.SimpleValue) MappingException(org.hibernate.boot.MappingException)

Example 8 with LocalMetadataBuildingContext

use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.

the class ModelBinder method bindJoinedSubclassEntity.

private void bindJoinedSubclassEntity(JoinedSubclassEntitySourceImpl entitySource, JoinedSubclass entityDescriptor) {
    MappingDocument mappingDocument = entitySource.sourceMappingDocument();
    bindBasicEntityValues(mappingDocument, entitySource, entityDescriptor);
    final Table primaryTable = bindEntityTableSpecification(mappingDocument, entitySource.getPrimaryTable(), null, entitySource, entityDescriptor);
    entityDescriptor.setTable(primaryTable);
    if (log.isDebugEnabled()) {
        log.debugf("Mapping joined-subclass: %s -> %s", entityDescriptor.getEntityName(), primaryTable.getName());
    }
    // KEY
    final SimpleValue keyBinding = new DependantValue(mappingDocument.getMetadataCollector(), primaryTable, entityDescriptor.getIdentifier());
    if (mappingDocument.getBuildingOptions().useNationalizedCharacterData()) {
        keyBinding.makeNationalized();
    }
    entityDescriptor.setKey(keyBinding);
    keyBinding.setCascadeDeleteEnabled(entitySource.isCascadeDeleteEnabled());
    relationalObjectBinder.bindColumns(mappingDocument, entitySource.getPrimaryKeyColumnSources(), keyBinding, false, new RelationalObjectBinder.ColumnNamingDelegate() {

        int count = 0;

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            final Column column = primaryTable.getPrimaryKey().getColumn(count++);
            return database.toIdentifier(column.getQuotedName());
        }
    });
    keyBinding.setForeignKeyName(entitySource.getExplicitForeignKeyName());
    // model.getKey().setType( new Type( model.getIdentifier() ) );
    entityDescriptor.createPrimaryKey();
    entityDescriptor.createForeignKey();
    // todo : tooling hints
    bindAllEntityAttributes(entitySource.sourceMappingDocument(), entitySource, entityDescriptor);
    bindJoinedSubclassEntities(entitySource, entityDescriptor);
}
Also used : Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Identifier(org.hibernate.boot.model.naming.Identifier) DependantValue(org.hibernate.mapping.DependantValue) Column(org.hibernate.mapping.Column) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 9 with LocalMetadataBuildingContext

use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.

the class ModelBinder method createBasicAttribute.

private Property createBasicAttribute(MappingDocument sourceDocument, final SingularAttributeSourceBasic attributeSource, SimpleValue value, String containingClassName) {
    final String attributeName = attributeSource.getName();
    bindSimpleValueType(sourceDocument, attributeSource.getTypeInformation(), value);
    relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, attributeSource.getRelationalValueSources(), value, attributeSource.areValuesNullableByDefault(), new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            return implicitNamingStrategy.determineBasicColumnName(attributeSource);
        }
    });
    prepareValueTypeViaReflection(sourceDocument, value, containingClassName, attributeName, attributeSource.getAttributeRole());
    //		// this is done here 'cos we might only know the type here (ugly!)
    //		// TODO: improve this a lot:
    //		if ( value instanceof ToOne ) {
    //			ToOne toOne = (ToOne) value;
    //			String propertyRef = toOne.getReferencedEntityAttributeName();
    //			if ( propertyRef != null ) {
    //				mappings.addUniquePropertyReference( toOne.getReferencedEntityName(), propertyRef );
    //			}
    //			toOne.setCascadeDeleteEnabled( "cascade".equals( subnode.attributeValue( "on-delete" ) ) );
    //		}
    //		else if ( value instanceof Collection ) {
    //			Collection coll = (Collection) value;
    //			String propertyRef = coll.getReferencedEntityAttributeName();
    //			// not necessarily a *unique* property reference
    //			if ( propertyRef != null ) {
    //				mappings.addPropertyReference( coll.getOwnerEntityName(), propertyRef );
    //			}
    //		}
    value.createForeignKey();
    Property property = new Property();
    property.setValue(value);
    bindProperty(sourceDocument, attributeSource, property);
    return property;
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Aggregations

Identifier (org.hibernate.boot.model.naming.Identifier)9 LocalMetadataBuildingContext (org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext)9 SimpleValue (org.hibernate.mapping.SimpleValue)7 MappingException (org.hibernate.boot.MappingException)4 Property (org.hibernate.mapping.Property)3 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)3 Column (org.hibernate.mapping.Column)2 DenormalizedTable (org.hibernate.mapping.DenormalizedTable)2 DependantValue (org.hibernate.mapping.DependantValue)2 Table (org.hibernate.mapping.Table)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ImplicitIdentifierColumnNameSource (org.hibernate.boot.model.naming.ImplicitIdentifierColumnNameSource)1 ImplicitIndexColumnNameSource (org.hibernate.boot.model.naming.ImplicitIndexColumnNameSource)1 ImplicitMapKeyColumnNameSource (org.hibernate.boot.model.naming.ImplicitMapKeyColumnNameSource)1 Namespace (org.hibernate.boot.model.relational.Namespace)1 IdentifierSourceSimple (org.hibernate.boot.model.source.spi.IdentifierSourceSimple)1 InLineViewSource (org.hibernate.boot.model.source.spi.InLineViewSource)1 PluralAttributeElementSourceManyToAny (org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny)1 PluralAttributeElementSourceOneToMany (org.hibernate.boot.model.source.spi.PluralAttributeElementSourceOneToMany)1