Search in sources :

Example 1 with NotYetImplementedException

use of org.hibernate.cfg.NotYetImplementedException 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, ConverterDescriptor 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.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, Date.class)) {
            isDate = true;
        } else if (buildingContext.getBootstrapContext().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.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, java.sql.Clob.class)) {
            type = isNationalized ? StandardBasicTypes.NCLOB.getName() : StandardBasicTypes.CLOB.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, java.sql.NClob.class)) {
            type = StandardBasicTypes.NCLOB.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, java.sql.Blob.class)) {
            type = "blob";
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, String.class)) {
            type = isNationalized ? StandardBasicTypes.MATERIALIZED_NCLOB.getName() : StandardBasicTypes.MATERIALIZED_CLOB.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, Character.class) && isArray) {
            type = isNationalized ? CharacterArrayNClobType.class.getName() : CharacterArrayClobType.class.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, char.class) && isArray) {
            type = isNationalized ? PrimitiveCharacterArrayNClobType.class.getName() : PrimitiveCharacterArrayClobType.class.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, Byte.class) && isArray) {
            type = WrappedMaterializedBlobType.class.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, byte.class) && isArray) {
            type = StandardBasicTypes.MATERIALIZED_BLOB.getName();
        } else if (buildingContext.getBootstrapContext().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.getBootstrapContext().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.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, String.class)) {
            // nvarchar
            type = StringNVarcharType.INSTANCE.getName();
            explicitType = type;
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, Character.class) || buildingContext.getBootstrapContext().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 before 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 2 with NotYetImplementedException

use of org.hibernate.cfg.NotYetImplementedException 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 3 with NotYetImplementedException

use of org.hibernate.cfg.NotYetImplementedException in project hibernate-orm by hibernate.

the class QueryBinder method bindNativeQuery.

public static void bindNativeQuery(org.hibernate.annotations.NamedNativeQuery queryAnn, MetadataBuildingContext context) {
    if (queryAnn == null) {
        return;
    }
    // ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    NamedSQLQueryDefinition query;
    String resultSetMapping = queryAnn.resultSetMapping();
    if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) {
        // sql result set usage
        query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setResultSetRef(resultSetMapping).setQuerySpaces(null).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
    } else if (!void.class.equals(queryAnn.resultClass())) {
        // class mapping usage
        // FIXME should be done in a second pass due to entity name?
        final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn("alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ);
        query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setQueryReturns(new NativeSQLQueryReturn[] { entityQueryReturn }).setQuerySpaces(null).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
    } else {
        throw new NotYetImplementedException("Pure native scalar queries are not yet supported");
    }
    context.getMetadataCollector().addNamedNativeQuery(query);
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named native query: %s => %s", query.getName(), queryAnn.query());
    }
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.spi.NamedSQLQueryDefinition) HashMap(java.util.HashMap) AnnotationException(org.hibernate.AnnotationException) NativeSQLQueryRootReturn(org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn) NamedSQLQueryDefinitionBuilder(org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder) NotYetImplementedException(org.hibernate.cfg.NotYetImplementedException)

Aggregations

AnnotationException (org.hibernate.AnnotationException)3 NotYetImplementedException (org.hibernate.cfg.NotYetImplementedException)3 Serializable (java.io.Serializable)2 Date (java.util.Date)2 Properties (java.util.Properties)2 Lob (javax.persistence.Lob)2 TemporalType (javax.persistence.TemporalType)2 AssertionFailure (org.hibernate.AssertionFailure)2 MapKeyType (org.hibernate.annotations.MapKeyType)2 Type (org.hibernate.annotations.Type)2 XClass (org.hibernate.annotations.common.reflection.XClass)2 AccessType (org.hibernate.cfg.AccessType)2 CharacterArrayClobType (org.hibernate.type.CharacterArrayClobType)2 CharacterArrayNClobType (org.hibernate.type.CharacterArrayNClobType)2 CharacterNCharType (org.hibernate.type.CharacterNCharType)2 EnumType (org.hibernate.type.EnumType)2 PrimitiveCharacterArrayClobType (org.hibernate.type.PrimitiveCharacterArrayClobType)2 PrimitiveCharacterArrayNClobType (org.hibernate.type.PrimitiveCharacterArrayNClobType)2 SerializableToBlobType (org.hibernate.type.SerializableToBlobType)2 StringNVarcharType (org.hibernate.type.StringNVarcharType)2