Search in sources :

Example 1 with PrimitiveJavaType

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

the class UnsavedValueFactory method getUnsavedIdentifierValue.

/**
 * Return the UnsavedValueStrategy for determining whether an entity instance is
 * unsaved based on the identifier.  If an explicit strategy is not specified, determine
 * the unsaved value by instantiating an instance of the entity and reading the value of
 * its id property, or if that is not possible, using the java default value for the type
 */
public static IdentifierValue getUnsavedIdentifierValue(KeyValue bootIdMapping, JavaType<?> idJtd, Getter getter, Supplier<?> templateInstanceAccess) {
    final String unsavedValue = bootIdMapping.getNullValue();
    if (unsavedValue == null) {
        if (getter != null && templateInstanceAccess != null) {
            // use the id value of a newly instantiated instance as the unsaved-value
            final Object templateInstance = templateInstanceAccess.get();
            final Object defaultValue = getter.get(templateInstance);
            return new IdentifierValue(defaultValue);
        } else if (idJtd instanceof PrimitiveJavaType) {
            return new IdentifierValue(((PrimitiveJavaType<?>) idJtd).getDefaultValue());
        } else {
            return IdentifierValue.NULL;
        }
    } else if ("null".equals(unsavedValue)) {
        return IdentifierValue.NULL;
    } else if ("undefined".equals(unsavedValue)) {
        return IdentifierValue.UNDEFINED;
    } else if ("none".equals(unsavedValue)) {
        return IdentifierValue.NONE;
    } else if ("any".equals(unsavedValue)) {
        return IdentifierValue.ANY;
    } else {
        return new IdentifierValue(idJtd.fromString(unsavedValue));
    }
}
Also used : IdentifierValue(org.hibernate.engine.spi.IdentifierValue) PrimitiveJavaType(org.hibernate.type.descriptor.java.spi.PrimitiveJavaType)

Example 2 with PrimitiveJavaType

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

the class ReflectHelper method getConstructor.

/**
 * Retrieve a constructor for the given class, with arguments matching
 * the specified Hibernate mapping {@linkplain Type types}.
 *
 * @param clazz The class needing instantiation
 * @param types The types representing the required ctor param signature
 * @return The matching constructor.
 * @throws PropertyNotFoundException Indicates we could not locate an appropriate constructor (todo : again with PropertyNotFoundException???)
 */
public static Constructor getConstructor(Class clazz, Type[] types) throws PropertyNotFoundException {
    final Constructor[] candidates = clazz.getConstructors();
    Constructor constructor = null;
    int numberOfMatchingConstructors = 0;
    for (final Constructor candidate : candidates) {
        final Class[] params = candidate.getParameterTypes();
        if (params.length == types.length) {
            boolean found = true;
            for (int j = 0; j < params.length; j++) {
                final boolean ok = types[j] == null || params[j].isAssignableFrom(types[j].getReturnedClass()) || (types[j] instanceof BasicType<?> && ((BasicType<?>) types[j]).getJavaTypeDescriptor() instanceof PrimitiveJavaType && params[j] == ((PrimitiveJavaType<?>) (((BasicType<?>) types[j]).getJavaTypeDescriptor())).getPrimitiveClass());
                if (!ok) {
                    found = false;
                    break;
                }
            }
            if (found) {
                numberOfMatchingConstructors++;
                ensureAccessibility(candidate);
                constructor = candidate;
            }
        }
    }
    if (numberOfMatchingConstructors == 1) {
        return constructor;
    }
    throw new PropertyNotFoundException("no appropriate constructor in class: " + clazz.getName());
}
Also used : BasicType(org.hibernate.type.BasicType) PropertyNotFoundException(org.hibernate.PropertyNotFoundException) Constructor(java.lang.reflect.Constructor) PrimitiveJavaType(org.hibernate.type.descriptor.java.spi.PrimitiveJavaType)

Example 3 with PrimitiveJavaType

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

the class AbstractSelectionQuery method verifyResultType.

protected static <T> void verifyResultType(Class<T> resultClass, SqmExpressible<?> sqmExpressible, SessionFactoryImplementor sessionFactory) {
    assert sqmExpressible != null;
    final JavaType<?> expressibleJavaType = sqmExpressible.getExpressibleJavaType();
    assert expressibleJavaType != null;
    final Class<?> javaTypeClass = expressibleJavaType.getJavaTypeClass();
    if (!resultClass.isAssignableFrom(javaTypeClass)) {
        if (expressibleJavaType instanceof PrimitiveJavaType) {
            if (((PrimitiveJavaType) expressibleJavaType).getPrimitiveClass() == resultClass) {
                return;
            }
            throwQueryTypeMismatchException(resultClass, sqmExpressible);
        }
        // But the expected resultClass could be a subtype of that, so we need to check the JdbcType
        if (javaTypeClass == Date.class) {
            JdbcType jdbcType = null;
            if (sqmExpressible instanceof BasicDomainType<?>) {
                jdbcType = ((BasicDomainType<?>) sqmExpressible).getJdbcType();
            } else if (sqmExpressible instanceof SqmPathSource<?>) {
                final DomainType<?> domainType = ((SqmPathSource<?>) sqmExpressible).getSqmPathType();
                if (domainType instanceof BasicDomainType<?>) {
                    jdbcType = ((BasicDomainType<?>) domainType).getJdbcType();
                }
            }
            if (jdbcType != null) {
                switch(jdbcType.getJdbcTypeCode()) {
                    case Types.DATE:
                        if (resultClass.isAssignableFrom(java.sql.Date.class)) {
                            return;
                        }
                        break;
                    case Types.TIME:
                        if (resultClass.isAssignableFrom(java.sql.Time.class)) {
                            return;
                        }
                        break;
                    case Types.TIMESTAMP:
                        if (resultClass.isAssignableFrom(java.sql.Timestamp.class)) {
                            return;
                        }
                        break;
                }
            }
        }
        throwQueryTypeMismatchException(resultClass, sqmExpressible);
    }
}
Also used : BasicDomainType(org.hibernate.metamodel.model.domain.BasicDomainType) DomainType(org.hibernate.metamodel.model.domain.DomainType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) PrimitiveJavaType(org.hibernate.type.descriptor.java.spi.PrimitiveJavaType) BasicDomainType(org.hibernate.metamodel.model.domain.BasicDomainType)

Aggregations

PrimitiveJavaType (org.hibernate.type.descriptor.java.spi.PrimitiveJavaType)3 Constructor (java.lang.reflect.Constructor)1 PropertyNotFoundException (org.hibernate.PropertyNotFoundException)1 IdentifierValue (org.hibernate.engine.spi.IdentifierValue)1 BasicDomainType (org.hibernate.metamodel.model.domain.BasicDomainType)1 DomainType (org.hibernate.metamodel.model.domain.DomainType)1 BasicType (org.hibernate.type.BasicType)1 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)1