Search in sources :

Example 11 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class TestTools method extractModProperties.

public static Set<String> extractModProperties(PersistentClass persistentClass, String suffix) {
    final Set<String> result = new HashSet<String>();
    final Iterator iterator = persistentClass.getPropertyIterator();
    while (iterator.hasNext()) {
        final Property property = (Property) iterator.next();
        final String propertyName = property.getName();
        if (propertyName.endsWith(suffix)) {
            result.add(propertyName);
        }
    }
    return result;
}
Also used : Iterator(java.util.Iterator) Property(org.hibernate.mapping.Property) HashSet(java.util.HashSet)

Example 12 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class ModelBinder method bindAllEntityAttributes.

private void bindAllEntityAttributes(MappingDocument mappingDocument, EntitySource entitySource, PersistentClass entityDescriptor) {
    final EntityTableXref entityTableXref = mappingDocument.getMetadataCollector().getEntityTableXref(entityDescriptor.getEntityName());
    if (entityTableXref == null) {
        throw new AssertionFailure(String.format(Locale.ENGLISH, "Unable to locate EntityTableXref for entity [%s] : %s", entityDescriptor.getEntityName(), mappingDocument.getOrigin()));
    }
    // make sure we bind secondary tables first!
    for (SecondaryTableSource secondaryTableSource : entitySource.getSecondaryTableMap().values()) {
        final Join secondaryTableJoin = new Join();
        secondaryTableJoin.setPersistentClass(entityDescriptor);
        bindSecondaryTable(mappingDocument, secondaryTableSource, secondaryTableJoin, entityTableXref);
        entityDescriptor.addJoin(secondaryTableJoin);
    }
    for (AttributeSource attributeSource : entitySource.attributeSources()) {
        if (PluralAttributeSource.class.isInstance(attributeSource)) {
            // plural attribute
            final Property attribute = createPluralAttribute(mappingDocument, (PluralAttributeSource) attributeSource, entityDescriptor);
            entityDescriptor.addProperty(attribute);
        } else {
            // singular attribute
            if (SingularAttributeSourceBasic.class.isInstance(attributeSource)) {
                final SingularAttributeSourceBasic basicAttributeSource = (SingularAttributeSourceBasic) attributeSource;
                final Identifier tableName = determineTable(mappingDocument, basicAttributeSource.getName(), basicAttributeSource);
                final AttributeContainer attributeContainer;
                final Table table;
                final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
                if (secondaryTableJoin == null) {
                    table = entityDescriptor.getTable();
                    attributeContainer = entityDescriptor;
                } else {
                    table = secondaryTableJoin.getTable();
                    attributeContainer = secondaryTableJoin;
                }
                final Property attribute = createBasicAttribute(mappingDocument, basicAttributeSource, new SimpleValue(mappingDocument.getMetadataCollector(), table), entityDescriptor.getClassName());
                if (secondaryTableJoin != null) {
                    attribute.setOptional(secondaryTableJoin.isOptional());
                }
                attributeContainer.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, basicAttributeSource.getNaturalIdMutability());
            } else if (SingularAttributeSourceEmbedded.class.isInstance(attributeSource)) {
                final SingularAttributeSourceEmbedded embeddedAttributeSource = (SingularAttributeSourceEmbedded) attributeSource;
                final Identifier tableName = determineTable(mappingDocument, embeddedAttributeSource);
                final AttributeContainer attributeContainer;
                final Table table;
                final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
                if (secondaryTableJoin == null) {
                    table = entityDescriptor.getTable();
                    attributeContainer = entityDescriptor;
                } else {
                    table = secondaryTableJoin.getTable();
                    attributeContainer = secondaryTableJoin;
                }
                final Property attribute = createEmbeddedAttribute(mappingDocument, (SingularAttributeSourceEmbedded) attributeSource, new Component(mappingDocument.getMetadataCollector(), table, entityDescriptor), entityDescriptor.getClassName());
                if (secondaryTableJoin != null) {
                    attribute.setOptional(secondaryTableJoin.isOptional());
                }
                attributeContainer.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, embeddedAttributeSource.getNaturalIdMutability());
            } else if (SingularAttributeSourceManyToOne.class.isInstance(attributeSource)) {
                final SingularAttributeSourceManyToOne manyToOneAttributeSource = (SingularAttributeSourceManyToOne) attributeSource;
                final Identifier tableName = determineTable(mappingDocument, manyToOneAttributeSource.getName(), manyToOneAttributeSource);
                final AttributeContainer attributeContainer;
                final Table table;
                final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
                if (secondaryTableJoin == null) {
                    table = entityDescriptor.getTable();
                    attributeContainer = entityDescriptor;
                } else {
                    table = secondaryTableJoin.getTable();
                    attributeContainer = secondaryTableJoin;
                }
                final Property attribute = createManyToOneAttribute(mappingDocument, manyToOneAttributeSource, new ManyToOne(mappingDocument.getMetadataCollector(), table), entityDescriptor.getClassName());
                if (secondaryTableJoin != null) {
                    attribute.setOptional(secondaryTableJoin.isOptional());
                }
                attributeContainer.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, manyToOneAttributeSource.getNaturalIdMutability());
            } else if (SingularAttributeSourceOneToOne.class.isInstance(attributeSource)) {
                final SingularAttributeSourceOneToOne oneToOneAttributeSource = (SingularAttributeSourceOneToOne) attributeSource;
                final Table table = entityDescriptor.getTable();
                final Property attribute = createOneToOneAttribute(mappingDocument, oneToOneAttributeSource, new OneToOne(mappingDocument.getMetadataCollector(), table, entityDescriptor), entityDescriptor.getClassName());
                entityDescriptor.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, oneToOneAttributeSource.getNaturalIdMutability());
            } else if (SingularAttributeSourceAny.class.isInstance(attributeSource)) {
                final SingularAttributeSourceAny anyAttributeSource = (SingularAttributeSourceAny) attributeSource;
                final Identifier tableName = determineTable(mappingDocument, anyAttributeSource.getName(), anyAttributeSource.getKeySource().getRelationalValueSources());
                final AttributeContainer attributeContainer;
                final Table table;
                final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
                if (secondaryTableJoin == null) {
                    table = entityDescriptor.getTable();
                    attributeContainer = entityDescriptor;
                } else {
                    table = secondaryTableJoin.getTable();
                    attributeContainer = secondaryTableJoin;
                }
                final Property attribute = createAnyAssociationAttribute(mappingDocument, anyAttributeSource, new Any(mappingDocument.getMetadataCollector(), table), entityDescriptor.getEntityName());
                if (secondaryTableJoin != null) {
                    attribute.setOptional(secondaryTableJoin.isOptional());
                }
                attributeContainer.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, anyAttributeSource.getNaturalIdMutability());
            }
        }
    }
}
Also used : SingularAttributeSourceBasic(org.hibernate.boot.model.source.spi.SingularAttributeSourceBasic) AssertionFailure(org.hibernate.AssertionFailure) VersionAttributeSource(org.hibernate.boot.model.source.spi.VersionAttributeSource) AttributeSource(org.hibernate.boot.model.source.spi.AttributeSource) PluralAttributeSource(org.hibernate.boot.model.source.spi.PluralAttributeSource) SingularAttributeSource(org.hibernate.boot.model.source.spi.SingularAttributeSource) Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) Join(org.hibernate.mapping.Join) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) AttributeContainer(org.hibernate.mapping.AttributeContainer) SecondaryTableSource(org.hibernate.boot.model.source.spi.SecondaryTableSource) SingularAttributeSourceOneToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceOneToOne) PluralAttributeElementSourceManyToAny(org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny) Any(org.hibernate.mapping.Any) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) SingularAttributeSourceEmbedded(org.hibernate.boot.model.source.spi.SingularAttributeSourceEmbedded) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) OneToOne(org.hibernate.mapping.OneToOne) SingularAttributeSourceOneToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceOneToOne) Identifier(org.hibernate.boot.model.naming.Identifier) EntityTableXref(org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Example 13 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class ModelBinder method bindNonAggregatedCompositeEntityIdentifier.

private void bindNonAggregatedCompositeEntityIdentifier(MappingDocument mappingDocument, EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
    final IdentifierSourceNonAggregatedComposite identifierSource = (IdentifierSourceNonAggregatedComposite) hierarchySource.getIdentifierSource();
    final Component cid = new Component(mappingDocument.getMetadataCollector(), rootEntityDescriptor);
    cid.setKey(true);
    rootEntityDescriptor.setIdentifier(cid);
    final String idClassName = extractIdClassName(identifierSource);
    bindComponent(mappingDocument, hierarchySource.getRoot().getAttributeRoleBase().append("<id>").getFullPath(), identifierSource.getEmbeddableSource(), cid, idClassName, rootEntityDescriptor.getClassName(), null, idClassName == null, false, null);
    if (idClassName != null) {
        // we also need to bind the "id mapper".  ugh, terrible name.  Basically we need to
        // create a virtual (embedded) composite for the non-aggregated attributes on the entity
        // itself.
        final Component mapper = new Component(mappingDocument.getMetadataCollector(), rootEntityDescriptor);
        bindComponent(mappingDocument, hierarchySource.getRoot().getAttributeRoleBase().append(ID_MAPPER_PATH_PART).getFullPath(), identifierSource.getEmbeddableSource(), mapper, rootEntityDescriptor.getClassName(), null, null, true, false, null);
        rootEntityDescriptor.setIdentifierMapper(mapper);
        Property property = new Property();
        property.setName(PropertyPath.IDENTIFIER_MAPPER_PROPERTY);
        property.setUpdateable(false);
        property.setInsertable(false);
        property.setValue(mapper);
        property.setPropertyAccessorName("embedded");
        rootEntityDescriptor.addProperty(property);
    }
    finishBindingCompositeIdentifier(mappingDocument, rootEntityDescriptor, identifierSource, cid, null);
}
Also used : IdentifierSourceNonAggregatedComposite(org.hibernate.boot.model.source.spi.IdentifierSourceNonAggregatedComposite) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Example 14 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class ModelBinder method finishBindingCompositeIdentifier.

private void finishBindingCompositeIdentifier(MappingDocument sourceDocument, RootClass rootEntityDescriptor, CompositeIdentifierSource identifierSource, Component cid, String propertyName) {
    if (propertyName == null) {
        rootEntityDescriptor.setEmbeddedIdentifier(cid.isEmbedded());
        if (cid.isEmbedded()) {
            // todo : what is the implication of this?
            cid.setDynamic(!rootEntityDescriptor.hasPojoRepresentation());
        /*
				 * Property prop = new Property(); prop.setName("id");
				 * prop.setPropertyAccessorName("embedded"); prop.setValue(id);
				 * entity.setIdentifierProperty(prop);
				 */
        }
    } else {
        Property prop = new Property();
        prop.setValue(cid);
        bindProperty(sourceDocument, ((IdentifierSourceAggregatedComposite) identifierSource).getIdentifierAttributeSource(), prop);
        rootEntityDescriptor.setIdentifierProperty(prop);
        rootEntityDescriptor.setDeclaredIdentifierProperty(prop);
    }
    makeIdentifier(sourceDocument, identifierSource.getIdentifierGeneratorDescriptor(), null, cid);
}
Also used : Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Example 15 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class ModelBinder method bindSimpleEntityIdentifier.

private void bindSimpleEntityIdentifier(MappingDocument sourceDocument, final EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
    final IdentifierSourceSimple idSource = (IdentifierSourceSimple) hierarchySource.getIdentifierSource();
    final SimpleValue idValue = new SimpleValue(sourceDocument.getMetadataCollector(), rootEntityDescriptor.getTable());
    rootEntityDescriptor.setIdentifier(idValue);
    bindSimpleValueType(sourceDocument, idSource.getIdentifierAttributeSource().getTypeInformation(), idValue);
    final String propertyName = idSource.getIdentifierAttributeSource().getName();
    if (propertyName == null || !rootEntityDescriptor.hasPojoRepresentation()) {
        if (!idValue.isTypeSpecified()) {
            throw new MappingException("must specify an identifier type: " + rootEntityDescriptor.getEntityName(), sourceDocument.getOrigin());
        }
    } else {
        idValue.setTypeUsingReflection(rootEntityDescriptor.getClassName(), propertyName);
    }
    relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, ((RelationalValueSourceContainer) idSource.getIdentifierAttributeSource()).getRelationalValueSources(), idValue, false, new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
            context.getBuildingOptions().getImplicitNamingStrategy().determineIdentifierColumnName(new ImplicitIdentifierColumnNameSource() {

                @Override
                public EntityNaming getEntityNaming() {
                    return hierarchySource.getRoot().getEntityNamingSource();
                }

                @Override
                public AttributePath getIdentifierAttributePath() {
                    return idSource.getIdentifierAttributeSource().getAttributePath();
                }

                @Override
                public MetadataBuildingContext getBuildingContext() {
                    return context;
                }
            });
            return database.toIdentifier(propertyName);
        }
    });
    if (propertyName != null) {
        Property prop = new Property();
        prop.setValue(idValue);
        bindProperty(sourceDocument, idSource.getIdentifierAttributeSource(), prop);
        rootEntityDescriptor.setIdentifierProperty(prop);
        rootEntityDescriptor.setDeclaredIdentifierProperty(prop);
    }
    makeIdentifier(sourceDocument, idSource.getIdentifierGeneratorDescriptor(), idSource.getUnsavedValue(), idValue);
}
Also used : ImplicitIdentifierColumnNameSource(org.hibernate.boot.model.naming.ImplicitIdentifierColumnNameSource) Identifier(org.hibernate.boot.model.naming.Identifier) IdentifierSourceSimple(org.hibernate.boot.model.source.spi.IdentifierSourceSimple) 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)

Aggregations

Property (org.hibernate.mapping.Property)94 PersistentClass (org.hibernate.mapping.PersistentClass)53 Component (org.hibernate.mapping.Component)30 Test (org.junit.Test)29 SimpleValue (org.hibernate.mapping.SimpleValue)24 Iterator (java.util.Iterator)23 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)18 MetadataSources (org.hibernate.boot.MetadataSources)17 Column (org.hibernate.mapping.Column)17 AnnotationException (org.hibernate.AnnotationException)14 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)14 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)14 XProperty (org.hibernate.annotations.common.reflection.XProperty)12 Metadata (org.hibernate.boot.Metadata)11 Collection (org.hibernate.mapping.Collection)11 HashMap (java.util.HashMap)10 AssertionFailure (org.hibernate.AssertionFailure)10 MappingException (org.hibernate.MappingException)9 TestForIssue (org.hibernate.testing.TestForIssue)9 Map (java.util.Map)7