Search in sources :

Example 1 with ManagedType

use of org.hibernate.boot.jaxb.mapping.spi.ManagedType in project hibernate-orm by hibernate.

the class JPAXMLOverriddenAnnotationReader method initAnnotations.

/*
	 * The idea is to create annotation proxies for the xml configuration elements. Using this proxy annotations together
	 * with the {@link JPAMetadataProvider} allows to handle xml configuration the same way as annotation configuration.
	 */
private void initAnnotations() {
    if (annotations == null) {
        // We don't want the global catalog and schema here: they are applied much later,
        // when SQL gets rendered.
        XMLContext.Default defaults = xmlContext.getDefaultWithoutGlobalCatalogAndSchema(className);
        if (className != null && propertyName == null) {
            // is a class
            ManagedType managedTypeOverride = xmlContext.getManagedTypeOverride(className);
            Annotation[] annotations = getPhysicalAnnotations();
            List<Annotation> annotationList = new ArrayList<>(annotations.length + 5);
            annotationsMap = new HashMap<>(annotations.length + 5);
            for (Annotation annotation : annotations) {
                if (!annotationToXml.containsKey(annotation.annotationType())) {
                    // unknown annotations are left over
                    annotationList.add(annotation);
                }
            }
            addIfNotNull(annotationList, getEntity(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getMappedSuperclass(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getEmbeddable(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getTable(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getSecondaryTables(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getPrimaryKeyJoinColumns(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getIdClass(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getCacheable(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getInheritance(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getDiscriminatorValue(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getDiscriminatorColumn(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getSequenceGenerator(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getTableGenerator(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getNamedQueries(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getNamedNativeQueries(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getNamedStoredProcedureQueries(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getNamedEntityGraphs(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getSqlResultSetMappings(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getExcludeDefaultListeners(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getExcludeSuperclassListeners(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getAccessType(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getAttributeOverrides(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getAssociationOverrides(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getEntityListeners(managedTypeOverride, defaults));
            addIfNotNull(annotationList, getConverts(managedTypeOverride, defaults));
            this.annotations = annotationList.toArray(new Annotation[annotationList.size()]);
            for (Annotation ann : this.annotations) {
                annotationsMap.put(ann.annotationType(), ann);
            }
            checkForOrphanProperties(managedTypeOverride);
        } else if (className != null) {
            // && propertyName != null ) { //always true but less confusing
            ManagedType managedTypeOverride = xmlContext.getManagedTypeOverride(className);
            JaxbEntityListener entityListenerOverride = xmlContext.getEntityListenerOverride(className);
            Annotation[] annotations = getPhysicalAnnotations();
            List<Annotation> annotationList = new ArrayList<>(annotations.length + 5);
            annotationsMap = new HashMap<>(annotations.length + 5);
            for (Annotation annotation : annotations) {
                if (!annotationToXml.containsKey(annotation.annotationType())) {
                    // unknown annotations are left over
                    annotationList.add(annotation);
                }
            }
            preCalculateElementsForProperty(managedTypeOverride, entityListenerOverride);
            Transient transientAnn = getTransient(defaults);
            if (transientAnn != null) {
                annotationList.add(transientAnn);
            } else {
                if (defaults.canUseJavaAnnotations()) {
                    Annotation annotation = getPhysicalAnnotation(Access.class);
                    addIfNotNull(annotationList, annotation);
                }
                getId(annotationList, defaults);
                getEmbeddedId(annotationList, defaults);
                getEmbedded(annotationList, defaults);
                getBasic(annotationList, defaults);
                getVersion(annotationList, defaults);
                getManyToOne(annotationList, defaults);
                getOneToOne(annotationList, defaults);
                getOneToMany(annotationList, defaults);
                getManyToMany(annotationList, defaults);
                getAny(annotationList, defaults);
                getManyToAny(annotationList, defaults);
                getElementCollection(annotationList, defaults);
                addIfNotNull(annotationList, getSequenceGenerator(elementsForProperty, defaults));
                addIfNotNull(annotationList, getTableGenerator(elementsForProperty, defaults));
                addIfNotNull(annotationList, getConvertsForAttribute(elementsForProperty, defaults));
            }
            processEventAnnotations(annotationList, defaults);
            // FIXME use annotationsMap rather than annotationList this will be faster since the annotation type is usually known at put() time
            this.annotations = annotationList.toArray(new Annotation[annotationList.size()]);
            for (Annotation ann : this.annotations) {
                annotationsMap.put(ann.annotationType(), ann);
            }
        } else {
            this.annotations = getPhysicalAnnotations();
            annotationsMap = new HashMap<>(annotations.length + 5);
            for (Annotation ann : this.annotations) {
                annotationsMap.put(ann.annotationType(), ann);
            }
        }
    }
}
Also used : ManagedType(org.hibernate.boot.jaxb.mapping.spi.ManagedType) HashMap(java.util.HashMap) JaxbEntityListener(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener) ArrayList(java.util.ArrayList) ClassLoaderAccess(org.hibernate.boot.spi.ClassLoaderAccess) Access(jakarta.persistence.Access) Annotation(java.lang.annotation.Annotation) ArrayList(java.util.ArrayList) List(java.util.List) JaxbTransient(org.hibernate.boot.jaxb.mapping.spi.JaxbTransient) Transient(jakarta.persistence.Transient)

Example 2 with ManagedType

use of org.hibernate.boot.jaxb.mapping.spi.ManagedType in project hibernate-orm by hibernate.

the class XMLContext method addClass.

private void addClass(List<? extends ManagedType> managedTypes, String packageName, Default defaults, List<String> addedClasses) {
    for (ManagedType element : managedTypes) {
        String className = buildSafeClassName(element.getClazz(), packageName);
        if (managedTypeOverride.containsKey(className)) {
            // maybe switch it to warn?
            throw new IllegalStateException("Duplicate XML entry for " + className);
        }
        addedClasses.add(className);
        managedTypeOverride.put(className, element);
        Default mergedDefaults = new Default();
        // Apply entity mapping defaults
        mergedDefaults.overrideWithCatalogAndSchema(defaults);
        // ... then apply entity settings
        Default fileDefaults = new Default();
        fileDefaults.setMetadataComplete(element.isMetadataComplete());
        fileDefaults.setAccess(element.getAccess());
        mergedDefaults.overrideWithCatalogAndSchema(fileDefaults);
        // ... and we get the merged defaults for that entity
        defaultsOverride.put(className, mergedDefaults);
        LOG.debugf("Adding XML overriding information for %s", className);
        if (element instanceof JaxbEntity) {
            addEntityListenerClasses(((JaxbEntity) element).getEntityListeners(), packageName, addedClasses);
        } else if (element instanceof JaxbMappedSuperclass) {
            addEntityListenerClasses(((JaxbMappedSuperclass) element).getEntityListeners(), packageName, addedClasses);
        }
    }
}
Also used : ManagedType(org.hibernate.boot.jaxb.mapping.spi.ManagedType) JaxbMappedSuperclass(org.hibernate.boot.jaxb.mapping.spi.JaxbMappedSuperclass) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)

Aggregations

ManagedType (org.hibernate.boot.jaxb.mapping.spi.ManagedType)2 Access (jakarta.persistence.Access)1 Transient (jakarta.persistence.Transient)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 JaxbEntity (org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)1 JaxbEntityListener (org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener)1 JaxbMappedSuperclass (org.hibernate.boot.jaxb.mapping.spi.JaxbMappedSuperclass)1 JaxbTransient (org.hibernate.boot.jaxb.mapping.spi.JaxbTransient)1 ClassLoaderAccess (org.hibernate.boot.spi.ClassLoaderAccess)1