Search in sources :

Example 1 with EnumJavaTypeDescriptor

use of org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor in project hibernate-orm by hibernate.

the class RegistryHelper method resolveDescriptor.

@SuppressWarnings("unchecked")
public <J> JavaTypeDescriptor<J> resolveDescriptor(Map<Class, JavaTypeDescriptor> descriptorsByClass, Class<J> cls, Supplier<JavaTypeDescriptor<J>> defaultValueSupplier) {
    if (cls == null) {
        throw new IllegalArgumentException("Class passed to locate JavaTypeDescriptor cannot be null");
    }
    JavaTypeDescriptor<J> descriptor = descriptorsByClass.get(cls);
    if (descriptor != null) {
        return descriptor;
    }
    if (cls.isEnum()) {
        descriptor = new EnumJavaTypeDescriptor(cls);
        descriptorsByClass.put(cls, descriptor);
        return descriptor;
    }
    // find the first "assignable" match
    for (Map.Entry<Class, JavaTypeDescriptor> entry : descriptorsByClass.entrySet()) {
        if (entry.getKey().isAssignableFrom(cls)) {
            log.debugf("Using  cached JavaTypeDescriptor instance for Java class [%s]", cls.getName());
            return entry.getValue();
        }
    }
    return defaultValueSupplier.get();
}
Also used : EnumJavaTypeDescriptor(org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor) Map(java.util.Map) EnumJavaTypeDescriptor(org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor) JavaTypeDescriptor(org.hibernate.type.descriptor.java.JavaTypeDescriptor)

Example 2 with EnumJavaTypeDescriptor

use of org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor in project hibernate-orm by hibernate.

the class EnumType method setParameterValues.

@Override
public void setParameterValues(Properties parameters) {
    // IMPL NOTE: we handle 2 distinct cases here:
    // 1) we are passed a ParameterType instance in the incoming Properties - generally
    // speaking this indicates the annotation-binding case, and the passed ParameterType
    // represents information about the attribute and annotation
    // 2) we are not passed a ParameterType - generally this indicates a hbm.xml binding case.
    final ParameterType reader = (ParameterType) parameters.get(PARAMETER_TYPE);
    if (reader != null) {
        enumClass = reader.getReturnedClass().asSubclass(Enum.class);
        final boolean isOrdinal;
        final javax.persistence.EnumType enumType = getEnumType(reader);
        if (enumType == null) {
            isOrdinal = true;
        } else if (javax.persistence.EnumType.ORDINAL.equals(enumType)) {
            isOrdinal = true;
        } else if (javax.persistence.EnumType.STRING.equals(enumType)) {
            isOrdinal = false;
        } else {
            throw new AssertionFailure("Unknown EnumType: " + enumType);
        }
        final EnumJavaTypeDescriptor enumJavaDescriptor = (EnumJavaTypeDescriptor) typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor(enumClass);
        if (isOrdinal) {
            this.enumValueConverter = new OrdinalEnumValueConverter(enumJavaDescriptor);
        } else {
            this.enumValueConverter = new NamedEnumValueConverter(enumJavaDescriptor);
        }
    } else {
        final String enumClassName = (String) parameters.get(ENUM);
        try {
            enumClass = ReflectHelper.classForName(enumClassName, this.getClass()).asSubclass(Enum.class);
        } catch (ClassNotFoundException exception) {
            throw new HibernateException("Enum class not found: " + enumClassName, exception);
        }
        this.enumValueConverter = interpretParameters(parameters);
    }
    LOG.debugf("Using %s-based conversion for Enum %s", isOrdinal() ? "ORDINAL" : "NAMED", enumClass.getName());
}
Also used : EnumJavaTypeDescriptor(org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor) NamedEnumValueConverter(org.hibernate.metamodel.model.convert.internal.NamedEnumValueConverter) AssertionFailure(org.hibernate.AssertionFailure) HibernateException(org.hibernate.HibernateException) OrdinalEnumValueConverter(org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter)

Aggregations

EnumJavaTypeDescriptor (org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor)2 Map (java.util.Map)1 AssertionFailure (org.hibernate.AssertionFailure)1 HibernateException (org.hibernate.HibernateException)1 NamedEnumValueConverter (org.hibernate.metamodel.model.convert.internal.NamedEnumValueConverter)1 OrdinalEnumValueConverter (org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter)1 JavaTypeDescriptor (org.hibernate.type.descriptor.java.JavaTypeDescriptor)1