use of org.hibernate.AnnotationException 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<>();
}
List namedStoredProcedureElements = element.elements("named-stored-procedure-query");
List<NamedStoredProcedureQuery> namedStoredProcedureQueries = new ArrayList<>();
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<>();
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(AnnotationFactory.create(parameterDescriptor));
}
ann.setValue("parameters", storedProcedureParameters.toArray(new StoredProcedureParameter[storedProcedureParameters.size()]));
elements = subElement.elements("result-class");
List<Class> returnClasses = new ArrayList<>();
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<>();
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(AnnotationFactory.create(ann));
}
return namedStoredProcedureQueries;
}
use of org.hibernate.AnnotationException 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<>();
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;
}
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReader method getColumn.
private Column getColumn(Element element, boolean isMandatory, Element current) {
// Element subelement = element != null ? element.element( "column" ) : null;
if (element != null) {
AnnotationDescriptor column = new AnnotationDescriptor(Column.class);
copyStringAttribute(column, element, "name", false);
copyBooleanAttribute(column, element, "unique");
copyBooleanAttribute(column, element, "nullable");
copyBooleanAttribute(column, element, "insertable");
copyBooleanAttribute(column, element, "updatable");
copyStringAttribute(column, element, "column-definition", false);
copyStringAttribute(column, element, "table", false);
copyIntegerAttribute(column, element, "length");
copyIntegerAttribute(column, element, "precision");
copyIntegerAttribute(column, element, "scale");
return (Column) AnnotationFactory.create(column);
} else {
if (isMandatory) {
throw new AnnotationException(current.getPath() + ".column is mandatory. " + SCHEMA_VALIDATION);
}
return null;
}
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class PropertyBinder method instantiateAndInitializeValueGeneration.
/**
* Instantiates the given generator annotation type, initializing it with the given instance of the corresponding
* generator annotation and the property's type.
*/
private <A extends Annotation> AnnotationValueGeneration<A> instantiateAndInitializeValueGeneration(A annotation, Class<? extends AnnotationValueGeneration<?>> generationType, XProperty property) {
try {
// This will cause a CCE in case the generation type doesn't match the annotation type; As this would be a
// programming error of the generation type developer and thus should show up during testing, we don't
// check this explicitly; If required, this could be done e.g. using ClassMate
@SuppressWarnings("unchecked") AnnotationValueGeneration<A> valueGeneration = (AnnotationValueGeneration<A>) generationType.newInstance();
valueGeneration.initialize(annotation, buildingContext.getBootstrapContext().getReflectionManager().toClass(property.getType()));
return valueGeneration;
} catch (HibernateException e) {
throw e;
} catch (Exception e) {
throw new AnnotationException("Exception occurred during processing of generator annotation:" + StringHelper.qualify(holder.getPath(), name), e);
}
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class AnnotationBinder method fillComponent.
public static Component fillComponent(PropertyHolder propertyHolder, PropertyData inferredData, // base inferred data correspond to the entity reproducing inferredData's properties (ie IdClass)
PropertyData baseInferredData, AccessType propertyAccessor, boolean isNullable, EntityBinder entityBinder, boolean isComponentEmbedded, boolean isIdentifierMapper, boolean inSecondPass, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
/**
* inSecondPass can only be used to apply right away the second pass of a composite-element
* Because it's a value type, there is no bidirectional association, hence second pass
* ordering does not matter
*/
Component comp = createComponent(propertyHolder, inferredData, isComponentEmbedded, isIdentifierMapper, buildingContext);
String subpath = BinderHelper.getPath(propertyHolder, inferredData);
LOG.tracev("Binding component with path: {0}", subpath);
PropertyHolder subHolder = PropertyHolderBuilder.buildPropertyHolder(comp, subpath, inferredData, propertyHolder, buildingContext);
// propertyHolder here is the owner of the component property. Tell it we are about to start the component...
propertyHolder.startingProperty(inferredData.getProperty());
final XClass xClassProcessed = inferredData.getPropertyClass();
List<PropertyData> classElements = new ArrayList<>();
XClass returnedClassOrElement = inferredData.getClassOrElement();
List<PropertyData> baseClassElements = null;
Map<String, PropertyData> orderedBaseClassElements = new HashMap<>();
XClass baseReturnedClassOrElement;
if (baseInferredData != null) {
baseClassElements = new ArrayList<>();
baseReturnedClassOrElement = baseInferredData.getClassOrElement();
bindTypeDefs(baseReturnedClassOrElement, buildingContext);
// might be spread across the subclasses and super classes.
while (!Object.class.getName().equals(baseReturnedClassOrElement.getName())) {
PropertyContainer propContainer = new PropertyContainer(baseReturnedClassOrElement, xClassProcessed, propertyAccessor);
addElementsOfClass(baseClassElements, propContainer, buildingContext);
for (PropertyData element : baseClassElements) {
orderedBaseClassElements.put(element.getPropertyName(), element);
}
baseReturnedClassOrElement = baseReturnedClassOrElement.getSuperclass();
}
}
// embeddable elements can have type defs
bindTypeDefs(returnedClassOrElement, buildingContext);
PropertyContainer propContainer = new PropertyContainer(returnedClassOrElement, xClassProcessed, propertyAccessor);
addElementsOfClass(classElements, propContainer, buildingContext);
// add elements of the embeddable superclass
XClass superClass = xClassProcessed.getSuperclass();
while (superClass != null && superClass.isAnnotationPresent(MappedSuperclass.class)) {
// FIXME: proper support of typevariables incl var resolved at upper levels
propContainer = new PropertyContainer(superClass, xClassProcessed, propertyAccessor);
addElementsOfClass(classElements, propContainer, buildingContext);
superClass = superClass.getSuperclass();
}
if (baseClassElements != null) {
// useful to avoid breaking pre JPA 2 mappings
if (!hasAnnotationsOnIdClass(xClassProcessed)) {
for (int i = 0; i < classElements.size(); i++) {
final PropertyData idClassPropertyData = classElements.get(i);
final PropertyData entityPropertyData = orderedBaseClassElements.get(idClassPropertyData.getPropertyName());
if (propertyHolder.isInIdClass()) {
if (entityPropertyData == null) {
throw new AnnotationException("Property of @IdClass not found in entity " + baseInferredData.getPropertyClass().getName() + ": " + idClassPropertyData.getPropertyName());
}
final boolean hasXToOneAnnotation = entityPropertyData.getProperty().isAnnotationPresent(ManyToOne.class) || entityPropertyData.getProperty().isAnnotationPresent(OneToOne.class);
final boolean isOfDifferentType = !entityPropertyData.getClassOrElement().equals(idClassPropertyData.getClassOrElement());
if (hasXToOneAnnotation && isOfDifferentType) {
// don't replace here as we need to use the actual original return type
// the annotation overriding will be dealt with by a mechanism similar to @MapsId
} else {
// this works since they are in the same order
classElements.set(i, entityPropertyData);
}
} else {
// this works since they are in the same order
classElements.set(i, entityPropertyData);
}
}
}
}
for (PropertyData propertyAnnotatedElement : classElements) {
processElementAnnotations(subHolder, isNullable ? Nullability.NO_CONSTRAINT : Nullability.FORCED_NOT_NULL, propertyAnnotatedElement, new HashMap<>(), entityBinder, isIdentifierMapper, isComponentEmbedded, inSecondPass, buildingContext, inheritanceStatePerClass);
XProperty property = propertyAnnotatedElement.getProperty();
if (property.isAnnotationPresent(GeneratedValue.class) && property.isAnnotationPresent(Id.class)) {
// clone classGenerator and override with local values
// Map<String, IdentifierGeneratorDefinition> localGenerators = new HashMap<>();
// localGenerators.putAll( buildGenerators( property, buildingContext ) );
buildGenerators(property, buildingContext);
GeneratedValue generatedValue = property.getAnnotation(GeneratedValue.class);
String generatorType = generatedValue != null ? generatorType(generatedValue, buildingContext, property.getType()) : "assigned";
String generator = generatedValue != null ? generatedValue.generator() : BinderHelper.ANNOTATION_STRING_DEFAULT;
SecondPass secondPass = new IdGeneratorResolverSecondPass((SimpleValue) comp.getProperty(property.getName()).getValue(), property, generatorType, generator, buildingContext);
buildingContext.getMetadataCollector().addSecondPass(secondPass);
}
}
return comp;
}
Aggregations