use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReader method copyIntegerAttribute.
private static void copyIntegerAttribute(AnnotationDescriptor annotation, Element element, String attributeName) {
String attribute = element.attributeValue(attributeName);
if (attribute != null) {
String annotationAttributeName = getJavaAttributeNameFromXMLOne(attributeName);
annotation.setValue(annotationAttributeName, attribute);
try {
int length = Integer.parseInt(attribute);
annotation.setValue(annotationAttributeName, length);
} catch (NumberFormatException e) {
throw new AnnotationException(element.getPath() + attributeName + " not parseable: " + attribute + " (" + SCHEMA_VALIDATION + ")");
}
}
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReader method buildNamedQueries.
public static List buildNamedQueries(Element element, boolean isNative, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
if (element == null) {
return new ArrayList();
}
List namedQueryElementList = isNative ? element.elements("named-native-query") : element.elements("named-query");
List namedQueries = new ArrayList();
for (Object aNamedQueryElementList : namedQueryElementList) {
Element subelement = (Element) aNamedQueryElementList;
AnnotationDescriptor ann = new AnnotationDescriptor(isNative ? NamedNativeQuery.class : NamedQuery.class);
copyStringAttribute(ann, subelement, "name", false);
Element queryElt = subelement.element("query");
if (queryElt == null) {
throw new AnnotationException("No <query> element found." + SCHEMA_VALIDATION);
}
copyStringElement(queryElt, ann, "query");
List<Element> elements = subelement.elements("hint");
buildQueryHints(elements, ann);
String clazzName = subelement.attributeValue("result-class");
if (StringHelper.isNotEmpty(clazzName)) {
Class clazz;
try {
clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
} catch (ClassLoadingException e) {
throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
}
ann.setValue("resultClass", clazz);
}
copyStringAttribute(ann, subelement, "result-set-mapping", false);
namedQueries.add(AnnotationFactory.create(ann));
}
return namedQueries;
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReader method getIdClass.
private IdClass getIdClass(Element tree, XMLContext.Default defaults) {
Element element = tree == null ? null : tree.element("id-class");
if (element != null) {
Attribute attr = element.attribute("class");
if (attr != null) {
AnnotationDescriptor ad = new AnnotationDescriptor(IdClass.class);
Class clazz;
try {
clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(attr.getValue(), defaults));
} catch (ClassLoadingException e) {
throw new AnnotationException("Unable to find id-class: " + attr.getValue(), e);
}
ad.setValue("value", clazz);
return AnnotationFactory.create(ad);
} else {
throw new AnnotationException("id-class without class. " + SCHEMA_VALIDATION);
}
} else if (defaults.canUseJavaAnnotations()) {
return getPhysicalAnnotation(IdClass.class);
} else {
return null;
}
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class ImmutableTest method testMisplacedImmutableAnnotation.
@Test
public void testMisplacedImmutableAnnotation() {
MetadataSources metadataSources = new MetadataSources().addAnnotatedClass(Foobar.class);
try {
metadataSources.buildMetadata();
fail("Expecting exception due to misplaced @Immutable annotation");
} catch (AnnotationException ignore) {
} finally {
ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();
if (metaServiceRegistry instanceof BootstrapServiceRegistry) {
BootstrapServiceRegistryBuilder.destroy(metaServiceRegistry);
}
}
}
use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.
the class AnnotationBinder method defineFetchingStrategy.
protected static void defineFetchingStrategy(ToOne toOne, XProperty property) {
LazyToOne lazy = property.getAnnotation(LazyToOne.class);
Fetch fetch = property.getAnnotation(Fetch.class);
ManyToOne manyToOne = property.getAnnotation(ManyToOne.class);
OneToOne oneToOne = property.getAnnotation(OneToOne.class);
FetchType fetchType;
if (manyToOne != null) {
fetchType = manyToOne.fetch();
} else if (oneToOne != null) {
fetchType = oneToOne.fetch();
} else {
throw new AssertionFailure("Define fetch strategy on a property not annotated with @OneToMany nor @OneToOne");
}
if (lazy != null) {
toOne.setLazy(!(lazy.value() == LazyToOneOption.FALSE));
toOne.setUnwrapProxy((lazy.value() == LazyToOneOption.NO_PROXY));
} else {
toOne.setLazy(fetchType == FetchType.LAZY);
toOne.setUnwrapProxy(false);
}
if (fetch != null) {
if (fetch.value() == org.hibernate.annotations.FetchMode.JOIN) {
toOne.setFetchMode(FetchMode.JOIN);
toOne.setLazy(false);
toOne.setUnwrapProxy(false);
} else if (fetch.value() == org.hibernate.annotations.FetchMode.SELECT) {
toOne.setFetchMode(FetchMode.SELECT);
} else if (fetch.value() == org.hibernate.annotations.FetchMode.SUBSELECT) {
throw new AnnotationException("Use of FetchMode.SUBSELECT not allowed on ToOne associations");
} else {
throw new AssertionFailure("Unknown FetchMode: " + fetch.value());
}
} else {
toOne.setFetchMode(getFetchMode(fetchType));
}
}
Aggregations