Search in sources :

Example 6 with MappingException

use of org.hibernate.boot.MappingException in project hibernate-orm by hibernate.

the class FilterDefinitionBinder method processFilterDefinition.

/**
	 * Handling for a {@code <filter-def/>} declaration.
	 *
	 * @param context Access to information relative to the mapping document containing this binding
	 * @param jaxbFilterDefinitionMapping The {@code <filter-def/>} JAXB mapping
	 */
@SuppressWarnings("unchecked")
public static void processFilterDefinition(HbmLocalMetadataBuildingContext context, JaxbHbmFilterDefinitionType jaxbFilterDefinitionMapping) {
    Map<String, Type> parameterMap = null;
    String condition = jaxbFilterDefinitionMapping.getCondition();
    for (Serializable content : jaxbFilterDefinitionMapping.getContent()) {
        if (String.class.isInstance(content)) {
            final String contentString = content.toString().trim();
            if (StringHelper.isNotEmpty(contentString)) {
                if (condition != null) {
                    log.debugf("filter-def [name=%s, origin=%s] defined multiple conditions, accepting arbitrary one", jaxbFilterDefinitionMapping.getName(), context.getOrigin().toString());
                }
            }
        } else {
            final JaxbHbmFilterParameterType jaxbParameterMapping;
            if (JaxbHbmFilterParameterType.class.isInstance(content)) {
                jaxbParameterMapping = (JaxbHbmFilterParameterType) content;
            } else if (JAXBElement.class.isInstance(content)) {
                final JAXBElement<JaxbHbmFilterParameterType> jaxbElement = (JAXBElement<JaxbHbmFilterParameterType>) content;
                jaxbParameterMapping = jaxbElement.getValue();
            } else {
                throw new MappingException("Unable to decipher filter-def content type [" + content.getClass().getName() + "]", context.getOrigin());
            }
            if (parameterMap == null) {
                parameterMap = new HashMap<String, Type>();
            }
            parameterMap.put(jaxbParameterMapping.getParameterName(), context.getMetadataCollector().getTypeResolver().heuristicType(jaxbParameterMapping.getParameterValueTypeName()));
        }
    }
    context.getMetadataCollector().addFilterDefinition(new FilterDefinition(jaxbFilterDefinitionMapping.getName(), condition, parameterMap));
    log.debugf("Processed filter definition : %s", jaxbFilterDefinitionMapping.getName());
}
Also used : FilterDefinition(org.hibernate.engine.spi.FilterDefinition) JaxbHbmFilterParameterType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmFilterParameterType) JaxbHbmFilterDefinitionType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmFilterDefinitionType) Type(org.hibernate.type.Type) Serializable(java.io.Serializable) JaxbHbmFilterParameterType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmFilterParameterType) JAXBElement(javax.xml.bind.JAXBElement) MappingException(org.hibernate.boot.MappingException)

Example 7 with MappingException

use of org.hibernate.boot.MappingException in project hibernate-orm by hibernate.

the class CacheableFileXmlSource method doBind.

@Override
@SuppressWarnings("unchecked")
public Binding doBind(Binder binder) {
    if (strict) {
        try {
            return new Binding(readSerFile(), getOrigin());
        } catch (SerializationException e) {
            throw new MappingException(String.format("Unable to deserialize from cached file [%s]", getOrigin().getName()), e, getOrigin());
        } catch (FileNotFoundException e) {
            throw new MappingException(String.format("Unable to locate cached file [%s]", getOrigin().getName()), e, getOrigin());
        }
    } else {
        if (!isSerfileObsolete()) {
            try {
                return readSerFile();
            } catch (SerializationException e) {
                log.unableToDeserializeCache(serFile.getName(), e);
            } catch (FileNotFoundException e) {
                log.cachedFileNotFound(serFile.getName(), e);
            }
        } else {
            log.cachedFileObsolete(serFile);
        }
        log.readingMappingsFromFile(xmlFile.getPath());
        final Binding binding = FileXmlSource.doBind(binder, xmlFile, getOrigin());
        writeSerFile(binding);
        return binding;
    }
}
Also used : Binding(org.hibernate.boot.jaxb.spi.Binding) SerializationException(org.hibernate.type.SerializationException) FileNotFoundException(java.io.FileNotFoundException) MappingException(org.hibernate.boot.MappingException)

