Search in sources :

Example 16 with Component

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

the class PropertyBinder method bind.

private Property bind(Property prop) {
    if (isId) {
        final RootClass rootClass = (RootClass) holder.getPersistentClass();
        //FIXME this pose a problem as the PK is the class instead of the associated class which is not really compliant with the spec
        if (isXToMany || entityBinder.wrapIdsInEmbeddedComponents()) {
            Component identifier = (Component) rootClass.getIdentifier();
            if (identifier == null) {
                identifier = AnnotationBinder.createComponent(holder, new PropertyPreloadedData(null, null, null), true, false, buildingContext);
                rootClass.setIdentifier(identifier);
                identifier.setNullValue("undefined");
                rootClass.setEmbeddedIdentifier(true);
                rootClass.setIdentifierMapper(identifier);
            }
            //FIXME is it good enough?
            identifier.addProperty(prop);
        } else {
            rootClass.setIdentifier((KeyValue) getValue());
            if (embedded) {
                rootClass.setEmbeddedIdentifier(true);
            } else {
                rootClass.setIdentifierProperty(prop);
                final org.hibernate.mapping.MappedSuperclass superclass = BinderHelper.getMappedSuperclassOrNull(declaringClass, inheritanceStatePerClass, buildingContext);
                if (superclass != null) {
                    superclass.setDeclaredIdentifierProperty(prop);
                } else {
                    //we know the property is on the actual entity
                    rootClass.setDeclaredIdentifierProperty(prop);
                }
            }
        }
    } else {
        holder.addProperty(prop, columns, declaringClass);
    }
    return prop;
}
Also used : RootClass(org.hibernate.mapping.RootClass) PropertyPreloadedData(org.hibernate.cfg.PropertyPreloadedData) Component(org.hibernate.mapping.Component)

Example 17 with Component

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

the class CopyIdentifierComponentSecondPass method doSecondPass.

@SuppressWarnings({ "unchecked" })
public void doSecondPass(Map persistentClasses) throws MappingException {
    PersistentClass referencedPersistentClass = (PersistentClass) persistentClasses.get(referencedEntityName);
    // TODO better error names
    if (referencedPersistentClass == null) {
        throw new AnnotationException("Unknown entity name: " + referencedEntityName);
    }
    if (!(referencedPersistentClass.getIdentifier() instanceof Component)) {
        throw new AssertionFailure("Unexpected identifier type on the referenced entity when mapping a @MapsId: " + referencedEntityName);
    }
    Component referencedComponent = (Component) referencedPersistentClass.getIdentifier();
    Iterator<Property> properties = referencedComponent.getPropertyIterator();
    //prepare column name structure
    boolean isExplicitReference = true;
    Map<String, Ejb3JoinColumn> columnByReferencedName = new HashMap<String, Ejb3JoinColumn>(joinColumns.length);
    for (Ejb3JoinColumn joinColumn : joinColumns) {
        final String referencedColumnName = joinColumn.getReferencedColumn();
        if (referencedColumnName == null || BinderHelper.isEmptyAnnotationValue(referencedColumnName)) {
            break;
        }
        //JPA 2 requires referencedColumnNames to be case insensitive
        columnByReferencedName.put(referencedColumnName.toLowerCase(Locale.ROOT), joinColumn);
    }
    //try default column orientation
    AtomicInteger index = new AtomicInteger(0);
    if (columnByReferencedName.isEmpty()) {
        isExplicitReference = false;
        for (Ejb3JoinColumn joinColumn : joinColumns) {
            columnByReferencedName.put("" + index.get(), joinColumn);
            index.getAndIncrement();
        }
        index.set(0);
    }
    while (properties.hasNext()) {
        Property referencedProperty = properties.next();
        if (referencedProperty.isComposite()) {
            Property property = createComponentProperty(referencedPersistentClass, isExplicitReference, columnByReferencedName, index, referencedProperty);
            component.addProperty(property);
        } else {
            Property property = createSimpleProperty(referencedPersistentClass, isExplicitReference, columnByReferencedName, index, referencedProperty);
            component.addProperty(property);
        }
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationException(org.hibernate.AnnotationException) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 18 with Component

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

the class CopyIdentifierComponentSecondPass method createComponentProperty.

private Property createComponentProperty(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());
    Component value = new Component(buildingContext.getMetadataCollector(), component.getOwner());
    property.setValue(value);
    final Component referencedValue = (Component) referencedProperty.getValue();
    value.setTypeName(referencedValue.getTypeName());
    value.setTypeParameters(referencedValue.getTypeParameters());
    value.setComponentClassName(referencedValue.getComponentClassName());
    Iterator<Property> propertyIterator = referencedValue.getPropertyIterator();
    while (propertyIterator.hasNext()) {
        Property referencedComponentProperty = propertyIterator.next();
        if (referencedComponentProperty.isComposite()) {
            Property componentProperty = createComponentProperty(referencedValue.getOwner(), isExplicitReference, columnByReferencedName, index, referencedComponentProperty);
            value.addProperty(componentProperty);
        } else {
            Property componentProperty = createSimpleProperty(referencedValue.getOwner(), isExplicitReference, columnByReferencedName, index, referencedComponentProperty);
            value.addProperty(componentProperty);
        }
    }
    return property;
}
Also used : Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property)

