Search in sources :

Example 1 with RelationalValueSource

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

the class EntityHierarchySourceImpl method interpretDiscriminatorSource.

private static DiscriminatorSource interpretDiscriminatorSource(final RootEntitySourceImpl rootEntitySource) {
    final JaxbHbmEntityDiscriminatorType jaxbDiscriminatorMapping = rootEntitySource.jaxbEntityMapping().getDiscriminator();
    if (jaxbDiscriminatorMapping == null) {
        return null;
    }
    final RelationalValueSource relationalValueSource = RelationalValueSourceHelper.buildValueSource(rootEntitySource.sourceMappingDocument(), null, new RelationalValueSourceHelper.AbstractColumnsAndFormulasSource() {

        @Override
        public XmlElementMetadata getSourceType() {
            return XmlElementMetadata.DISCRIMINATOR;
        }

        @Override
        public String getSourceName() {
            return null;
        }

        @Override
        public SizeSource getSizeSource() {
            return Helper.interpretSizeSource(jaxbDiscriminatorMapping.getLength(), (Integer) null, null);
        }

        @Override
        public String getFormulaAttribute() {
            return jaxbDiscriminatorMapping.getFormulaAttribute();
        }

        @Override
        public String getColumnAttribute() {
            return jaxbDiscriminatorMapping.getColumnAttribute();
        }

        private List columnOrFormulas;

        @Override
        public List getColumnOrFormulaElements() {
            if (columnOrFormulas == null) {
                if (jaxbDiscriminatorMapping.getColumn() != null) {
                    if (jaxbDiscriminatorMapping.getFormula() != null) {
                        throw new MappingException(String.format(Locale.ENGLISH, "discriminator mapping [%s] named both <column/> and <formula/>, but only one or other allowed", rootEntitySource.getEntityNamingSource().getEntityName()), rootEntitySource.sourceMappingDocument().getOrigin());
                    } else {
                        columnOrFormulas = Collections.singletonList(jaxbDiscriminatorMapping.getColumn());
                    }
                } else {
                    if (jaxbDiscriminatorMapping.getFormula() != null) {
                        columnOrFormulas = Collections.singletonList(jaxbDiscriminatorMapping.getFormula());
                    } else {
                        columnOrFormulas = Collections.emptyList();
                    }
                }
            }
            return columnOrFormulas;
        }

        @Override
        public Boolean isNullable() {
            return !jaxbDiscriminatorMapping.isNotNull();
        }
    });
    return new DiscriminatorSource() {

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

        @Override
        public MetadataBuildingContext getBuildingContext() {
            return rootEntitySource.metadataBuildingContext();
        }

        @Override
        public RelationalValueSource getDiscriminatorRelationalValueSource() {
            return relationalValueSource;
        }

        @Override
        public String getExplicitHibernateTypeName() {
            return jaxbDiscriminatorMapping.getType();
        }

        @Override
        public boolean isForced() {
            return jaxbDiscriminatorMapping.isForce();
        }

        @Override
        public boolean isInserted() {
            return jaxbDiscriminatorMapping.isInsert();
        }
    };
}
Also used : SizeSource(org.hibernate.boot.model.source.spi.SizeSource) JaxbHbmEntityDiscriminatorType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmEntityDiscriminatorType) RelationalValueSource(org.hibernate.boot.model.source.spi.RelationalValueSource) DiscriminatorSource(org.hibernate.boot.model.source.spi.DiscriminatorSource) MappingException(org.hibernate.boot.MappingException) List(java.util.List)

Example 2 with RelationalValueSource

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

the class RelationalObjectBinder method bindColumnsAndFormulas.

public void bindColumnsAndFormulas(MappingDocument sourceDocument, List<RelationalValueSource> relationalValueSources, SimpleValue simpleValue, boolean areColumnsNullableByDefault, ColumnNamingDelegate columnNamingDelegate) {
    for (RelationalValueSource relationalValueSource : relationalValueSources) {
        if (ColumnSource.class.isInstance(relationalValueSource)) {
            final ColumnSource columnSource = (ColumnSource) relationalValueSource;
            bindColumn(sourceDocument, columnSource, simpleValue, areColumnsNullableByDefault, columnNamingDelegate);
        } else {
            final DerivedValueSource formulaSource = (DerivedValueSource) relationalValueSource;
            simpleValue.addFormula(new Formula(formulaSource.getExpression()));
        }
    }
}
Also used : Formula(org.hibernate.mapping.Formula) RelationalValueSource(org.hibernate.boot.model.source.spi.RelationalValueSource) DerivedValueSource(org.hibernate.boot.model.source.spi.DerivedValueSource) ColumnSource(org.hibernate.boot.model.source.spi.ColumnSource)