Example 8 with MappingException

use of org.hibernate.boot.MappingException in project hibernate-orm by hibernate.

the class ModelBinder method bindSimpleEntityIdentifier.

private void bindSimpleEntityIdentifier(MappingDocument sourceDocument, final EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
    final IdentifierSourceSimple idSource = (IdentifierSourceSimple) hierarchySource.getIdentifierSource();
    final SimpleValue idValue = new SimpleValue(sourceDocument.getMetadataCollector(), rootEntityDescriptor.getTable());
    rootEntityDescriptor.setIdentifier(idValue);
    bindSimpleValueType(sourceDocument, idSource.getIdentifierAttributeSource().getTypeInformation(), idValue);
    final String propertyName = idSource.getIdentifierAttributeSource().getName();
    if (propertyName == null || !rootEntityDescriptor.hasPojoRepresentation()) {
        if (!idValue.isTypeSpecified()) {
            throw new MappingException("must specify an identifier type: " + rootEntityDescriptor.getEntityName(), sourceDocument.getOrigin());
        }
    } else {
        idValue.setTypeUsingReflection(rootEntityDescriptor.getClassName(), propertyName);
    }
    relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, ((RelationalValueSourceContainer) idSource.getIdentifierAttributeSource()).getRelationalValueSources(), idValue, false, new RelationalObjectBinder.ColumnNamingDelegate() {

        @Override
        public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
            context.getBuildingOptions().getImplicitNamingStrategy().determineIdentifierColumnName(new ImplicitIdentifierColumnNameSource() {

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

                @Override
                public AttributePath getIdentifierAttributePath() {
                    return idSource.getIdentifierAttributeSource().getAttributePath();
                }

                @Override
                public MetadataBuildingContext getBuildingContext() {
                    return context;
                }
            });
            return database.toIdentifier(propertyName);
        }
    });
    if (propertyName != null) {
        Property prop = new Property();
        prop.setValue(idValue);
        bindProperty(sourceDocument, idSource.getIdentifierAttributeSource(), prop);
        rootEntityDescriptor.setIdentifierProperty(prop);
        rootEntityDescriptor.setDeclaredIdentifierProperty(prop);
    }
    makeIdentifier(sourceDocument, idSource.getIdentifierGeneratorDescriptor(), idSource.getUnsavedValue(), idValue);
}
Also used : ImplicitIdentifierColumnNameSource(org.hibernate.boot.model.naming.ImplicitIdentifierColumnNameSource) Identifier(org.hibernate.boot.model.naming.Identifier) IdentifierSourceSimple(org.hibernate.boot.model.source.spi.IdentifierSourceSimple) 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)

Example 9 with MappingException

use of org.hibernate.boot.MappingException in project hibernate-orm by hibernate.

the class ModelBinder method bindEntityTableSpecification.

