Search in sources :

Example 41 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class JpaXsdVersionsTest method testInvalidOrm1.

@Test
public void testInvalidOrm1() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("invalid-orm1-test", "1.0").addMappingFileName("org/hibernate/test/jpa/xml/versions/invalid-orm-1_0.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = null;
    try {
        emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
        Assert.fail("expecting 'invalid content' error");
    } catch (InvalidMappingException | AnnotationException expected) {
    // expected condition
    } catch (PersistenceException expected) {
    // expected condition
    } finally {
        if (emf != null) {
            emf.close();
        }
    }
}
Also used : InvalidMappingException(org.hibernate.InvalidMappingException) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) AnnotationException(org.hibernate.AnnotationException) Test(org.junit.Test)

Example 42 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getDiscriminatorColumn.

private DiscriminatorColumn getDiscriminatorColumn(Element tree, XMLContext.Default defaults) {
    Element element = tree != null ? tree.element("discriminator-column") : null;
    if (element != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(DiscriminatorColumn.class);
        copyStringAttribute(ad, element, "name", false);
        copyStringAttribute(ad, element, "column-definition", false);
        String value = element.attributeValue("discriminator-type");
        DiscriminatorType type = DiscriminatorType.STRING;
        if (value != null) {
            if ("STRING".equals(value)) {
                type = DiscriminatorType.STRING;
            } else if ("CHAR".equals(value)) {
                type = DiscriminatorType.CHAR;
            } else if ("INTEGER".equals(value)) {
                type = DiscriminatorType.INTEGER;
            } else {
                throw new AnnotationException("Unknown DiscrimiatorType in XML: " + value + " (" + SCHEMA_VALIDATION + ")");
            }
        }
        ad.setValue("discriminatorType", type);
        copyIntegerAttribute(ad, element, "length");
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(DiscriminatorColumn.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) DiscriminatorType(javax.persistence.DiscriminatorType) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) AnnotationException(org.hibernate.AnnotationException)

Example 43 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class InheritanceState method getElementsToProcess.

/*
     * Get the annotated elements and determine access type from hierarchy, guessing from @Id or @EmbeddedId presence if not
     * specified.
     * Change EntityBinder by side effect
     */
public ElementsToProcess getElementsToProcess() {
    if (elementsToProcess == null) {
        InheritanceState inheritanceState = inheritanceStatePerClass.get(clazz);
        assert !inheritanceState.isEmbeddableSuperclass();
        getMappedSuperclassesTillNextEntityOrdered();
        accessType = determineDefaultAccessType();
        ArrayList<PropertyData> elements = new ArrayList<PropertyData>();
        int idPropertyCount = 0;
        for (XClass classToProcessForMappedSuperclass : classesToProcessForMappedSuperclass) {
            PropertyContainer propertyContainer = new PropertyContainer(classToProcessForMappedSuperclass, clazz, accessType);
            int currentIdPropertyCount = AnnotationBinder.addElementsOfClass(elements, propertyContainer, buildingContext);
            idPropertyCount += currentIdPropertyCount;
        }
        if (idPropertyCount == 0 && !inheritanceState.hasParents()) {
            throw new AnnotationException("No identifier specified for entity: " + clazz.getName());
        }
        elements.trimToSize();
        elementsToProcess = new ElementsToProcess(elements, idPropertyCount);
    }
    return elementsToProcess;
}
Also used : ArrayList(java.util.ArrayList) AnnotationException(org.hibernate.AnnotationException) XClass(org.hibernate.annotations.common.reflection.XClass)

Example 44 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getEnumerated.

private void getEnumerated(List<Annotation> annotationList, Element element) {
    Element subElement = element != null ? element.element("enumerated") : null;
    if (subElement != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(Enumerated.class);
        String enumerated = subElement.getTextTrim();
        if ("ORDINAL".equalsIgnoreCase(enumerated)) {
            ad.setValue("value", EnumType.ORDINAL);
        } else if ("STRING".equalsIgnoreCase(enumerated)) {
            ad.setValue("value", EnumType.STRING);
        } else if (StringHelper.isNotEmpty(enumerated)) {
            throw new AnnotationException("Unknown EnumType: " + enumerated + ". " + SCHEMA_VALIDATION);
        }
        annotationList.add(AnnotationFactory.create(ad));
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) AnnotationException(org.hibernate.AnnotationException)

Example 45 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getAccessType.

private void getAccessType(List<Annotation> annotationList, Element element) {
    if (element == null) {
        return;
    }
    String access = element.attributeValue("access");
    if (access != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(Access.class);
        AccessType type;
        try {
            type = AccessType.valueOf(access);
        } catch (IllegalArgumentException e) {
            throw new AnnotationException(access + " is not a valid access type. Check you xml confguration.");
        }
        if ((AccessType.PROPERTY.equals(type) && this.element instanceof Method) || (AccessType.FIELD.equals(type) && this.element instanceof Field)) {
            return;
        }
        ad.setValue("value", type);
        annotationList.add(AnnotationFactory.create(ad));
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) Field(java.lang.reflect.Field) AnnotationException(org.hibernate.AnnotationException) Method(java.lang.reflect.Method) AccessType(javax.persistence.AccessType)

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