Search in sources :

Example 21 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getAssociationId.

/**
 * Adds an @Id annotation to the specified annotationList if the specified element has the id
 * attribute set to true. This should only be the case for many-to-one or one-to-one
 * associations.
 */
private void getAssociationId(List<Annotation> annotationList, Element element) {
    String attrVal = element.attributeValue("id");
    if ("true".equals(attrVal)) {
        AnnotationDescriptor ad = new AnnotationDescriptor(Id.class);
        annotationList.add(AnnotationFactory.create(ad));
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)

Example 22 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getAssociation.

/**
 * As per section 12.2 of the JPA 2.0 specification, the association
 * subelements (many-to-one, one-to-many, one-to-one, many-to-many,
 * element-collection) completely override the mapping for the specified
 * field or property.  Thus, any methods which might in some contexts merge
 * with annotations must not do so in this context.
 *
 * @see #getElementCollection(List, org.hibernate.cfg.annotations.reflection.XMLContext.Default)
 */
private void getAssociation(Class<? extends Annotation> annotationType, List<Annotation> annotationList, XMLContext.Default defaults) {
    String xmlName = annotationToXml.get(annotationType);
    for (Element element : elementsForProperty) {
        if (xmlName.equals(element.getName())) {
            AnnotationDescriptor ad = new AnnotationDescriptor(annotationType);
            addTargetClass(element, ad, "target-entity", defaults);
            getFetchType(ad, element);
            getCascades(ad, element, defaults);
            getJoinTable(annotationList, element, defaults);
            buildJoinColumns(annotationList, element);
            Annotation annotation = getPrimaryKeyJoinColumns(element, defaults, false);
            addIfNotNull(annotationList, annotation);
            copyBooleanAttribute(ad, element, "optional");
            copyBooleanAttribute(ad, element, "orphan-removal");
            copyStringAttribute(ad, element, "mapped-by", false);
            getOrderBy(annotationList, element);
            getMapKey(annotationList, element);
            getMapKeyClass(annotationList, element, defaults);
            getMapKeyColumn(annotationList, element);
            getOrderColumn(annotationList, element);
            getMapKeyTemporal(annotationList, element);
            getMapKeyEnumerated(annotationList, element);
            annotation = getMapKeyAttributeOverrides(element, defaults);
            addIfNotNull(annotationList, annotation);
            buildMapKeyJoinColumns(annotationList, element);
            getAssociationId(annotationList, element);
            getMapsId(annotationList, element);
            annotationList.add(AnnotationFactory.create(ad));
            getAccessType(annotationList, element);
        }
    }
    if (elementsForProperty.size() == 0 && defaults.canUseJavaAnnotations()) {
        Annotation annotation = getPhysicalAnnotation(annotationType);
        if (annotation != null) {
            annotation = overridesDefaultCascadePersist(annotation, defaults);
            annotationList.add(annotation);
            annotation = overridesDefaultsInJoinTable(annotation, defaults);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(JoinColumn.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(JoinColumns.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(PrimaryKeyJoinColumn.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(PrimaryKeyJoinColumns.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKey.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(OrderBy.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(AttributeOverride.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(AttributeOverrides.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(AssociationOverride.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(AssociationOverrides.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Lob.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Enumerated.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Temporal.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Column.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Columns.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyClass.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyTemporal.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyEnumerated.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyColumn.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyJoinColumn.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyJoinColumns.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(OrderColumn.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Cascade.class);
            addIfNotNull(annotationList, annotation);
        } else if (isPhysicalAnnotationPresent(ElementCollection.class)) {
            // JPA2
            annotation = overridesDefaultsInJoinTable(getPhysicalAnnotation(ElementCollection.class), defaults);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKey.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(OrderBy.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(AttributeOverride.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(AttributeOverrides.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(AssociationOverride.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(AssociationOverrides.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Lob.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Enumerated.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Temporal.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(Column.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(OrderColumn.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyClass.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyTemporal.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyEnumerated.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyColumn.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyJoinColumn.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(MapKeyJoinColumns.class);
            addIfNotNull(annotationList, annotation);
            annotation = getPhysicalAnnotation(CollectionTable.class);
            addIfNotNull(annotationList, annotation);
        }
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AssociationOverride(javax.persistence.AssociationOverride) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) MapKeyClass(javax.persistence.MapKeyClass) AttributeOverride(javax.persistence.AttributeOverride) Enumerated(javax.persistence.Enumerated) MapKeyEnumerated(javax.persistence.MapKeyEnumerated) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) MapKeyTemporal(javax.persistence.MapKeyTemporal) Temporal(javax.persistence.Temporal) MapKeyColumn(javax.persistence.MapKeyColumn) OrderColumn(javax.persistence.OrderColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(javax.persistence.Column) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) JoinColumn(javax.persistence.JoinColumn) ElementCollection(javax.persistence.ElementCollection) Lob(javax.persistence.Lob) Cascade(org.hibernate.annotations.Cascade) OrderBy(javax.persistence.OrderBy) OrderColumn(javax.persistence.OrderColumn) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) Columns(org.hibernate.annotations.Columns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) MapKeyColumn(javax.persistence.MapKeyColumn) MapKeyEnumerated(javax.persistence.MapKeyEnumerated) Annotation(java.lang.annotation.Annotation) AttributeOverrides(javax.persistence.AttributeOverrides) AssociationOverrides(javax.persistence.AssociationOverrides) MapKey(javax.persistence.MapKey) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) MapKeyTemporal(javax.persistence.MapKeyTemporal)

Example 23 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method buildConstructorResult.

private static ConstructorResult buildConstructorResult(Element constructorResultElement, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
    AnnotationDescriptor constructorResultDescriptor = new AnnotationDescriptor(ConstructorResult.class);
    final Class entityClass = resolveClassReference(constructorResultElement.attributeValue("target-class"), defaults, classLoaderAccess);
    constructorResultDescriptor.setValue("targetClass", entityClass);
    List<ColumnResult> columnResultAnnotations = new ArrayList<>();
    for (Element columnResultElement : (List<Element>) constructorResultElement.elements("column")) {
        columnResultAnnotations.add(buildColumnResult(columnResultElement, defaults, classLoaderAccess));
    }
    constructorResultDescriptor.setValue("columns", columnResultAnnotations.toArray(new ColumnResult[columnResultAnnotations.size()]));
    return AnnotationFactory.create(constructorResultDescriptor);
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) ColumnResult(javax.persistence.ColumnResult) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass) ArrayList(java.util.ArrayList) List(java.util.List)

Example 24 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getTable.

private Table getTable(Element tree, XMLContext.Default defaults) {
    Element subelement = tree == null ? null : tree.element("table");
    if (subelement == null) {
        // no element but might have some default or some annotation
        if (StringHelper.isNotEmpty(defaults.getCatalog()) || StringHelper.isNotEmpty(defaults.getSchema())) {
            AnnotationDescriptor annotation = new AnnotationDescriptor(Table.class);
            if (defaults.canUseJavaAnnotations()) {
                Table table = getPhysicalAnnotation(Table.class);
                if (table != null) {
                    annotation.setValue("name", table.name());
                    annotation.setValue("schema", table.schema());
                    annotation.setValue("catalog", table.catalog());
                    annotation.setValue("uniqueConstraints", table.uniqueConstraints());
                    annotation.setValue("indexes", table.indexes());
                }
            }
            if (StringHelper.isEmpty((String) annotation.valueOf("schema")) && StringHelper.isNotEmpty(defaults.getSchema())) {
                annotation.setValue("schema", defaults.getSchema());
            }
            if (StringHelper.isEmpty((String) annotation.valueOf("catalog")) && StringHelper.isNotEmpty(defaults.getCatalog())) {
                annotation.setValue("catalog", defaults.getCatalog());
            }
            return AnnotationFactory.create(annotation);
        } else if (defaults.canUseJavaAnnotations()) {
            return getPhysicalAnnotation(Table.class);
        } else {
            return null;
        }
    } else {
        // ignore java annotation, an element is defined
        AnnotationDescriptor annotation = new AnnotationDescriptor(Table.class);
        copyStringAttribute(annotation, subelement, "name", false);
        copyStringAttribute(annotation, subelement, "catalog", false);
        if (StringHelper.isNotEmpty(defaults.getCatalog()) && StringHelper.isEmpty((String) annotation.valueOf("catalog"))) {
            annotation.setValue("catalog", defaults.getCatalog());
        }
        copyStringAttribute(annotation, subelement, "schema", false);
        if (StringHelper.isNotEmpty(defaults.getSchema()) && StringHelper.isEmpty((String) annotation.valueOf("schema"))) {
            annotation.setValue("schema", defaults.getSchema());
        }
        buildUniqueConstraints(annotation, subelement);
        buildIndex(annotation, subelement);
        return AnnotationFactory.create(annotation);
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) Table(javax.persistence.Table) CollectionTable(javax.persistence.CollectionTable) SecondaryTable(javax.persistence.SecondaryTable) JoinTable(javax.persistence.JoinTable) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element)

Example 25 with AnnotationDescriptor

use of org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method buildAttributeOverrides.

private List<AttributeOverride> buildAttributeOverrides(List<Element> subelements, String nodeName) {
    List<AttributeOverride> overrides = new ArrayList<>();
    if (subelements != null && subelements.size() > 0) {
        for (Element current : subelements) {
            if (!current.getName().equals(nodeName)) {
                continue;
            }
            AnnotationDescriptor override = new AnnotationDescriptor(AttributeOverride.class);
            copyStringAttribute(override, current, "name", true);
            Element column = current.element("column");
            override.setValue("column", getColumn(column, true, current));
            overrides.add(AnnotationFactory.create(override));
        }
    }
    return overrides;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) AttributeOverride(javax.persistence.AttributeOverride)

Aggregations

AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)70 AnnotatedElement (java.lang.reflect.AnnotatedElement)46 Element (org.dom4j.Element)46 ArrayList (java.util.ArrayList)23 AnnotationException (org.hibernate.AnnotationException)15 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)13 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)13 List (java.util.List)11 JoinColumn (javax.persistence.JoinColumn)11 MapKeyClass (javax.persistence.MapKeyClass)10 IdClass (javax.persistence.IdClass)9 Annotation (java.lang.annotation.Annotation)8 AttributeOverride (javax.persistence.AttributeOverride)8 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)8 AssociationOverride (javax.persistence.AssociationOverride)7 Column (javax.persistence.Column)7 MapKeyColumn (javax.persistence.MapKeyColumn)7 OrderColumn (javax.persistence.OrderColumn)7 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)7 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)7