Search in sources :

Example 11 with ClassLoadingException

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

the class UUIDGenerator method configure.

@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
    // check first for the strategy instance
    strategy = (UUIDGenerationStrategy) params.get(UUID_GEN_STRATEGY);
    if (strategy == null) {
        // next check for the strategy class
        final String strategyClassName = params.getProperty(UUID_GEN_STRATEGY_CLASS);
        if (strategyClassName != null) {
            try {
                final ClassLoaderService cls = serviceRegistry.getService(ClassLoaderService.class);
                final Class strategyClass = cls.classForName(strategyClassName);
                try {
                    strategy = (UUIDGenerationStrategy) strategyClass.newInstance();
                } catch (Exception ignore) {
                    LOG.unableToInstantiateUuidGenerationStrategy(ignore);
                }
            } catch (ClassLoadingException ignore) {
                LOG.unableToLocateUuidGenerationStrategy(strategyClassName);
            }
        }
    }
    if (strategy == null) {
        // lastly use the standard random generator
        strategy = StandardRandomStrategy.INSTANCE;
    }
    if (UUID.class.isAssignableFrom(type.getReturnedClass())) {
        valueTransformer = UUIDTypeDescriptor.PassThroughTransformer.INSTANCE;
    } else if (String.class.isAssignableFrom(type.getReturnedClass())) {
        valueTransformer = UUIDTypeDescriptor.ToStringTransformer.INSTANCE;
    } else if (byte[].class.isAssignableFrom(type.getReturnedClass())) {
        valueTransformer = UUIDTypeDescriptor.ToBytesTransformer.INSTANCE;
    } else {
        throw new HibernateException("Unanticipated return type [" + type.getReturnedClass().getName() + "] for UUID conversion");
    }
}
Also used : HibernateException(org.hibernate.HibernateException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) MappingException(org.hibernate.MappingException) HibernateException(org.hibernate.HibernateException) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 12 with ClassLoadingException

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

the class AbstractSharedSessionContract method resultClassChecking.

@SuppressWarnings({ "unchecked", "WeakerAccess" })
protected void resultClassChecking(Class resultType, NamedSQLQueryDefinition namedQueryDefinition) {
    final NativeSQLQueryReturn[] queryReturns;
    if (namedQueryDefinition.getQueryReturns() != null) {
        queryReturns = namedQueryDefinition.getQueryReturns();
    } else if (namedQueryDefinition.getResultSetRef() != null) {
        final ResultSetMappingDefinition rsMapping = getFactory().getNamedQueryRepository().getResultSetMappingDefinition(namedQueryDefinition.getResultSetRef());
        queryReturns = rsMapping.getQueryReturns();
    } else {
        throw new AssertionFailure("Unsupported named query model. Please report the bug in Hibernate EntityManager");
    }
    if (queryReturns.length > 1) {
        throw new IllegalArgumentException("Cannot create TypedQuery for query with more than one return");
    }
    final NativeSQLQueryReturn nativeSQLQueryReturn = queryReturns[0];
    if (nativeSQLQueryReturn instanceof NativeSQLQueryRootReturn) {
        final Class<?> actualReturnedClass;
        final String entityClassName = ((NativeSQLQueryRootReturn) nativeSQLQueryReturn).getReturnEntityName();
        try {
            actualReturnedClass = getFactory().getServiceRegistry().getService(ClassLoaderService.class).classForName(entityClassName);
        } catch (ClassLoadingException e) {
            throw new AssertionFailure("Unable to load class [" + entityClassName + "] declared on named native query [" + namedQueryDefinition.getName() + "]");
        }
        if (!resultType.isAssignableFrom(actualReturnedClass)) {
            throw buildIncompatibleException(resultType, actualReturnedClass);
        }
    } else if (nativeSQLQueryReturn instanceof NativeSQLQueryConstructorReturn) {
        final NativeSQLQueryConstructorReturn ctorRtn = (NativeSQLQueryConstructorReturn) nativeSQLQueryReturn;
        if (!resultType.isAssignableFrom(ctorRtn.getTargetClass())) {
            throw buildIncompatibleException(resultType, ctorRtn.getTargetClass());
        }
    } else {
        log.debugf("Skiping unhandled NativeSQLQueryReturn type : " + nativeSQLQueryReturn);
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) NativeSQLQueryConstructorReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryConstructorReturn) NativeSQLQueryReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) ResultSetMappingDefinition(org.hibernate.engine.ResultSetMappingDefinition)

Example 13 with ClassLoadingException

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

the class MetamodelImpl method getImplementors.

/**
	 * Given the name of an entity class, determine all the class and interface names by which it can be
	 * referenced in an HQL query.
	 *
	 * @param className The name of the entity class
	 *
	 * @return the names of all persistent (mapped) classes that extend or implement the
	 *     given class or interface, accounting for implicit/explicit polymorphism settings
	 *     and excluding mapped subclasses/joined-subclasses of other classes in the result.
	 * @throws MappingException
	 */
