Search in sources :

Example 46 with AnnotationException

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 + ")");
        }
    }
}
Also used : AnnotationException(org.hibernate.AnnotationException) QueryHint(javax.persistence.QueryHint) UniqueConstraint(javax.persistence.UniqueConstraint)

Example 47 with AnnotationException

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;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) NamedNativeQuery(javax.persistence.NamedNativeQuery) 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) ArrayList(java.util.ArrayList) List(java.util.List) AccessibleObject(java.lang.reflect.AccessibleObject) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass) NamedQuery(javax.persistence.NamedQuery)

Example 48 with AnnotationException

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;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) IdClass(javax.persistence.IdClass) Attribute(org.dom4j.Attribute) 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 49 with AnnotationException

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);
        }
    }
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) AnnotationException(org.hibernate.AnnotationException) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) Test(org.junit.Test)

Example 50 with AnnotationException

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));
    }
}
Also used : Fetch(org.hibernate.annotations.Fetch) OneToOne(javax.persistence.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) FetchType(javax.persistence.FetchType) AnnotationException(org.hibernate.AnnotationException) ManyToOne(javax.persistence.ManyToOne) LazyToOne(org.hibernate.annotations.LazyToOne)

Aggregations

AnnotationException (org.hibernate.AnnotationException)85 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)15 PersistentClass (org.hibernate.mapping.PersistentClass)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)12 Element (org.dom4j.Element)12 XClass (org.hibernate.annotations.common.reflection.XClass)12 HashMap (java.util.HashMap)11 MappingException (org.hibernate.MappingException)11 XProperty (org.hibernate.annotations.common.reflection.XProperty)11 Property (org.hibernate.mapping.Property)11 SimpleValue (org.hibernate.mapping.SimpleValue)11 Test (org.junit.Test)10 AssertionFailure (org.hibernate.AssertionFailure)9 ArrayList (java.util.ArrayList)8 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)8 Column (org.hibernate.mapping.Column)8 IdClass (javax.persistence.IdClass)7 MapKeyClass (javax.persistence.MapKeyClass)7 Component (org.hibernate.mapping.Component)7 Properties (java.util.Properties)6