Search in sources :

Example 1 with DependantBasicValue

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

the class MapBinder method createFormulatedValue.

protected Value createFormulatedValue(Value value, Collection collection, String targetPropertyName, PersistentClass associatedClass, PersistentClass targetPropertyPersistentClass, MetadataBuildingContext buildingContext) {
    final Table mapKeyTable;
    // HHH-11005 - only if we are OneToMany and location of map key property is at a different level, need to add a select
    if (!associatedClass.equals(targetPropertyPersistentClass)) {
        mapKeyTable = targetPropertyPersistentClass.getTable();
    } else {
        mapKeyTable = associatedClass.getTable();
    }
    if (value instanceof Component) {
        Component component = (Component) value;
        Component indexComponent = new Component(getBuildingContext(), collection);
        indexComponent.setComponentClassName(component.getComponentClassName());
        for (Property current : component.getProperties()) {
            Property newProperty = new Property();
            newProperty.setCascade(current.getCascade());
            newProperty.setValueGenerationStrategy(current.getValueGenerationStrategy());
            newProperty.setInsertable(false);
            newProperty.setUpdateable(false);
            newProperty.setMetaAttributes(current.getMetaAttributes());
            newProperty.setName(current.getName());
            newProperty.setNaturalIdentifier(false);
            // newProperty.setOptimisticLocked( false );
            newProperty.setOptional(false);
            newProperty.setPersistentClass(current.getPersistentClass());
            newProperty.setPropertyAccessorName(current.getPropertyAccessorName());
            newProperty.setSelectable(current.isSelectable());
            newProperty.setValue(createFormulatedValue(current.getValue(), collection, targetPropertyName, associatedClass, associatedClass, buildingContext));
            indexComponent.addProperty(newProperty);
        }
        return indexComponent;
    } else if (value instanceof BasicValue) {
        final BasicValue sourceValue = (BasicValue) value;
        final DependantBasicValue dependantBasicValue = new DependantBasicValue(getBuildingContext(), mapKeyTable, sourceValue, false, false);
        final Selectable sourceValueColumn = sourceValue.getColumn();
        if (sourceValueColumn instanceof Column) {
            dependantBasicValue.addColumn(((Column) sourceValueColumn).clone());
        } else if (sourceValueColumn instanceof Formula) {
            dependantBasicValue.addFormula(new Formula(((Formula) sourceValueColumn).getFormula()));
        } else {
            throw new AssertionFailure("Unknown element column type : " + sourceValueColumn.getClass());
        }
        return dependantBasicValue;
    } else if (value instanceof SimpleValue) {
        SimpleValue sourceValue = (SimpleValue) value;
        SimpleValue targetValue;
        if (value instanceof ManyToOne) {
            ManyToOne sourceManyToOne = (ManyToOne) sourceValue;
            ManyToOne targetManyToOne = new ManyToOne(getBuildingContext(), mapKeyTable);
            targetManyToOne.setFetchMode(FetchMode.DEFAULT);
            targetManyToOne.setLazy(true);
            // targetValue.setIgnoreNotFound( ); does not make sense for a map key
            targetManyToOne.setReferencedEntityName(sourceManyToOne.getReferencedEntityName());
            targetValue = targetManyToOne;
        } else {
            targetValue = new BasicValue(getBuildingContext(), mapKeyTable);
            targetValue.copyTypeFrom(sourceValue);
        }
        for (Selectable current : sourceValue.getSelectables()) {
            if (current instanceof Column) {
                targetValue.addColumn(((Column) current).clone());
            } else if (current instanceof Formula) {
                targetValue.addFormula(new Formula(((Formula) current).getFormula()));
            } else {
                throw new AssertionFailure("Unknown element in column iterator: " + current.getClass());
            }
        }
        return targetValue;
    } else {
        throw new AssertionFailure("Unknown type encountered for map key: " + value.getClass());
    }
}
Also used : DependantBasicValue(org.hibernate.mapping.DependantBasicValue) Formula(org.hibernate.mapping.Formula) Table(org.hibernate.mapping.Table) AssertionFailure(org.hibernate.AssertionFailure) Selectable(org.hibernate.mapping.Selectable) MapKeyColumn(jakarta.persistence.MapKeyColumn) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) Column(org.hibernate.mapping.Column) AnnotatedColumn(org.hibernate.cfg.AnnotatedColumn) AnnotatedJoinColumn(org.hibernate.cfg.AnnotatedJoinColumn) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty) ManyToOne(org.hibernate.mapping.ManyToOne) BasicValue(org.hibernate.mapping.BasicValue) DependantBasicValue(org.hibernate.mapping.DependantBasicValue) SimpleValue(org.hibernate.mapping.SimpleValue)

Aggregations

MapKeyColumn (jakarta.persistence.MapKeyColumn)1 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)1 AssertionFailure (org.hibernate.AssertionFailure)1 XProperty (org.hibernate.annotations.common.reflection.XProperty)1 AnnotatedColumn (org.hibernate.cfg.AnnotatedColumn)1 AnnotatedJoinColumn (org.hibernate.cfg.AnnotatedJoinColumn)1 BasicValue (org.hibernate.mapping.BasicValue)1 Column (org.hibernate.mapping.Column)1 Component (org.hibernate.mapping.Component)1 DependantBasicValue (org.hibernate.mapping.DependantBasicValue)1 Formula (org.hibernate.mapping.Formula)1 ManyToOne (org.hibernate.mapping.ManyToOne)1 Property (org.hibernate.mapping.Property)1 Selectable (org.hibernate.mapping.Selectable)1 SimpleValue (org.hibernate.mapping.SimpleValue)1 Table (org.hibernate.mapping.Table)1