public String[] getImplementors(String className) throws MappingException {
    final Class clazz;
    try {
        clazz = getSessionFactory().getServiceRegistry().getService(ClassLoaderService.class).classForName(className);
    } catch (ClassLoadingException e) {
        //for a dynamic-class
        return new String[] { className };
    }
    ArrayList<String> results = new ArrayList<>();
    for (EntityPersister checkPersister : entityPersisters().values()) {
        if (!Queryable.class.isInstance(checkPersister)) {
            continue;
        }
        final Queryable checkQueryable = Queryable.class.cast(checkPersister);
        final String checkQueryableEntityName = checkQueryable.getEntityName();
        final boolean isMappedClass = className.equals(checkQueryableEntityName);
        if (checkQueryable.isExplicitPolymorphism()) {
            if (isMappedClass) {
                //NOTE EARLY EXIT
                return new String[] { className };
            }
        } else {
            if (isMappedClass) {
                results.add(checkQueryableEntityName);
            } else {
                final Class mappedClass = checkQueryable.getMappedClass();
                if (mappedClass != null && clazz.isAssignableFrom(mappedClass)) {
                    final boolean assignableSuperclass;
                    if (checkQueryable.isInherited()) {
                        Class mappedSuperclass = entityPersister(checkQueryable.getMappedSuperclass()).getMappedClass();
                        assignableSuperclass = clazz.isAssignableFrom(mappedSuperclass);
                    } else {
                        assignableSuperclass = false;
                    }
                    if (!assignableSuperclass) {
                        results.add(checkQueryableEntityName);
                    }
                }
            }
        }
    }
    return results.toArray(new String[results.size()]);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Queryable(org.hibernate.persister.entity.Queryable) ArrayList(java.util.ArrayList) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 14 with ClassLoadingException

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

the class JPAOverriddenAnnotationReader method bindNamedSubgraph.

private static void bindNamedSubgraph(XMLContext.Default defaults, AnnotationDescriptor ann, List<Element> subgraphNodes, ClassLoaderAccess classLoaderAccess) {
    List<NamedSubgraph> annSubgraphNodes = new ArrayList<NamedSubgraph>();
    for (Element subgraphNode : subgraphNodes) {
        AnnotationDescriptor annSubgraphNode = new AnnotationDescriptor(NamedSubgraph.class);
        copyStringAttribute(annSubgraphNode, subgraphNode, "name", true);
        String clazzName = subgraphNode.attributeValue("class");
        Class clazz;
        try {
            clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
        } catch (ClassLoadingException e) {
            throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
        }
        annSubgraphNode.setValue("type", clazz);
        bindNamedAttributeNodes(subgraphNode, annSubgraphNode);
        annSubgraphNodes.add((NamedSubgraph) AnnotationFactory.create(annSubgraphNode));
    }
    ann.setValue("subgraphs", annSubgraphNodes.toArray(new NamedSubgraph[annSubgraphNodes.size()]));
}
Also used : NamedSubgraph(javax.persistence.NamedSubgraph) 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)

Example 15 with ClassLoadingException

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

the class JPAOverriddenAnnotationReader method buildNamedStoreProcedureQueries.

public static List<NamedStoredProcedureQuery> buildNamedStoreProcedureQueries(Element element, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
    if (element == null) {
        return new ArrayList<NamedStoredProcedureQuery>();
    }
    List namedStoredProcedureElements = element.elements("named-stored-procedure-query");
    List<NamedStoredProcedureQuery> namedStoredProcedureQueries = new ArrayList<NamedStoredProcedureQuery>();
    for (Object obj : namedStoredProcedureElements) {
        Element subElement = (Element) obj;
        AnnotationDescriptor ann = new AnnotationDescriptor(NamedStoredProcedureQuery.class);
        copyStringAttribute(ann, subElement, "name", true);
        copyStringAttribute(ann, subElement, "procedure-name", true);
        List<Element> elements = subElement.elements("parameter");
        List<StoredProcedureParameter> storedProcedureParameters = new ArrayList<StoredProcedureParameter>();
        for (Element parameterElement : elements) {
            AnnotationDescriptor parameterDescriptor = new AnnotationDescriptor(StoredProcedureParameter.class);
            copyStringAttribute(parameterDescriptor, parameterElement, "name", false);
            String modeValue = parameterElement.attributeValue("mode");
            if (modeValue == null) {
                parameterDescriptor.setValue("mode", ParameterMode.IN);
            } else {
                parameterDescriptor.setValue("mode", ParameterMode.valueOf(modeValue.toUpperCase(Locale.ROOT)));
            }
            String clazzName = parameterElement.attributeValue("class");
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            parameterDescriptor.setValue("type", clazz);
            storedProcedureParameters.add((StoredProcedureParameter) AnnotationFactory.create(parameterDescriptor));
        }
        ann.setValue("parameters", storedProcedureParameters.toArray(new StoredProcedureParameter[storedProcedureParameters.size()]));
        elements = subElement.elements("result-class");
        List<Class> returnClasses = new ArrayList<Class>();
        for (Element classElement : elements) {
            String clazzName = classElement.getTextTrim();
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            returnClasses.add(clazz);
        }
        ann.setValue("resultClasses", returnClasses.toArray(new Class[returnClasses.size()]));
        elements = subElement.elements("result-set-mapping");
        List<String> resultSetMappings = new ArrayList<String>();
        for (Element resultSetMappingElement : elements) {
            resultSetMappings.add(resultSetMappingElement.getTextTrim());
        }
        ann.setValue("resultSetMappings", resultSetMappings.toArray(new String[resultSetMappings.size()]));
        elements = subElement.elements("hint");
        buildQueryHints(elements, ann);
        namedStoredProcedureQueries.add((NamedStoredProcedureQuery) AnnotationFactory.create(ann));
    }
    return namedStoredProcedureQueries;
}
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) StoredProcedureParameter(javax.persistence.StoredProcedureParameter) AnnotationException(org.hibernate.AnnotationException) ArrayList(java.util.ArrayList) List(java.util.List) AccessibleObject(java.lang.reflect.AccessibleObject) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery)

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