Search in sources :

Example 1 with MappingException

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

the class ScanningCoordinator method applyScanResultsToManagedResources.

public void applyScanResultsToManagedResources(ManagedResourcesImpl managedResources, ScanResult scanResult, MetadataBuildingOptions options, XmlMappingBinderAccess xmlMappingBinderAccess) {
    final ScanEnvironment scanEnvironment = options.getScanEnvironment();
    final ServiceRegistry serviceRegistry = options.getServiceRegistry();
    final ClassLoaderService classLoaderService = serviceRegistry.getService(ClassLoaderService.class);
    // mapping files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final Set<String> nonLocatedMappingFileNames = new HashSet<String>();
    final List<String> explicitMappingFileNames = scanEnvironment.getExplicitlyListedMappingFiles();
    if (explicitMappingFileNames != null) {
        nonLocatedMappingFileNames.addAll(explicitMappingFileNames);
    }
    for (MappingFileDescriptor mappingFileDescriptor : scanResult.getLocatedMappingFiles()) {
        managedResources.addXmlBinding(xmlMappingBinderAccess.bind(mappingFileDescriptor.getStreamAccess()));
        nonLocatedMappingFileNames.remove(mappingFileDescriptor.getName());
    }
    for (String name : nonLocatedMappingFileNames) {
        final URL url = classLoaderService.locateResource(name);
        if (url == null) {
            throw new MappingException("Unable to resolve explicitly named mapping-file : " + name, new Origin(SourceType.RESOURCE, name));
        }
        final UrlInputStreamAccess inputStreamAccess = new UrlInputStreamAccess(url);
        managedResources.addXmlBinding(xmlMappingBinderAccess.bind(inputStreamAccess));
    }
    // classes and packages ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final List<String> unresolvedListedClassNames = scanEnvironment.getExplicitlyListedClassNames() == null ? new ArrayList<String>() : new ArrayList<String>(scanEnvironment.getExplicitlyListedClassNames());
    for (ClassDescriptor classDescriptor : scanResult.getLocatedClasses()) {
        if (classDescriptor.getCategorization() == ClassDescriptor.Categorization.CONVERTER) {
            // converter classes are safe to load because we never enhance them,
            // and notice we use the ClassLoaderService specifically, not the temp ClassLoader (if any)
            managedResources.addAttributeConverterDefinition(AttributeConverterDefinition.from(classLoaderService.<AttributeConverter>classForName(classDescriptor.getName())));
        } else if (classDescriptor.getCategorization() == ClassDescriptor.Categorization.MODEL) {
            managedResources.addAnnotatedClassName(classDescriptor.getName());
        }
        unresolvedListedClassNames.remove(classDescriptor.getName());
    }
    // IMPL NOTE : "explicitlyListedClassNames" can contain class or package names...
    for (PackageDescriptor packageDescriptor : scanResult.getLocatedPackages()) {
        managedResources.addAnnotatedPackageName(packageDescriptor.getName());
        unresolvedListedClassNames.remove(packageDescriptor.getName());
    }
    for (String unresolvedListedClassName : unresolvedListedClassNames) {
        // because the explicit list can contain either class names or package names
        // we need to check for both here...
        // First, try it as a class name
        final String classFileName = unresolvedListedClassName.replace('.', '/') + ".class";
        final URL classFileUrl = classLoaderService.locateResource(classFileName);
        if (classFileUrl != null) {
            managedResources.addAnnotatedClassName(unresolvedListedClassName);
            continue;
        }
        // Then, try it as a package name
        final String packageInfoFileName = unresolvedListedClassName.replace('.', '/') + "/package-info.class";
        final URL packageInfoFileUrl = classLoaderService.locateResource(packageInfoFileName);
        if (packageInfoFileUrl != null) {
            managedResources.addAnnotatedPackageName(unresolvedListedClassName);
            continue;
        }
        log.debugf("Unable to resolve class [%s] named in persistence unit [%s]", unresolvedListedClassName, scanEnvironment.getRootUrl());
    }
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) MappingFileDescriptor(org.hibernate.boot.archive.scan.spi.MappingFileDescriptor) ClassDescriptor(org.hibernate.boot.archive.scan.spi.ClassDescriptor) UrlInputStreamAccess(org.hibernate.boot.archive.internal.UrlInputStreamAccess) PackageDescriptor(org.hibernate.boot.archive.scan.spi.PackageDescriptor) URL(java.net.URL) MappingException(org.hibernate.boot.MappingException) AttributeConverter(javax.persistence.AttributeConverter) ScanEnvironment(org.hibernate.boot.archive.scan.spi.ScanEnvironment) ServiceRegistry(org.hibernate.service.ServiceRegistry) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) HashSet(java.util.HashSet)

