Search in sources :

Example 21 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class SimpleValue method createParameterImpl.

private void createParameterImpl() {
    try {
        String[] columnsNames = new String[columns.size()];
        for (int i = 0; i < columns.size(); i++) {
            Selectable column = columns.get(i);
            if (column instanceof Column) {
                columnsNames[i] = ((Column) column).getName();
            }
        }
        final XProperty xProperty = (XProperty) typeParameters.get(DynamicParameterizedType.XPROPERTY);
        // todo : not sure this works for handling @MapKeyEnumerated
        final Annotation[] annotations = xProperty == null ? null : xProperty.getAnnotations();
        final ClassLoaderService classLoaderService = getMetadata().getMetadataBuildingOptions().getServiceRegistry().getService(ClassLoaderService.class);
        typeParameters.put(DynamicParameterizedType.PARAMETER_TYPE, new ParameterTypeImpl(classLoaderService.classForName(typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS)), annotations, table.getCatalog(), table.getSchema(), table.getName(), Boolean.valueOf(typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY)), columnsNames));
    } catch (ClassLoadingException e) {
        throw new MappingException("Could not create DynamicParameterizedType for type: " + typeName, e);
    }
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Annotation(java.lang.annotation.Annotation) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) MappingException(org.hibernate.MappingException)

Example 22 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class JPAMetadataProvider method getDefaults.

@Override
public Map<Object, Object> getDefaults() {
    if (defaults == null) {
        defaults = new HashMap<Object, Object>();
        XMLContext.Default xmlDefaults = xmlContext.getDefault(null);
        defaults.put("schema", xmlDefaults.getSchema());
        defaults.put("catalog", xmlDefaults.getCatalog());
        defaults.put("delimited-identifier", xmlDefaults.getDelimitedIdentifier());
        List<Class> entityListeners = new ArrayList<Class>();
        for (String className : xmlContext.getDefaultEntityListeners()) {
            try {
                entityListeners.add(classLoaderAccess.classForName(className));
            } catch (ClassLoadingException e) {
                throw new IllegalStateException("Default entity listener class not found: " + className);
            }
        }
        defaults.put(EntityListeners.class, entityListeners);
        for (Element element : xmlContext.getAllDocuments()) {
            @SuppressWarnings("unchecked") List<Element> elements = element.elements("sequence-generator");
            List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get(SequenceGenerator.class);
            if (sequenceGenerators == null) {
                sequenceGenerators = new ArrayList<SequenceGenerator>();
                defaults.put(SequenceGenerator.class, sequenceGenerators);
            }
            for (Element subelement : elements) {
                sequenceGenerators.add(JPAOverriddenAnnotationReader.buildSequenceGeneratorAnnotation(subelement));
            }
            elements = element.elements("table-generator");
            List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get(TableGenerator.class);
            if (tableGenerators == null) {
                tableGenerators = new ArrayList<TableGenerator>();
                defaults.put(TableGenerator.class, tableGenerators);
            }
            for (Element subelement : elements) {
                tableGenerators.add(JPAOverriddenAnnotationReader.buildTableGeneratorAnnotation(subelement, xmlDefaults));
            }
            List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get(NamedQuery.class);
            if (namedQueries == null) {
                namedQueries = new ArrayList<NamedQuery>();
                defaults.put(NamedQuery.class, namedQueries);
            }
            List<NamedQuery> currentNamedQueries = JPAOverriddenAnnotationReader.buildNamedQueries(element, false, xmlDefaults, classLoaderAccess);
            namedQueries.addAll(currentNamedQueries);
            List<NamedNativeQuery> namedNativeQueries = (List<NamedNativeQuery>) defaults.get(NamedNativeQuery.class);
            if (namedNativeQueries == null) {
                namedNativeQueries = new ArrayList<NamedNativeQuery>();
                defaults.put(NamedNativeQuery.class, namedNativeQueries);
            }
            List<NamedNativeQuery> currentNamedNativeQueries = JPAOverriddenAnnotationReader.buildNamedQueries(element, true, xmlDefaults, classLoaderAccess);
            namedNativeQueries.addAll(currentNamedNativeQueries);
            List<SqlResultSetMapping> sqlResultSetMappings = (List<SqlResultSetMapping>) defaults.get(SqlResultSetMapping.class);
            if (sqlResultSetMappings == null) {
                sqlResultSetMappings = new ArrayList<SqlResultSetMapping>();
                defaults.put(SqlResultSetMapping.class, sqlResultSetMappings);
            }
            List<SqlResultSetMapping> currentSqlResultSetMappings = JPAOverriddenAnnotationReader.buildSqlResultsetMappings(element, xmlDefaults, classLoaderAccess);
            sqlResultSetMappings.addAll(currentSqlResultSetMappings);
            List<NamedStoredProcedureQuery> namedStoredProcedureQueries = (List<NamedStoredProcedureQuery>) defaults.get(NamedStoredProcedureQuery.class);
            if (namedStoredProcedureQueries == null) {
                namedStoredProcedureQueries = new ArrayList<NamedStoredProcedureQuery>();
                defaults.put(NamedStoredProcedureQuery.class, namedStoredProcedureQueries);
            }
            List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAOverriddenAnnotationReader.buildNamedStoreProcedureQueries(element, xmlDefaults, classLoaderAccess);
            namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
        }
    }
    return defaults;
}
Also used : NamedNativeQuery(javax.persistence.NamedNativeQuery) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) SequenceGenerator(javax.persistence.SequenceGenerator) TableGenerator(javax.persistence.TableGenerator) NamedQuery(javax.persistence.NamedQuery) SqlResultSetMapping(javax.persistence.SqlResultSetMapping) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery)

