Search in sources :

Example 1 with Converts

use of jakarta.persistence.Converts in project hibernate-orm by hibernate.

the class ComponentPropertyHolder method processAttributeConversions.

/**
 * This is called from our constructor and handles (in order):<ol>
 *     <li>@Convert annotation at the Embeddable class level</li>
 *     <li>@Converts annotation at the Embeddable class level</li>
 *     <li>@Convert annotation at the Embedded attribute level</li>
 *     <li>@Converts annotation at the Embedded attribute level</li>
 * </ol>
 * <p/>
 * The order is important to ensure proper precedence.
 * <p/>
 * {@literal @Convert/@Converts} annotations at the Embeddable attribute level are handled in the calls to
 * {@link #startingProperty}.  Duplicates are simply ignored there.
 *
 * @param embeddedXProperty The property that is the composite being described by this ComponentPropertyHolder
 */
private Map<String, AttributeConversionInfo> processAttributeConversions(XProperty embeddedXProperty) {
    final Map<String, AttributeConversionInfo> infoMap = new HashMap<>();
    final XClass embeddableXClass = embeddedXProperty.getType();
    // as a baseline, we want to apply conversions from the Embeddable and then overlay conversions
    // from the Embedded
    // first apply conversions from the Embeddable...
    processAttributeConversions(embeddableXClass, infoMap);
    // then we can overlay any conversions from the Embedded attribute
    {
        // @Convert annotation on the Embedded attribute
        final Convert convertAnnotation = embeddedXProperty.getAnnotation(Convert.class);
        if (convertAnnotation != null) {
            final AttributeConversionInfo info = new AttributeConversionInfo(convertAnnotation, embeddableXClass);
            if (StringHelper.isEmpty(info.getAttributeName())) {
                throw new IllegalStateException("Convert placed on Embedded attribute must define (sub)attributeName");
            }
            infoMap.put(info.getAttributeName(), info);
        }
    }
    {
        // @Converts annotation on the Embedded attribute
        final Converts convertsAnnotation = embeddedXProperty.getAnnotation(Converts.class);
        if (convertsAnnotation != null) {
            for (Convert convertAnnotation : convertsAnnotation.value()) {
                final AttributeConversionInfo info = new AttributeConversionInfo(convertAnnotation, embeddableXClass);
                if (StringHelper.isEmpty(info.getAttributeName())) {
                    throw new IllegalStateException("Convert placed on Embedded attribute must define (sub)attributeName");
                }
                infoMap.put(info.getAttributeName(), info);
            }
        }
    }
    return infoMap;
}
Also used : Convert(jakarta.persistence.Convert) HashMap(java.util.HashMap) Converts(jakarta.persistence.Converts) XClass(org.hibernate.annotations.common.reflection.XClass)

Example 2 with Converts

use of jakarta.persistence.Converts in project hibernate-orm by hibernate.

the class JPAXMLOverriddenAnnotationReader method applyPhysicalConvertAnnotations.

private void applyPhysicalConvertAnnotations(String attributeNamePrefix, Map<String, Convert> convertAnnotationsMap) {
    final Convert physicalAnnotation = getPhysicalAnnotation(Convert.class);
    if (physicalAnnotation != null) {
        // only add if no XML element named a converter for this attribute
        final String qualifiedAttributeName = qualifyConverterAttributeName(attributeNamePrefix, physicalAnnotation.attributeName());
        if (!convertAnnotationsMap.containsKey(qualifiedAttributeName)) {
            convertAnnotationsMap.put(qualifiedAttributeName, physicalAnnotation);
        }
    }
    final Converts physicalGroupingAnnotation = getPhysicalAnnotation(Converts.class);
    if (physicalGroupingAnnotation != null) {
        for (Convert convertAnnotation : physicalGroupingAnnotation.value()) {
            // again, only add if no XML element named a converter for this attribute
            final String qualifiedAttributeName = qualifyConverterAttributeName(attributeNamePrefix, convertAnnotation.attributeName());
            if (!convertAnnotationsMap.containsKey(qualifiedAttributeName)) {
                convertAnnotationsMap.put(qualifiedAttributeName, convertAnnotation);
            }
        }
    }
}
Also used : Convert(jakarta.persistence.Convert) JaxbConvert(org.hibernate.boot.jaxb.mapping.spi.JaxbConvert) Converts(jakarta.persistence.Converts)

Aggregations

Convert (jakarta.persistence.Convert)2 Converts (jakarta.persistence.Converts)2 HashMap (java.util.HashMap)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 JaxbConvert (org.hibernate.boot.jaxb.mapping.spi.JaxbConvert)1