Search in sources :

Example 56 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class SimpleValueBinder method setType.

//TODO execute it lazily to be order safe
public void setType(XProperty property, XClass returnedClass, String declaringClassName, AttributeConverterDescriptor attributeConverterDescriptor) {
    if (returnedClass == null) {
        // we cannot guess anything
        return;
    }
    XClass returnedClassOrElement = returnedClass;
    boolean isArray = false;
    if (property.isArray()) {
        returnedClassOrElement = property.getElementClass();
        isArray = true;
    }
    this.xproperty = property;
    Properties typeParameters = this.typeParameters;
    typeParameters.clear();
    String type = BinderHelper.ANNOTATION_STRING_DEFAULT;
    if (getDialect().supportsNationalizedTypes()) {
        isNationalized = property.isAnnotationPresent(Nationalized.class) || buildingContext.getBuildingOptions().useNationalizedCharacterData();
    }
    Type annType = null;
    if ((!key && property.isAnnotationPresent(Type.class)) || (key && property.isAnnotationPresent(MapKeyType.class))) {
        if (key) {
            MapKeyType ann = property.getAnnotation(MapKeyType.class);
            annType = ann.value();
        } else {
            annType = property.getAnnotation(Type.class);
        }
    }
    if (annType != null) {
        setExplicitType(annType);
        type = explicitType;
    } else if ((!key && property.isAnnotationPresent(Temporal.class)) || (key && property.isAnnotationPresent(MapKeyTemporal.class))) {
        boolean isDate;
        if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, Date.class)) {
            isDate = true;
        } else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, Calendar.class)) {
            isDate = false;
        } else {
            throw new AnnotationException("@Temporal should only be set on a java.util.Date or java.util.Calendar property: " + StringHelper.qualify(persistentClassName, propertyName));
        }
        final TemporalType temporalType = getTemporalType(property);
        switch(temporalType) {
            case DATE:
                type = isDate ? "date" : "calendar_date";
                break;
            case TIME:
                type = "time";
                if (!isDate) {
                    throw new NotYetImplementedException("Calendar cannot persist TIME only" + StringHelper.qualify(persistentClassName, propertyName));
                }
                break;
            case TIMESTAMP:
                type = isDate ? "timestamp" : "calendar";
                break;
            default:
                throw new AssertionFailure("Unknown temporal type: " + temporalType);
        }
        explicitType = type;
    } else if (!key && property.isAnnotationPresent(Lob.class)) {
        isLob = true;
        if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, java.sql.Clob.class)) {
            type = isNationalized ? StandardBasicTypes.NCLOB.getName() : StandardBasicTypes.CLOB.getName();
        } else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, java.sql.NClob.class)) {
            type = StandardBasicTypes.NCLOB.getName();
        } else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, java.sql.Blob.class)) {
            type = "blob";
        } else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, String.class)) {
            type = isNationalized ? StandardBasicTypes.MATERIALIZED_NCLOB.getName() : StandardBasicTypes.MATERIALIZED_CLOB.getName();
        } else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, Character.class) && isArray) {
            type = isNationalized ? CharacterArrayNClobType.class.getName() : CharacterArrayClobType.class.getName();
        } else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, char.class) && isArray) {
            type = isNationalized ? PrimitiveCharacterArrayNClobType.class.getName() : PrimitiveCharacterArrayClobType.class.getName();
        } else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, Byte.class) && isArray) {
            type = WrappedMaterializedBlobType.class.getName();
        } else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, byte.class) && isArray) {
            type = StandardBasicTypes.MATERIALIZED_BLOB.getName();
        } else if (buildingContext.getBuildingOptions().getReflectionManager().toXClass(Serializable.class).isAssignableFrom(returnedClassOrElement)) {
            type = SerializableToBlobType.class.getName();
            typeParameters.setProperty(SerializableToBlobType.CLASS_NAME, returnedClassOrElement.getName());
        } else {
            type = "blob";
        }
        defaultType = type;
    } else if ((!key && property.isAnnotationPresent(Enumerated.class)) || (key && property.isAnnotationPresent(MapKeyEnumerated.class))) {
        final Class attributeJavaType = buildingContext.getBuildingOptions().getReflectionManager().toClass(returnedClassOrElement);
        if (!Enum.class.isAssignableFrom(attributeJavaType)) {
            throw new AnnotationException(String.format("Attribute [%s.%s] was annotated as enumerated, but its java type is not an enum [%s]", declaringClassName, xproperty.getName(), attributeJavaType.getName()));
        }
        type = EnumType.class.getName();
        explicitType = type;
    } else if (isNationalized) {
        if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, String.class)) {
            // nvarchar
            type = StringNVarcharType.INSTANCE.getName();
            explicitType = type;
        } else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, Character.class) || buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, char.class)) {
            if (isArray) {
                // nvarchar
                type = StringNVarcharType.INSTANCE.getName();
            } else {
                // nchar
                type = CharacterNCharType.INSTANCE.getName();
            }
            explicitType = type;
        }
    }
    // implicit type will check basic types and Serializable classes
    if (columns == null) {
        throw new AssertionFailure("SimpleValueBinder.setColumns should be set beforeQuery SimpleValueBinder.setType");
    }
    if (BinderHelper.ANNOTATION_STRING_DEFAULT.equals(type)) {
        if (returnedClassOrElement.isEnum()) {
            type = EnumType.class.getName();
        }
    }
    defaultType = BinderHelper.isEmptyAnnotationValue(type) ? returnedClassName : type;
    this.typeParameters = typeParameters;
    applyAttributeConverter(property, attributeConverterDescriptor);
}
Also used : Serializable(java.io.Serializable) AssertionFailure(org.hibernate.AssertionFailure) WrappedMaterializedBlobType(org.hibernate.type.WrappedMaterializedBlobType) Properties(java.util.Properties) XClass(org.hibernate.annotations.common.reflection.XClass) Date(java.util.Date) NotYetImplementedException(org.hibernate.cfg.NotYetImplementedException) PrimitiveCharacterArrayNClobType(org.hibernate.type.PrimitiveCharacterArrayNClobType) EnumType(org.hibernate.type.EnumType) AccessType(org.hibernate.cfg.AccessType) WrappedMaterializedBlobType(org.hibernate.type.WrappedMaterializedBlobType) DynamicParameterizedType(org.hibernate.usertype.DynamicParameterizedType) Type(org.hibernate.annotations.Type) CharacterArrayNClobType(org.hibernate.type.CharacterArrayNClobType) PrimitiveCharacterArrayNClobType(org.hibernate.type.PrimitiveCharacterArrayNClobType) SerializableToBlobType(org.hibernate.type.SerializableToBlobType) PrimitiveCharacterArrayClobType(org.hibernate.type.PrimitiveCharacterArrayClobType) CharacterNCharType(org.hibernate.type.CharacterNCharType) CharacterArrayClobType(org.hibernate.type.CharacterArrayClobType) StringNVarcharType(org.hibernate.type.StringNVarcharType) TemporalType(javax.persistence.TemporalType) MapKeyType(org.hibernate.annotations.MapKeyType) PrimitiveCharacterArrayClobType(org.hibernate.type.PrimitiveCharacterArrayClobType) SerializableToBlobType(org.hibernate.type.SerializableToBlobType) EnumType(org.hibernate.type.EnumType) AnnotationException(org.hibernate.AnnotationException) XClass(org.hibernate.annotations.common.reflection.XClass) MapKeyType(org.hibernate.annotations.MapKeyType) Lob(javax.persistence.Lob) TemporalType(javax.persistence.TemporalType)