Example 3 with RelationalValueSource

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

the class ModelBinder method resolveLob.

private void resolveLob(final SingularAttributeSourceBasic attributeSource, SimpleValue value) {
    // Essentially this expects the type to map to a CLOB/NCLOB/BLOB sql type internally and compares.
    if (!value.isLob() && value.getTypeName() != null) {
        final TypeResolver typeResolver = attributeSource.getBuildingContext().getMetadataCollector().getTypeResolver();
        final BasicType basicType = typeResolver.basic(value.getTypeName());
        if (basicType instanceof AbstractSingleColumnStandardBasicType) {
            if (isLob(((AbstractSingleColumnStandardBasicType) basicType).getSqlTypeDescriptor().getSqlType(), null)) {
                value.makeLob();
            }
        }
    }
    // if this maps to CLOB/NCLOB/BLOB then the value will be marked as lob.
    if (!value.isLob()) {
        for (RelationalValueSource relationalValueSource : attributeSource.getRelationalValueSources()) {
            if (ColumnSource.class.isInstance(relationalValueSource)) {
                if (isLob(null, ((ColumnSource) relationalValueSource).getSqlType())) {
                    value.makeLob();
                }
            }
        }
    }
}
Also used : BasicType(org.hibernate.type.BasicType) AbstractSingleColumnStandardBasicType(org.hibernate.type.AbstractSingleColumnStandardBasicType) TypeResolver(org.hibernate.type.TypeResolver) AbstractSingleColumnStandardBasicType(org.hibernate.type.AbstractSingleColumnStandardBasicType) RelationalValueSource(org.hibernate.boot.model.source.spi.RelationalValueSource)

Example 4 with RelationalValueSource

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

the class RelationalValueSourceHelper method buildColumnSource.

/**
 * Given a {@link ColumnsAndFormulasSource}, build a single {@link RelationalValueSource}
 * which is required to be a column.  More than one {@link RelationalValueSource} will result
 * in an exception.  A formula, rather than a column, will result in an exception.
 *
 * @param mappingDocument the mapping document
 * @param containingTableName The logical name of the table containing the relational values
 * @param columnsAndFormulasSource the adapter describing the value sources.
 *
 * @return The single ColumnSource.
 */
public static ColumnSource buildColumnSource(MappingDocument mappingDocument, String containingTableName, RelationalValueSourceHelper.ColumnsAndFormulasSource columnsAndFormulasSource) {
    final List<RelationalValueSource> sources = buildValueSources(mappingDocument, containingTableName, columnsAndFormulasSource);
    if (sources.size() > 1) {
        final String errorMessage;
        if (columnsAndFormulasSource.getSourceType().canBeNamed() && StringHelper.isNotEmpty(columnsAndFormulasSource.getSourceName())) {
            errorMessage = String.format(Locale.ENGLISH, "Expecting just a single formula/column in context of <%s name=\"%s\"/>", columnsAndFormulasSource.getSourceType().getElementName(), columnsAndFormulasSource.getSourceName());
        } else {
            errorMessage = String.format(Locale.ENGLISH, "Expecting just a single formula/column in context of <%s/>", columnsAndFormulasSource.getSourceType().getElementName());
        }
        throw new MappingException(errorMessage, mappingDocument.getOrigin());
    }
    final RelationalValueSource result = sources.get(0);
    if (!ColumnSource.class.isInstance(result)) {
        final String errorMessage;
        if (columnsAndFormulasSource.getSourceType().canBeNamed() && StringHelper.isNotEmpty(columnsAndFormulasSource.getSourceName())) {
            errorMessage = String.format(Locale.ENGLISH, "Expecting single column in context of <%s name=\"%s\"/>, but found formula [%s]", columnsAndFormulasSource.getSourceType().getElementName(), columnsAndFormulasSource.getSourceName(), ((DerivedValueSource) result).getExpression());
        } else {
            errorMessage = String.format(Locale.ENGLISH, "Expecting single column in context of <%s/>, but found formula [%s]", columnsAndFormulasSource.getSourceType().getElementName(), ((DerivedValueSource) result).getExpression());
        }
        throw new MappingException(errorMessage, mappingDocument.getOrigin());
    }
    return (ColumnSource) result;
}
Also used : RelationalValueSource(org.hibernate.boot.model.source.spi.RelationalValueSource) DerivedValueSource(org.hibernate.boot.model.source.spi.DerivedValueSource) ColumnSource(org.hibernate.boot.model.source.spi.ColumnSource) MappingException(org.hibernate.boot.MappingException)

