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;
}
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;
}
}
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);
}
}
}
}
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) {
}
}
}
Aggregations