Example 23 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class EntityBinder method bindEntity.

public void bindEntity() {
    persistentClass.setAbstract(annotatedClass.isAbstract());
    persistentClass.setClassName(annotatedClass.getName());
    persistentClass.setJpaEntityName(name);
    //persistentClass.setDynamic(false); //no longer needed with the Entity name refactoring?
    persistentClass.setEntityName(annotatedClass.getName());
    bindDiscriminatorValue();
    persistentClass.setLazy(lazy);
    if (proxyClass != null) {
        persistentClass.setProxyInterfaceName(proxyClass.getName());
    }
    persistentClass.setDynamicInsert(dynamicInsert);
    persistentClass.setDynamicUpdate(dynamicUpdate);
    if (persistentClass instanceof RootClass) {
        RootClass rootClass = (RootClass) persistentClass;
        boolean mutable = true;
        //priority on @Immutable, then @Entity.mutable()
        if (annotatedClass.isAnnotationPresent(Immutable.class)) {
            mutable = false;
        } else {
            org.hibernate.annotations.Entity entityAnn = annotatedClass.getAnnotation(org.hibernate.annotations.Entity.class);
            if (entityAnn != null) {
                mutable = entityAnn.mutable();
            }
        }
        rootClass.setMutable(mutable);
        rootClass.setExplicitPolymorphism(isExplicitPolymorphism(polymorphismType));
        if (StringHelper.isNotEmpty(where))
            rootClass.setWhere(where);
        if (cacheConcurrentStrategy != null) {
            rootClass.setCacheConcurrencyStrategy(cacheConcurrentStrategy);
            rootClass.setCacheRegionName(cacheRegion);
            rootClass.setLazyPropertiesCacheable(cacheLazyProperty);
        }
        rootClass.setNaturalIdCacheRegionName(naturalIdCacheRegion);
        boolean forceDiscriminatorInSelects = forceDiscriminator == null ? context.getBuildingOptions().shouldImplicitlyForceDiscriminatorInSelect() : forceDiscriminator;
        rootClass.setForceDiscriminator(forceDiscriminatorInSelects);
        if (insertableDiscriminator != null) {
            rootClass.setDiscriminatorInsertable(insertableDiscriminator);
        }
    } else {
        if (explicitHibernateEntityAnnotation) {
            LOG.entityAnnotationOnNonRoot(annotatedClass.getName());
        }
        if (annotatedClass.isAnnotationPresent(Immutable.class)) {
            LOG.immutableAnnotationOnNonRoot(annotatedClass.getName());
        }
        if (annotatedClass.isAnnotationPresent(Cacheable.class) || annotatedClass.isAnnotationPresent(Cache.class)) {
            LOG.cacheOrCacheableAnnotationOnNonRoot(annotatedClass.getName());
        }
    }
    persistentClass.setOptimisticLockStyle(getVersioning(optimisticLockType));
    persistentClass.setSelectBeforeUpdate(selectBeforeUpdate);
    //set persister if needed
    Persister persisterAnn = annotatedClass.getAnnotation(Persister.class);
    Class persister = null;
    if (persisterAnn != null) {
        persister = persisterAnn.impl();
    } else {
        org.hibernate.annotations.Entity entityAnn = annotatedClass.getAnnotation(org.hibernate.annotations.Entity.class);
        if (entityAnn != null && !BinderHelper.isEmptyAnnotationValue(entityAnn.persister())) {
            try {
                persister = context.getClassLoaderAccess().classForName(entityAnn.persister());
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Could not find persister class: " + entityAnn.persister(), e);
            }
        }
    }
    if (persister != null) {
        persistentClass.setEntityPersisterClass(persister);
    }
    persistentClass.setBatchSize(batchSize);
    //SQL overriding
    SQLInsert sqlInsert = annotatedClass.getAnnotation(SQLInsert.class);
    SQLUpdate sqlUpdate = annotatedClass.getAnnotation(SQLUpdate.class);
    SQLDelete sqlDelete = annotatedClass.getAnnotation(SQLDelete.class);
    SQLDeleteAll sqlDeleteAll = annotatedClass.getAnnotation(SQLDeleteAll.class);
    Loader loader = annotatedClass.getAnnotation(Loader.class);
    if (sqlInsert != null) {
        persistentClass.setCustomSQLInsert(sqlInsert.sql().trim(), sqlInsert.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlInsert.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlUpdate != null) {
        persistentClass.setCustomSQLUpdate(sqlUpdate.sql(), sqlUpdate.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlUpdate.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlDelete != null) {
        persistentClass.setCustomSQLDelete(sqlDelete.sql(), sqlDelete.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDelete.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (sqlDeleteAll != null) {
        persistentClass.setCustomSQLDelete(sqlDeleteAll.sql(), sqlDeleteAll.callable(), ExecuteUpdateResultCheckStyle.fromExternalName(sqlDeleteAll.check().toString().toLowerCase(Locale.ROOT)));
    }
    if (loader != null) {
        persistentClass.setLoaderName(loader.namedQuery());
    }
    final JdbcEnvironment jdbcEnvironment = context.getMetadataCollector().getDatabase().getJdbcEnvironment();
    if (annotatedClass.isAnnotationPresent(Synchronize.class)) {
        Synchronize synchronizedWith = annotatedClass.getAnnotation(Synchronize.class);
        String[] tables = synchronizedWith.value();
        for (String table : tables) {
            persistentClass.addSynchronizedTable(context.getBuildingOptions().getPhysicalNamingStrategy().toPhysicalTableName(jdbcEnvironment.getIdentifierHelper().toIdentifier(table), jdbcEnvironment).render(jdbcEnvironment.getDialect()));
        }
    }
    if (annotatedClass.isAnnotationPresent(Subselect.class)) {
        Subselect subselect = annotatedClass.getAnnotation(Subselect.class);
        this.subselect = subselect.value();
    }
    //tuplizers
    if (annotatedClass.isAnnotationPresent(Tuplizers.class)) {
        for (Tuplizer tuplizer : annotatedClass.getAnnotation(Tuplizers.class).value()) {
            EntityMode mode = EntityMode.parse(tuplizer.entityMode());
            //todo tuplizer.entityModeType
            persistentClass.addTuplizer(mode, tuplizer.impl().getName());
        }
    }
    if (annotatedClass.isAnnotationPresent(Tuplizer.class)) {
        Tuplizer tuplizer = annotatedClass.getAnnotation(Tuplizer.class);
        EntityMode mode = EntityMode.parse(tuplizer.entityMode());
        //todo tuplizer.entityModeType
        persistentClass.addTuplizer(mode, tuplizer.impl().getName());
    }
    for (Filter filter : filters) {
        String filterName = filter.name();
        String cond = filter.condition();
        if (BinderHelper.isEmptyAnnotationValue(cond)) {
            FilterDefinition definition = context.getMetadataCollector().getFilterDefinition(filterName);
            cond = definition == null ? null : definition.getDefaultFilterCondition();
            if (StringHelper.isEmpty(cond)) {
                throw new AnnotationException("no filter condition found for filter " + filterName + " in " + this.name);
            }
        }
        persistentClass.addFilter(filterName, cond, filter.deduceAliasInjectionPoints(), toAliasTableMap(filter.aliases()), toAliasEntityMap(filter.aliases()));
    }
    LOG.debugf("Import with entity name %s", name);
    try {
        context.getMetadataCollector().addImport(name, persistentClass.getEntityName());
        String entityName = persistentClass.getEntityName();
        if (!entityName.equals(name)) {
            context.getMetadataCollector().addImport(entityName, entityName);
        }
    } catch (MappingException me) {
        throw new AnnotationException("Use of the same entity name twice: " + name, me);
    }
    processNamedEntityGraphs();
}
Also used : Cacheable(javax.persistence.Cacheable) Synchronize(org.hibernate.annotations.Synchronize) Loader(org.hibernate.annotations.Loader) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) MappingException(org.hibernate.MappingException) FilterDefinition(org.hibernate.engine.spi.FilterDefinition) AnnotationException(org.hibernate.AnnotationException) SQLInsert(org.hibernate.annotations.SQLInsert) Persister(org.hibernate.annotations.Persister) RootClass(org.hibernate.mapping.RootClass) Tuplizer(org.hibernate.annotations.Tuplizer) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Subselect(org.hibernate.annotations.Subselect) SQLDelete(org.hibernate.annotations.SQLDelete) EntityMode(org.hibernate.EntityMode) Filter(org.hibernate.annotations.Filter) PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) XClass(org.hibernate.annotations.common.reflection.XClass) SQLUpdate(org.hibernate.annotations.SQLUpdate) SQLDeleteAll(org.hibernate.annotations.SQLDeleteAll) Tuplizers(org.hibernate.annotations.Tuplizers) NaturalIdCache(org.hibernate.annotations.NaturalIdCache) Cache(org.hibernate.annotations.Cache)

Example 24 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getMapKeyClass.

private void getMapKeyClass(List<Annotation> annotationList, Element element, XMLContext.Default defaults) {
    String nodeName = "map-key-class";
    Element subelement = element != null ? element.element(nodeName) : null;
    if (subelement != null) {
        String mapKeyClassName = subelement.attributeValue("class");
        AnnotationDescriptor ad = new AnnotationDescriptor(MapKeyClass.class);
        if (StringHelper.isNotEmpty(mapKeyClassName)) {
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(mapKeyClassName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find " + element.getPath() + " " + nodeName + ": " + mapKeyClassName, e);
            }
            ad.setValue("value", clazz);
        }
        annotationList.add(AnnotationFactory.create(ad));
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass)

Example 25 with ClassLoadingException

use of org.hibernate.boot.registry.classloading.spi.ClassLoadingException in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getEntityListeners.

private EntityListeners getEntityListeners(Element tree, XMLContext.Default defaults) {
    Element element = tree != null ? tree.element("entity-listeners") : null;
    if (element != null) {
        List<Class> entityListenerClasses = new ArrayList<Class>();
        for (Element subelement : (List<Element>) element.elements("entity-listener")) {
            String className = subelement.attributeValue("class");
            try {
                entityListenerClasses.add(classLoaderAccess.classForName(XMLContext.buildSafeClassName(className, defaults)));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find " + element.getPath() + ".class: " + className, e);
            }
        }
        AnnotationDescriptor ad = new AnnotationDescriptor(EntityListeners.class);
        ad.setValue("value", entityListenerClasses.toArray(new Class[entityListenerClasses.size()]));
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(EntityListeners.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass) ArrayList(java.util.ArrayList) List(java.util.List) EntityListeners(javax.persistence.EntityListeners)

Aggregations

ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)26 Element (org.dom4j.Element)10 AnnotationException (org.hibernate.AnnotationException)10 AnnotatedElement (java.lang.reflect.AnnotatedElement)9 IdClass (javax.persistence.IdClass)9 MapKeyClass (javax.persistence.MapKeyClass)9 ArrayList (java.util.ArrayList)8 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)7 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)7 List (java.util.List)5 MappingException (org.hibernate.MappingException)5 HibernateException (org.hibernate.HibernateException)3 AccessibleObject (java.lang.reflect.AccessibleObject)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 NamedNativeQuery (javax.persistence.NamedNativeQuery)2 NamedQuery (javax.persistence.NamedQuery)2 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)2 Attribute (org.dom4j.Attribute)2