Example 57 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class CollectionBinder method bindCollectionSecondPass.

private static void bindCollectionSecondPass(Collection collValue, PersistentClass collectionEntity, Ejb3JoinColumn[] joinColumns, boolean cascadeDeleteEnabled, XProperty property, PropertyHolder propertyHolder, MetadataBuildingContext buildingContext) {
    try {
        BinderHelper.createSyntheticPropertyReference(joinColumns, collValue.getOwner(), collectionEntity, collValue, false, buildingContext);
    } catch (AnnotationException ex) {
        throw new AnnotationException("Unable to map collection " + collValue.getOwner().getClassName() + "." + property.getName(), ex);
    }
    SimpleValue key = buildCollectionKey(collValue, joinColumns, cascadeDeleteEnabled, property, propertyHolder, buildingContext);
    if (property.isAnnotationPresent(ElementCollection.class) && joinColumns.length > 0) {
        joinColumns[0].setJPA2ElementCollection(true);
    }
    TableBinder.bindFk(collValue.getOwner(), collectionEntity, joinColumns, key, false, buildingContext);
}
Also used : AnnotationException(org.hibernate.AnnotationException) ElementCollection(javax.persistence.ElementCollection) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 58 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class QueryHintDefinition method getFlushMode.

public FlushMode getFlushMode(String query) {
    String hitName = QueryHints.FLUSH_MODE;
    String value = (String) hintsMap.get(hitName);
    if (value == null) {
        return null;
    }
    try {
        return FlushMode.interpretExternalSetting(value);
    } catch (MappingException e) {
        throw new AnnotationException("Unknown FlushMode in hint: " + query + ":" + hitName, e);
    }
}
Also used : AnnotationException(org.hibernate.AnnotationException) MappingException(org.hibernate.MappingException)

