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");
}
}
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);
}
}
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()]);
}
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()]));
}
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;
}
Aggregations