Search in sources :

Example 1 with DerivedValueSource

use of org.hibernate.boot.model.source.spi.DerivedValueSource 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 2 with DerivedValueSource

use of org.hibernate.boot.model.source.spi.DerivedValueSource 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 3 with DerivedValueSource

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

the class RelationalValueSourceHelper method buildColumnSources.

/**
	 * Given a {@link ColumnsAndFormulasSource}, build the corresponding list of
	 * {@link ColumnSource}.  Any 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 corresponding list.
	 */
public static List<ColumnSource> buildColumnSources(MappingDocument mappingDocument, String containingTableName, RelationalValueSourceHelper.ColumnsAndFormulasSource columnsAndFormulasSource) {
    final List<RelationalValueSource> sources = buildValueSources(mappingDocument, containingTableName, columnsAndFormulasSource);
    final List<ColumnSource> columnSources = CollectionHelper.arrayList(sources.size());
    for (RelationalValueSource source : sources) {
        if (!ColumnSource.class.isInstance(source)) {
            final String errorMessage;
            if (columnsAndFormulasSource.getSourceType().canBeNamed() && StringHelper.isNotEmpty(columnsAndFormulasSource.getSourceName())) {
                errorMessage = String.format(Locale.ENGLISH, "Expecting only columns in context of <%s name=\"%s\"/>, but found formula [%s]", columnsAndFormulasSource.getSourceType().getElementName(), columnsAndFormulasSource.getSourceName(), ((DerivedValueSource) source).getExpression());
            } else {
                errorMessage = String.format(Locale.ENGLISH, "Expecting only columns in context of <%s/>, but found formula [%s]", columnsAndFormulasSource.getSourceType().getElementName(), ((DerivedValueSource) source).getExpression());
            }
            throw new MappingException(errorMessage, mappingDocument.getOrigin());
        }
        columnSources.add((ColumnSource) source);
    }
    return columnSources;
}
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)

Aggregations

ColumnSource (org.hibernate.boot.model.source.spi.ColumnSource)3 DerivedValueSource (org.hibernate.boot.model.source.spi.DerivedValueSource)3 RelationalValueSource (org.hibernate.boot.model.source.spi.RelationalValueSource)3 MappingException (org.hibernate.boot.MappingException)2 Formula (org.hibernate.mapping.Formula)1