Example 59 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getMapKeyClass.

private void getMapKeyClass(List<Annotation> annotationList, Element element, XMLContext.Default defaults) {
    String nodeName = "map-key-class";
    Element subelement = element != null ? element.element(nodeName) : null;
    if (subelement != null) {
        String mapKeyClassName = subelement.attributeValue("class");
        AnnotationDescriptor ad = new AnnotationDescriptor(MapKeyClass.class);
        if (StringHelper.isNotEmpty(mapKeyClassName)) {
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(mapKeyClassName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find " + element.getPath() + " " + nodeName + ": " + mapKeyClassName, e);
            }
            ad.setValue("value", clazz);
        }
        annotationList.add(AnnotationFactory.create(ad));
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) 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)

Example 60 with AnnotationException

use of org.hibernate.AnnotationException in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method buildGeneratedValue.

private GeneratedValue buildGeneratedValue(Element element) {
    Element subElement = element != null ? element.element("generated-value") : null;
    if (subElement != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(GeneratedValue.class);
        String strategy = subElement.attributeValue("strategy");
        if ("TABLE".equalsIgnoreCase(strategy)) {
            ad.setValue("strategy", GenerationType.TABLE);
        } else if ("SEQUENCE".equalsIgnoreCase(strategy)) {
            ad.setValue("strategy", GenerationType.SEQUENCE);
        } else if ("IDENTITY".equalsIgnoreCase(strategy)) {
            ad.setValue("strategy", GenerationType.IDENTITY);
        } else if ("AUTO".equalsIgnoreCase(strategy)) {
            ad.setValue("strategy", GenerationType.AUTO);
        } else if (StringHelper.isNotEmpty(strategy)) {
            throw new AnnotationException("Unknown GenerationType: " + strategy + ". " + SCHEMA_VALIDATION);
        }
        copyStringAttribute(ad, subElement, "generator", false);
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) AnnotationException(org.hibernate.AnnotationException)

Aggregations

AnnotationException (org.hibernate.AnnotationException)85 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)15 PersistentClass (org.hibernate.mapping.PersistentClass)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)12 Element (org.dom4j.Element)12 XClass (org.hibernate.annotations.common.reflection.XClass)12 HashMap (java.util.HashMap)11 MappingException (org.hibernate.MappingException)11 XProperty (org.hibernate.annotations.common.reflection.XProperty)11 Property (org.hibernate.mapping.Property)11 SimpleValue (org.hibernate.mapping.SimpleValue)11 Test (org.junit.Test)10 AssertionFailure (org.hibernate.AssertionFailure)9 ArrayList (java.util.ArrayList)8 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)8 Column (org.hibernate.mapping.Column)8 IdClass (javax.persistence.IdClass)7 MapKeyClass (javax.persistence.MapKeyClass)7 Component (org.hibernate.mapping.Component)7 Properties (java.util.Properties)6