private Table bindEntityTableSpecification(final MappingDocument mappingDocument, TableSpecificationSource tableSpecSource, Table denormalizedSuperTable, final EntitySource entitySource, PersistentClass entityDescriptor) {
    final Namespace namespace = database.locateNamespace(determineCatalogName(tableSpecSource), determineSchemaName(tableSpecSource));
    final boolean isTable = TableSource.class.isInstance(tableSpecSource);
    final boolean isAbstract = entityDescriptor.isAbstract() == null ? false : entityDescriptor.isAbstract();
    final String subselect;
    final Identifier logicalTableName;
    final Table table;
    if (isTable) {
        final TableSource tableSource = (TableSource) tableSpecSource;
        if (StringHelper.isNotEmpty(tableSource.getExplicitTableName())) {
            logicalTableName = database.toIdentifier(tableSource.getExplicitTableName());
        } else {
            final ImplicitEntityNameSource implicitNamingSource = new ImplicitEntityNameSource() {

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

                @Override
                public MetadataBuildingContext getBuildingContext() {
                    return mappingDocument;
                }
            };
            logicalTableName = mappingDocument.getBuildingOptions().getImplicitNamingStrategy().determinePrimaryTableName(implicitNamingSource);
        }
        if (denormalizedSuperTable == null) {
            table = namespace.createTable(logicalTableName, isAbstract);
        } else {
            table = namespace.createDenormalizedTable(logicalTableName, isAbstract, denormalizedSuperTable);
        }
    } else {
        final InLineViewSource inLineViewSource = (InLineViewSource) tableSpecSource;
        subselect = inLineViewSource.getSelectStatement();
        logicalTableName = database.toIdentifier(inLineViewSource.getLogicalName());
        if (denormalizedSuperTable == null) {
            table = new Table(namespace, subselect, isAbstract);
        } else {
            table = new DenormalizedTable(namespace, subselect, isAbstract, denormalizedSuperTable);
        }
        table.setName(logicalTableName.render());
    }
    EntityTableXref superEntityTableXref = null;
    if (entitySource.getSuperType() != null) {
        //noinspection SuspiciousMethodCalls
        final String superEntityName = ((EntitySource) entitySource.getSuperType()).getEntityNamingSource().getEntityName();
        superEntityTableXref = mappingDocument.getMetadataCollector().getEntityTableXref(superEntityName);
        if (superEntityTableXref == null) {
            throw new MappingException(String.format(Locale.ENGLISH, "Unable to locate entity table xref for entity [%s] super-type [%s]", entityDescriptor.getEntityName(), superEntityName), mappingDocument.getOrigin());
        }
    }
    mappingDocument.getMetadataCollector().addEntityTableXref(entitySource.getEntityNamingSource().getEntityName(), logicalTableName, table, superEntityTableXref);
    if (isTable) {
        final TableSource tableSource = (TableSource) tableSpecSource;
        table.setRowId(tableSource.getRowId());
        if (StringHelper.isNotEmpty(tableSource.getCheckConstraint())) {
            table.addCheckConstraint(tableSource.getCheckConstraint());
        }
    }
    table.setComment(tableSpecSource.getComment());
    mappingDocument.getMetadataCollector().addTableNameBinding(logicalTableName, table);
    return table;
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) TableSource(org.hibernate.boot.model.source.spi.TableSource) SecondaryTableSource(org.hibernate.boot.model.source.spi.SecondaryTableSource) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) EntityTableXref(org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref) ImplicitEntityNameSource(org.hibernate.boot.model.naming.ImplicitEntityNameSource) InLineViewSource(org.hibernate.boot.model.source.spi.InLineViewSource) Namespace(org.hibernate.boot.model.relational.Namespace) MappingException(org.hibernate.boot.MappingException)

Example 10 with MappingException

use of org.hibernate.boot.MappingException 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)

Aggregations

MappingException (org.hibernate.boot.MappingException)22 Identifier (org.hibernate.boot.model.naming.Identifier)7 LocalMetadataBuildingContext (org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext)4 RelationalValueSource (org.hibernate.boot.model.source.spi.RelationalValueSource)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Property (org.hibernate.mapping.Property)3 HashSet (java.util.HashSet)2 List (java.util.List)2 JAXBException (javax.xml.bind.JAXBException)2 JaxbHbmNativeQueryPropertyReturnType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmNativeQueryPropertyReturnType)2 ColumnSource (org.hibernate.boot.model.source.spi.ColumnSource)2 DerivedValueSource (org.hibernate.boot.model.source.spi.DerivedValueSource)2 VersionAttributeSource (org.hibernate.boot.model.source.spi.VersionAttributeSource)2 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)2 FilterDefinition (org.hibernate.engine.spi.FilterDefinition)2 Component (org.hibernate.mapping.Component)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 SimpleValue (org.hibernate.mapping.SimpleValue)2 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)2