Example 5 with RelationalValueSource

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

the class EntityHierarchySourceImpl method interpretMultiTenancySource.

private static MultiTenancySource interpretMultiTenancySource(final RootEntitySourceImpl rootEntitySource) {
    final JaxbHbmMultiTenancyType jaxbMultiTenancy = rootEntitySource.jaxbEntityMapping().getMultiTenancy();
    if (jaxbMultiTenancy == null) {
        return null;
    }
    final RelationalValueSource relationalValueSource = RelationalValueSourceHelper.buildValueSource(rootEntitySource.sourceMappingDocument(), null, new RelationalValueSourceHelper.AbstractColumnsAndFormulasSource() {

        @Override
        public XmlElementMetadata getSourceType() {
            return XmlElementMetadata.MULTI_TENANCY;
        }

        @Override
        public String getSourceName() {
            return null;
        }

        @Override
        public String getFormulaAttribute() {
            return jaxbMultiTenancy.getFormulaAttribute();
        }

        @Override
        public String getColumnAttribute() {
            return jaxbMultiTenancy.getColumnAttribute();
        }

        private List columnOrFormulas;

        @Override
        public List getColumnOrFormulaElements() {
            if (columnOrFormulas == null) {
                if (jaxbMultiTenancy.getColumn() != null) {
                    if (jaxbMultiTenancy.getFormula() != null) {
                        throw new MappingException(String.format(Locale.ENGLISH, "discriminator mapping [%s] named both <column/> and <formula/>, but only one or other allowed", rootEntitySource.getEntityNamingSource().getEntityName()), rootEntitySource.sourceMappingDocument().getOrigin());
                    } else {
                        columnOrFormulas = Collections.singletonList(jaxbMultiTenancy.getColumn());
                    }
                } else {
                    if (jaxbMultiTenancy.getFormula() != null) {
                        columnOrFormulas = Collections.singletonList(jaxbMultiTenancy.getColumn());
                    } else {
                        columnOrFormulas = Collections.emptyList();
                    }
                }
            }
            return columnOrFormulas;
        }

        @Override
        public Boolean isNullable() {
            return false;
        }
    });
    return new MultiTenancySource() {

        @Override
        public RelationalValueSource getRelationalValueSource() {
            return relationalValueSource;
        }

        @Override
        public boolean isShared() {
            return jaxbMultiTenancy.isShared();
        }

        @Override
        public boolean bindAsParameter() {
            return jaxbMultiTenancy.isBindAsParam();
        }
    };
}
Also used : JaxbHbmMultiTenancyType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmMultiTenancyType) MultiTenancySource(org.hibernate.boot.model.source.spi.MultiTenancySource) List(java.util.List) RelationalValueSource(org.hibernate.boot.model.source.spi.RelationalValueSource) MappingException(org.hibernate.boot.MappingException)

Aggregations

RelationalValueSource (org.hibernate.boot.model.source.spi.RelationalValueSource)6 MappingException (org.hibernate.boot.MappingException)4 ColumnSource (org.hibernate.boot.model.source.spi.ColumnSource)3 DerivedValueSource (org.hibernate.boot.model.source.spi.DerivedValueSource)3 List (java.util.List)2 JaxbHbmEntityDiscriminatorType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmEntityDiscriminatorType)1 JaxbHbmMultiTenancyType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmMultiTenancyType)1 DiscriminatorSource (org.hibernate.boot.model.source.spi.DiscriminatorSource)1 MultiTenancySource (org.hibernate.boot.model.source.spi.MultiTenancySource)1 SizeSource (org.hibernate.boot.model.source.spi.SizeSource)1 Formula (org.hibernate.mapping.Formula)1 AbstractSingleColumnStandardBasicType (org.hibernate.type.AbstractSingleColumnStandardBasicType)1 BasicType (org.hibernate.type.BasicType)1 TypeResolver (org.hibernate.type.TypeResolver)1