Search in sources :

Example 1 with JaxbConvert

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

the class JPAXMLOverriddenAnnotationReader method applyXmlDefinedConverts.

private void applyXmlDefinedConverts(List<JaxbConvert> elements, XMLContext.Default defaults, String attributeNamePrefix, Map<String, Convert> convertAnnotationsMap) {
    for (JaxbConvert convertElement : elements) {
        final AnnotationDescriptor convertAnnotationDescriptor = new AnnotationDescriptor(Convert.class);
        copyAttribute(convertAnnotationDescriptor, "attribute-name", convertElement.getAttributeName(), false);
        copyAttribute(convertAnnotationDescriptor, "disable-conversion", convertElement.isDisableConversion(), false);
        final String converter = convertElement.getConverter();
        if (converter != null) {
            final String converterClassName = XMLContext.buildSafeClassName(converter, defaults);
            try {
                final Class converterClass = classLoaderAccess.classForName(converterClassName);
                convertAnnotationDescriptor.setValue("converter", converterClass);
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find specified converter class id-class: " + converterClassName, e);
            }
        }
        final Convert convertAnnotation = AnnotationFactory.create(convertAnnotationDescriptor);
        final String qualifiedAttributeName = qualifyConverterAttributeName(attributeNamePrefix, convertAnnotation.attributeName());
        convertAnnotationsMap.put(qualifiedAttributeName, convertAnnotation);
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) Convert(jakarta.persistence.Convert) JaxbConvert(org.hibernate.boot.jaxb.mapping.spi.JaxbConvert) JaxbConvert(org.hibernate.boot.jaxb.mapping.spi.JaxbConvert) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) 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)

Example 2 with JaxbConvert

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

the class JPAXMLOverriddenAnnotationReader method getConvertsForAttribute.

private Annotation getConvertsForAttribute(PropertyMappingElementCollector elementsForProperty, XMLContext.Default defaults) {
    // NOTE : we use a map here to make sure that an xml and annotation referring to the same attribute
    // properly overrides.  Very sparse map, yes, but easy setup.
    // todo : revisit this
    // although bear in mind that this code is no longer used in 5.0...
    final Map<String, Convert> convertAnnotationsMap = new HashMap<>();
    for (JaxbBasic element : elementsForProperty.getBasic()) {
        JaxbConvert convert = element.getConvert();
        if (convert != null) {
            applyXmlDefinedConverts(Collections.singletonList(convert), defaults, null, convertAnnotationsMap);
        }
    }
    for (JaxbEmbedded element : elementsForProperty.getEmbedded()) {
        applyXmlDefinedConverts(element.getConvert(), defaults, propertyName, convertAnnotationsMap);
    }
    for (JaxbElementCollection element : elementsForProperty.getElementCollection()) {
        applyXmlDefinedConverts(element.getConvert(), defaults, propertyName, convertAnnotationsMap);
    }
    if (defaults.canUseJavaAnnotations()) {
        // todo : note sure how to best handle attributeNamePrefix here
        applyPhysicalConvertAnnotations(propertyName, convertAnnotationsMap);
    }
    if (!convertAnnotationsMap.isEmpty()) {
        final AnnotationDescriptor groupingDescriptor = new AnnotationDescriptor(Converts.class);
        groupingDescriptor.setValue("value", convertAnnotationsMap.values().toArray(new Convert[convertAnnotationsMap.size()]));
        return AnnotationFactory.create(groupingDescriptor);
    }
    return null;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) Convert(jakarta.persistence.Convert) JaxbConvert(org.hibernate.boot.jaxb.mapping.spi.JaxbConvert) HashMap(java.util.HashMap) JaxbConvert(org.hibernate.boot.jaxb.mapping.spi.JaxbConvert) JaxbEmbedded(org.hibernate.boot.jaxb.mapping.spi.JaxbEmbedded) JaxbElementCollection(org.hibernate.boot.jaxb.mapping.spi.JaxbElementCollection) JaxbBasic(org.hibernate.boot.jaxb.mapping.spi.JaxbBasic)

Aggregations

Convert (jakarta.persistence.Convert)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 JaxbConvert (org.hibernate.boot.jaxb.mapping.spi.JaxbConvert)2 IdClass (jakarta.persistence.IdClass)1 MapKeyClass (jakarta.persistence.MapKeyClass)1 HashMap (java.util.HashMap)1 AnnotationException (org.hibernate.AnnotationException)1 JaxbBasic (org.hibernate.boot.jaxb.mapping.spi.JaxbBasic)1 JaxbElementCollection (org.hibernate.boot.jaxb.mapping.spi.JaxbElementCollection)1 JaxbEmbedded (org.hibernate.boot.jaxb.mapping.spi.JaxbEmbedded)1 JaxbIdClass (org.hibernate.boot.jaxb.mapping.spi.JaxbIdClass)1 JaxbMapKeyClass (org.hibernate.boot.jaxb.mapping.spi.JaxbMapKeyClass)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1