Example 2 with MappingException

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

the class EntityHierarchySourceImpl method processSubclass.

public void processSubclass(SubclassEntitySourceImpl subclassEntitySource) {
    final InheritanceType inheritanceType = Helper.interpretInheritanceType(subclassEntitySource.jaxbEntityMapping());
    if (hierarchyInheritanceType == InheritanceType.NO_INHERITANCE) {
        hierarchyInheritanceType = inheritanceType;
    } else if (hierarchyInheritanceType != inheritanceType) {
        throw new MappingException("Mixed inheritance strategies not supported", subclassEntitySource.getOrigin());
    }
    collectedEntityNames.add(subclassEntitySource.getEntityNamingSource().getEntityName());
}
Also used : InheritanceType(org.hibernate.boot.model.source.spi.InheritanceType) MappingException(org.hibernate.boot.MappingException)

Example 3 with MappingException

use of org.hibernate.boot.MappingException 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 4 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 5 with MappingException

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

the class ModelBinder method bindBasicEntityValues.

private void bindBasicEntityValues(MappingDocument sourceDocument, AbstractEntitySourceImpl entitySource, PersistentClass entityDescriptor) {
    entityDescriptor.setEntityName(entitySource.getEntityNamingSource().getEntityName());
    entityDescriptor.setJpaEntityName(entitySource.getEntityNamingSource().getJpaEntityName());
    entityDescriptor.setClassName(entitySource.getEntityNamingSource().getClassName());
    entityDescriptor.setDiscriminatorValue(entitySource.getDiscriminatorMatchValue() != null ? entitySource.getDiscriminatorMatchValue() : entityDescriptor.getEntityName());
    // NOTE : entitySource#isLazy already accounts for MappingDefaults#areEntitiesImplicitlyLazy
    if (StringHelper.isNotEmpty(entitySource.getProxy())) {
        final String qualifiedProxyName = sourceDocument.qualifyClassName(entitySource.getProxy());
        entityDescriptor.setProxyInterfaceName(qualifiedProxyName);
        entityDescriptor.setLazy(true);
    } else if (entitySource.isLazy()) {
        entityDescriptor.setProxyInterfaceName(entityDescriptor.getClassName());
        entityDescriptor.setLazy(true);
    } else {
        entityDescriptor.setProxyInterfaceName(null);
        entityDescriptor.setLazy(false);
    }
    entityDescriptor.setAbstract(entitySource.isAbstract());
    sourceDocument.getMetadataCollector().addImport(entitySource.getEntityNamingSource().getEntityName(), entitySource.getEntityNamingSource().getEntityName());
    if (sourceDocument.getMappingDefaults().isAutoImportEnabled() && entitySource.getEntityNamingSource().getEntityName().indexOf('.') > 0) {
        sourceDocument.getMetadataCollector().addImport(StringHelper.unqualify(entitySource.getEntityNamingSource().getEntityName()), entitySource.getEntityNamingSource().getEntityName());
    }
    if (entitySource.getTuplizerClassMap() != null) {
        if (entitySource.getTuplizerClassMap().size() > 1) {
            DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfMultipleEntityModeSupport();
        }
        for (Map.Entry<EntityMode, String> tuplizerEntry : entitySource.getTuplizerClassMap().entrySet()) {
            entityDescriptor.addTuplizer(tuplizerEntry.getKey(), tuplizerEntry.getValue());
        }
    }
    if (StringHelper.isNotEmpty(entitySource.getXmlNodeName())) {
        DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfDomEntityModeSupport();
    }
    entityDescriptor.setDynamicInsert(entitySource.isDynamicInsert());
    entityDescriptor.setDynamicUpdate(entitySource.isDynamicUpdate());
    entityDescriptor.setBatchSize(entitySource.getBatchSize());
    entityDescriptor.setSelectBeforeUpdate(entitySource.isSelectBeforeUpdate());
    if (StringHelper.isNotEmpty(entitySource.getCustomPersisterClassName())) {
        try {
            entityDescriptor.setEntityPersisterClass(sourceDocument.getBootstrapContext().getClassLoaderAccess().classForName(entitySource.getCustomPersisterClassName()));
        } catch (ClassLoadingException e) {
            throw new MappingException(String.format(Locale.ENGLISH, "Unable to load specified persister class : %s", entitySource.getCustomPersisterClassName()), e, sourceDocument.getOrigin());
        }
    }
    bindCustomSql(sourceDocument, entitySource, entityDescriptor);
    final JdbcEnvironment jdbcEnvironment = sourceDocument.getMetadataCollector().getDatabase().getJdbcEnvironment();
    for (String tableName : entitySource.getSynchronizedTableNames()) {
        final Identifier physicalTableName = sourceDocument.getBuildingOptions().getPhysicalNamingStrategy().toPhysicalTableName(jdbcEnvironment.getIdentifierHelper().toIdentifier(tableName), jdbcEnvironment);
        entityDescriptor.addSynchronizedTable(physicalTableName.render(jdbcEnvironment.getDialect()));
    }
    for (FilterSource filterSource : entitySource.getFilterSources()) {
        String condition = filterSource.getCondition();
        if (condition == null) {
            final FilterDefinition filterDefinition = sourceDocument.getMetadataCollector().getFilterDefinition(filterSource.getName());
            if (filterDefinition != null) {
                condition = filterDefinition.getDefaultFilterCondition();
            }
        }
        entityDescriptor.addFilter(filterSource.getName(), condition, filterSource.shouldAutoInjectAliases(), filterSource.getAliasToTableMap(), filterSource.getAliasToEntityMap());
    }
    for (JaxbHbmNamedQueryType namedQuery : entitySource.getNamedQueries()) {
        NamedQueryBinder.processNamedQuery(sourceDocument, namedQuery, entitySource.getEntityNamingSource().getEntityName() + ".");
    }
    for (JaxbHbmNamedNativeQueryType namedQuery : entitySource.getNamedNativeQueries()) {
        NamedQueryBinder.processNamedNativeQuery(sourceDocument, namedQuery, entitySource.getEntityNamingSource().getEntityName() + ".");
    }
    entityDescriptor.setMetaAttributes(entitySource.getToolingHintContext().getMetaAttributeMap());
}
Also used : FilterDefinition(org.hibernate.engine.spi.FilterDefinition) Identifier(org.hibernate.boot.model.naming.Identifier) FilterSource(org.hibernate.boot.model.source.spi.FilterSource) JaxbHbmNamedNativeQueryType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmNamedNativeQueryType) JaxbHbmNamedQueryType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmNamedQueryType) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Map(java.util.Map) HashMap(java.util.HashMap) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) EntityMode(org.hibernate.EntityMode) MappingException(org.hibernate.boot.MappingException)

Aggregations

MappingException (org.hibernate.boot.MappingException)23 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 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Property (org.hibernate.mapping.Property)3 URL (java.net.URL)2 List (java.util.List)2 AttributeConverter (javax.persistence.AttributeConverter)2 JAXBException (javax.xml.bind.JAXBException)2 UrlInputStreamAccess (org.hibernate.boot.archive.internal.UrlInputStreamAccess)2 ClassDescriptor (org.hibernate.boot.archive.scan.spi.ClassDescriptor)2 MappingFileDescriptor (org.hibernate.boot.archive.scan.spi.MappingFileDescriptor)2 PackageDescriptor (org.hibernate.boot.archive.scan.spi.PackageDescriptor)2 ScanEnvironment (org.hibernate.boot.archive.scan.spi.ScanEnvironment)2 Origin (org.hibernate.boot.jaxb.Origin)2 JaxbHbmNativeQueryPropertyReturnType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmNativeQueryPropertyReturnType)2 ColumnSource (org.hibernate.boot.model.source.spi.ColumnSource)2