Search in sources :

Example 61 with Property

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

the class LazyAttributesMetadata method from.

/**
	 * Build a LazyFetchGroupMetadata based on the attributes defined for the
	 * PersistentClass
	 *
	 * @param mappedEntity The entity definition
	 *
	 * @return The built LazyFetchGroupMetadata
	 */
public static LazyAttributesMetadata from(PersistentClass mappedEntity) {
    final Map<String, LazyAttributeDescriptor> lazyAttributeDescriptorMap = new LinkedHashMap<>();
    final Map<String, Set<String>> fetchGroupToAttributesMap = new HashMap<>();
    int i = -1;
    int x = 0;
    final Iterator itr = mappedEntity.getPropertyClosureIterator();
    while (itr.hasNext()) {
        i++;
        final Property property = (Property) itr.next();
        if (property.isLazy()) {
            final LazyAttributeDescriptor lazyAttributeDescriptor = LazyAttributeDescriptor.from(property, i, x++);
            lazyAttributeDescriptorMap.put(lazyAttributeDescriptor.getName(), lazyAttributeDescriptor);
            final Set<String> attributeSet = fetchGroupToAttributesMap.computeIfAbsent(lazyAttributeDescriptor.getFetchGroupName(), k -> new LinkedHashSet<>());
            attributeSet.add(lazyAttributeDescriptor.getName());
        }
    }
    if (lazyAttributeDescriptorMap.isEmpty()) {
        return new LazyAttributesMetadata(mappedEntity.getEntityName());
    }
    for (Map.Entry<String, Set<String>> entry : fetchGroupToAttributesMap.entrySet()) {
        entry.setValue(Collections.unmodifiableSet(entry.getValue()));
    }
    return new LazyAttributesMetadata(mappedEntity.getEntityName(), Collections.unmodifiableMap(lazyAttributeDescriptorMap), Collections.unmodifiableMap(fetchGroupToAttributesMap));
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Iterator(java.util.Iterator) Property(org.hibernate.mapping.Property) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 62 with Property

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

the class CopyIdentifierComponentSecondPass method createSimpleProperty.

private Property createSimpleProperty(PersistentClass referencedPersistentClass, boolean isExplicitReference, Map<String, Ejb3JoinColumn> columnByReferencedName, AtomicInteger index, Property referencedProperty) {
    Property property = new Property();
    property.setName(referencedProperty.getName());
    //FIXME set optional?
    //property.setOptional( property.isOptional() );
    property.setPersistentClass(component.getOwner());
    property.setPropertyAccessorName(referencedProperty.getPropertyAccessorName());
    SimpleValue value = new SimpleValue(buildingContext.getMetadataCollector(), component.getTable());
    property.setValue(value);
    final SimpleValue referencedValue = (SimpleValue) referencedProperty.getValue();
    value.setTypeName(referencedValue.getTypeName());
    value.setTypeParameters(referencedValue.getTypeParameters());
    final Iterator<Selectable> columns = referencedValue.getColumnIterator();
    if (joinColumns[0].isNameDeferred()) {
        joinColumns[0].copyReferencedStructureAndCreateDefaultJoinColumns(referencedPersistentClass, columns, value);
    } else {
        //FIXME take care of Formula
        while (columns.hasNext()) {
            final Selectable selectable = columns.next();
            if (!Column.class.isInstance(selectable)) {
                log.debug("Encountered formula definition; skipping");
                continue;
            }
            final Column column = (Column) selectable;
            final Ejb3JoinColumn joinColumn;
            String logicalColumnName = null;
            if (isExplicitReference) {
                final String columnName = column.getName();
                logicalColumnName = buildingContext.getMetadataCollector().getLogicalColumnName(referencedPersistentClass.getTable(), columnName);
                //JPA 2 requires referencedColumnNames to be case insensitive
                joinColumn = columnByReferencedName.get(logicalColumnName.toLowerCase(Locale.ROOT));
            } else {
                joinColumn = columnByReferencedName.get("" + index.get());
                index.getAndIncrement();
            }
            if (joinColumn == null && !joinColumns[0].isNameDeferred()) {
                throw new AnnotationException(isExplicitReference ? "Unable to find column reference in the @MapsId mapping: " + logicalColumnName : "Implicit column reference in the @MapsId mapping fails, try to use explicit referenceColumnNames: " + referencedEntityName);
            }
            final String columnName = joinColumn == null || joinColumn.isNameDeferred() ? "tata_" + column.getName() : joinColumn.getName();
            value.addColumn(new Column(columnName));
            if (joinColumn != null) {
                joinColumn.linkWithValue(value);
            }
            column.setValue(value);
        }
    }
    return property;
}
Also used : Selectable(org.hibernate.mapping.Selectable) Column(org.hibernate.mapping.Column) AnnotationException(org.hibernate.AnnotationException) Property(org.hibernate.mapping.Property) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 63 with Property

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

the class ModelBinder method createEmbeddedAttribute.

private Property createEmbeddedAttribute(MappingDocument sourceDocument, SingularAttributeSourceEmbedded embeddedSource, Component componentBinding, String containingClassName) {
    final String attributeName = embeddedSource.getName();
    bindComponent(sourceDocument, embeddedSource.getEmbeddableSource(), componentBinding, containingClassName, attributeName, embeddedSource.getXmlNodeName(), embeddedSource.isVirtualAttribute());
    prepareValueTypeViaReflection(sourceDocument, componentBinding, componentBinding.getComponentClassName(), attributeName, embeddedSource.getAttributeRole());
    componentBinding.createForeignKey();
    final Property attribute;
    if (embeddedSource.isVirtualAttribute()) {
        attribute = new SyntheticProperty() {

            @Override
            public String getPropertyAccessorName() {
                return "embedded";
            }
        };
    } else {
        attribute = new Property();
    }
    attribute.setValue(componentBinding);
    bindProperty(sourceDocument, embeddedSource, attribute);
    if (StringHelper.isNotEmpty(embeddedSource.getXmlNodeName())) {
        DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfDomEntityModeSupport();
    }
    return attribute;
}
Also used : SyntheticProperty(org.hibernate.mapping.SyntheticProperty) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Example 64 with Property

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

the class ModelBinder method bindAllCompositeAttributes.

private void bindAllCompositeAttributes(MappingDocument sourceDocument, EmbeddableSource embeddableSource, Component component) {
    for (AttributeSource attributeSource : embeddableSource.attributeSources()) {
        Property attribute = null;
        if (SingularAttributeSourceBasic.class.isInstance(attributeSource)) {
            attribute = createBasicAttribute(sourceDocument, (SingularAttributeSourceBasic) attributeSource, new SimpleValue(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
        } else if (SingularAttributeSourceEmbedded.class.isInstance(attributeSource)) {
            attribute = createEmbeddedAttribute(sourceDocument, (SingularAttributeSourceEmbedded) attributeSource, new Component(sourceDocument.getMetadataCollector(), component), component.getComponentClassName());
        } else if (SingularAttributeSourceManyToOne.class.isInstance(attributeSource)) {
            attribute = createManyToOneAttribute(sourceDocument, (SingularAttributeSourceManyToOne) attributeSource, new ManyToOne(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
        } else if (SingularAttributeSourceOneToOne.class.isInstance(attributeSource)) {
            attribute = createOneToOneAttribute(sourceDocument, (SingularAttributeSourceOneToOne) attributeSource, new OneToOne(sourceDocument.getMetadataCollector(), component.getTable(), component.getOwner()), component.getComponentClassName());
        } else if (SingularAttributeSourceAny.class.isInstance(attributeSource)) {
            attribute = createAnyAssociationAttribute(sourceDocument, (SingularAttributeSourceAny) attributeSource, new Any(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
        } else if (PluralAttributeSource.class.isInstance(attributeSource)) {
            attribute = createPluralAttribute(sourceDocument, (PluralAttributeSource) attributeSource, component.getOwner());
        } else {
            throw new AssertionFailure(String.format(Locale.ENGLISH, "Unexpected AttributeSource sub-type [%s] as part of composite [%s]", attributeSource.getClass().getName(), attributeSource.getAttributeRole().getFullPath()));
        }
        component.addProperty(attribute);
    }
}
Also used : SingularAttributeSourceBasic(org.hibernate.boot.model.source.spi.SingularAttributeSourceBasic) 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) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) AssertionFailure(org.hibernate.AssertionFailure) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) 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) PluralAttributeSource(org.hibernate.boot.model.source.spi.PluralAttributeSource) OneToOne(org.hibernate.mapping.OneToOne) SingularAttributeSourceOneToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceOneToOne) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Example 65 with Property

use of org.hibernate.mapping.Property 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)

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