Search in sources :

Example 1 with Convert

use of javax.persistence.Convert 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<String, AttributeConversionInfo>();
    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(javax.persistence.Convert) HashMap(java.util.HashMap) Converts(javax.persistence.Converts) XClass(org.hibernate.annotations.common.reflection.XClass)

Example 2 with Convert

use of javax.persistence.Convert in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getConverts.

private Converts getConverts(Element tree, XMLContext.Default defaults) {
    // NOTE : we use a map here to make sure that an xml and annotation referring to the same attribute
    // properly overrides.  Bit sparse, but easy...
    final Map<String, Convert> convertAnnotationsMap = new HashMap<String, Convert>();
    if (tree != null) {
        applyXmlDefinedConverts(tree, defaults, null, convertAnnotationsMap);
    }
    if (defaults.canUseJavaAnnotations()) {
        applyPhysicalConvertAnnotations(null, 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(javax.persistence.Convert) HashMap(java.util.HashMap)

Example 3 with Convert

use of javax.persistence.Convert in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getConvertsForAttribute.

private Annotation getConvertsForAttribute(List<Element> 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<String, Convert>();
    for (Element element : elementsForProperty) {
        final boolean isBasic = "basic".equals(element.getName());
        final boolean isEmbedded = "embedded".equals(element.getName());
        // todo : can be collections too
        final boolean canHaveConverts = isBasic || isEmbedded;
        if (!canHaveConverts) {
            continue;
        }
        final String attributeNamePrefix = isBasic ? null : propertyName;
        applyXmlDefinedConverts(element, defaults, attributeNamePrefix, 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(javax.persistence.Convert) HashMap(java.util.HashMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element)

Example 4 with Convert

use of javax.persistence.Convert in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader 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(javax.persistence.Convert) Converts(javax.persistence.Converts)

Example 5 with Convert

use of javax.persistence.Convert in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method applyXmlDefinedConverts.

private void applyXmlDefinedConverts(Element containingElement, XMLContext.Default defaults, String attributeNamePrefix, Map<String, Convert> convertAnnotationsMap) {
    final List<Element> convertElements = containingElement.elements("convert");
    for (Element convertElement : convertElements) {
        final AnnotationDescriptor convertAnnotationDescriptor = new AnnotationDescriptor(Convert.class);
        copyStringAttribute(convertAnnotationDescriptor, convertElement, "attribute-name", false);
        copyBooleanAttribute(convertAnnotationDescriptor, convertElement, "disable-conversion");
        final Attribute converterClassAttr = convertElement.attribute("converter");
        if (converterClassAttr != null) {
            final String converterClassName = XMLContext.buildSafeClassName(converterClassAttr.getValue(), 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(javax.persistence.Convert) Attribute(org.dom4j.Attribute) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass)

Aggregations

Convert (javax.persistence.Convert)5 HashMap (java.util.HashMap)3 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)3 AnnotatedElement (java.lang.reflect.AnnotatedElement)2 Converts (javax.persistence.Converts)2 Element (org.dom4j.Element)2 IdClass (javax.persistence.IdClass)1 MapKeyClass (javax.persistence.MapKeyClass)1 Attribute (org.dom4j.Attribute)1 AnnotationException (org.hibernate.AnnotationException)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1