Example 19 with Component

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

the class ClassPropertyHolder method addProperty.

public void addProperty(Property prop, XClass declaringClass) {
    if (prop.getValue() instanceof Component) {
        //TODO handle quote and non quote table comparison
        String tableName = prop.getValue().getTable().getName();
        if (getJoinsPerRealTableName().containsKey(tableName)) {
            final Join join = getJoinsPerRealTableName().get(tableName);
            addPropertyToJoin(prop, declaringClass, join);
        } else {
            addPropertyToPersistentClass(prop, declaringClass);
        }
    } else {
        addPropertyToPersistentClass(prop, declaringClass);
    }
}
Also used : Join(org.hibernate.mapping.Join) Component(org.hibernate.mapping.Component)

Example 20 with Component

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

the class IndexOrUniqueKeySecondPass method doSecondPass.

@Override
public void doSecondPass(Map persistentClasses) throws MappingException {
    if (columns != null) {
        for (int i = 0; i < columns.length; i++) {
            addConstraintToColumn(columns[i]);
        }
    }
    if (column != null) {
        this.table = column.getTable();
        PersistentClass persistentClass = (PersistentClass) persistentClasses.get(column.getPropertyHolder().getEntityName());
        Property property = persistentClass.getProperty(column.getPropertyName());
        if (property.getValue() instanceof Component) {
            Component component = (Component) property.getValue();
            List<Column> columns = new ArrayList<>();
            component.getColumnIterator().forEachRemaining(selectable -> {
                if (selectable instanceof Column) {
                    columns.add((Column) selectable);
                }
            });
            addConstraintToColumns(columns);
        } else {
            addConstraintToColumn(buildingContext.getMetadataCollector().getLogicalColumnName(table, column.getMappingColumn().getQuotedName()));
        }
    }
}
Also used : Column(org.hibernate.mapping.Column) ArrayList(java.util.ArrayList) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

Component (org.hibernate.mapping.Component)42 Property (org.hibernate.mapping.Property)29 PersistentClass (org.hibernate.mapping.PersistentClass)17 Iterator (java.util.Iterator)11 AnnotationException (org.hibernate.AnnotationException)9 SimpleValue (org.hibernate.mapping.SimpleValue)9 XProperty (org.hibernate.annotations.common.reflection.XProperty)8 ManyToOne (org.hibernate.mapping.ManyToOne)8 Value (org.hibernate.mapping.Value)8 HashMap (java.util.HashMap)6 Collection (org.hibernate.mapping.Collection)6 ArrayList (java.util.ArrayList)5 AssertionFailure (org.hibernate.AssertionFailure)5 MappingException (org.hibernate.MappingException)5 Any (org.hibernate.mapping.Any)5 Column (org.hibernate.mapping.Column)5 Join (org.hibernate.mapping.Join)5 XClass (org.hibernate.annotations.common.reflection.XClass)4 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)4 Ejb3Column (org.hibernate.cfg.Ejb3Column)3