Search in sources :

Example 1 with JaxbEntityListener

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

the class XMLContext method addEntityListenerClasses.

private List<String> addEntityListenerClasses(JaxbEntityListeners listeners, String packageName, List<String> addedClasses) {
    List<String> localAddedClasses = new ArrayList<>();
    if (listeners != null) {
        List<JaxbEntityListener> elements = listeners.getEntityListener();
        for (JaxbEntityListener listener : elements) {
            String listenerClassName = buildSafeClassName(listener.getClazz(), packageName);
            if (entityListenerOverride.containsKey(listenerClassName)) {
                LOG.duplicateListener(listenerClassName);
                continue;
            }
            localAddedClasses.add(listenerClassName);
            entityListenerOverride.put(listenerClassName, listener);
        }
    }
    LOG.debugf("Adding XML overriding information for listeners: %s", localAddedClasses);
    addedClasses.addAll(localAddedClasses);
    return localAddedClasses;
}
Also used : JaxbEntityListener(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener) ArrayList(java.util.ArrayList)

Example 2 with JaxbEntityListener

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

the class JPAXMLOverriddenAnnotationReader method getEntityListeners.

private EntityListeners getEntityListeners(ManagedType root, XMLContext.Default defaults) {
    JaxbEntityListeners element = root instanceof EntityOrMappedSuperclass ? ((EntityOrMappedSuperclass) root).getEntityListeners() : null;
    if (element != null) {
        List<Class> entityListenerClasses = new ArrayList<>();
        for (JaxbEntityListener subelement : element.getEntityListener()) {
            String className = subelement.getClazz();
            try {
                entityListenerClasses.add(classLoaderAccess.classForName(XMLContext.buildSafeClassName(className, defaults)));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find class: " + className, e);
            }
        }
        AnnotationDescriptor ad = new AnnotationDescriptor(EntityListeners.class);
        ad.setValue("value", entityListenerClasses.toArray(new Class[entityListenerClasses.size()]));
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(EntityListeners.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbEntityListeners(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListeners) JaxbEntityListener(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) ArrayList(java.util.ArrayList) EntityOrMappedSuperclass(org.hibernate.boot.jaxb.mapping.spi.EntityOrMappedSuperclass) AnnotationException(org.hibernate.AnnotationException) IdClass(jakarta.persistence.IdClass) MapKeyClass(jakarta.persistence.MapKeyClass) JaxbIdClass(org.hibernate.boot.jaxb.mapping.spi.JaxbIdClass) JaxbMapKeyClass(org.hibernate.boot.jaxb.mapping.spi.JaxbMapKeyClass) EntityListeners(jakarta.persistence.EntityListeners) JaxbEntityListeners(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListeners)

Example 3 with JaxbEntityListener

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener 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 4 with JaxbEntityListener

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

the class JakartaXmlSmokeTests method testLoadingOrmXml.

@Test
public void testLoadingOrmXml(ServiceRegistryScope scope) {
    final ClassLoaderService cls = scope.getRegistry().getService(ClassLoaderService.class);
    final MappingBinder mappingBinder = new MappingBinder(cls, true);
    final InputStream inputStream = cls.locateResourceStream("xml/jakarta/simple/orm.xml");
    try {
        final Binding<JaxbEntityMappings> binding = mappingBinder.bind(new StreamSource(inputStream), new Origin(SourceType.RESOURCE, "xml/jakarta/simple/orm.xml"));
        assertThat(binding.getRoot().getEntity().stream().map(JaxbEntity::getClazz)).containsOnly("Lighter", "ApplicationServer");
        final JaxbPersistenceUnitMetadata puMetadata = binding.getRoot().getPersistenceUnitMetadata();
        final JaxbPersistenceUnitDefaults puDefaults = puMetadata.getPersistenceUnitDefaults();
        final Stream<String> listenerNames = puDefaults.getEntityListeners().getEntityListener().stream().map(JaxbEntityListener::getClazz);
        assertThat(listenerNames).containsOnly("org.hibernate.jpa.test.pack.defaultpar.IncrementListener");
    } finally {
        try {
            inputStream.close();
        } catch (IOException ignore) {
        }
    }
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) InputStream(java.io.InputStream) JaxbEntityListener(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) MappingBinder(org.hibernate.boot.jaxb.internal.MappingBinder) JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings) JaxbPersistenceUnitDefaults(org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitDefaults) JaxbPersistenceUnitMetadata(org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitMetadata) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity) Test(org.junit.jupiter.api.Test)

Aggregations

JaxbEntityListener (org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener)4 ArrayList (java.util.ArrayList)3 Access (jakarta.persistence.Access)1 EntityListeners (jakarta.persistence.EntityListeners)1 IdClass (jakarta.persistence.IdClass)1 MapKeyClass (jakarta.persistence.MapKeyClass)1 Transient (jakarta.persistence.Transient)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 List (java.util.List)1 StreamSource (javax.xml.transform.stream.StreamSource)1 AnnotationException (org.hibernate.AnnotationException)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 Origin (org.hibernate.boot.jaxb.Origin)1 MappingBinder (org.hibernate.boot.jaxb.internal.MappingBinder)1 EntityOrMappedSuperclass (org.hibernate.boot.jaxb.mapping.spi.EntityOrMappedSuperclass)1 JaxbEntity (org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)1 JaxbEntityListeners (org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListeners)1