Search in sources :

Example 1 with DomainType

use of org.hibernate.metamodel.model.domain.DomainType 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

BasicDomainType (org.hibernate.metamodel.model.domain.BasicDomainType)1 DomainType (org.hibernate.metamodel.model.domain.DomainType)1 PrimitiveJavaType (org.hibernate.type.descriptor.java.spi.PrimitiveJavaType)1 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)1