Search in sources :

Example 1 with StringRepresentableType

use of org.hibernate.type.StringRepresentableType in project hibernate-orm by hibernate.

the class CriteriaQueryTranslator method getTypedValue.

/**
 * Get the a typed value for the given property value.
 */
@Override
public TypedValue getTypedValue(Criteria subcriteria, String propertyName, Object value) throws HibernateException {
    // Detect discriminator values...
    if (value instanceof Class) {
        final Class entityClass = (Class) value;
        final Queryable q = SessionFactoryHelper.findQueryableUsingImports(sessionFactory, entityClass.getName());
        if (q != null) {
            final Type type = q.getDiscriminatorType();
            String stringValue = q.getDiscriminatorSQLValue();
            if (stringValue != null && stringValue.length() > 2 && stringValue.startsWith("'") && stringValue.endsWith("'")) {
                // remove the single quotes
                stringValue = stringValue.substring(1, stringValue.length() - 1);
            }
            // Convert the string value into the proper type.
            if (type instanceof StringRepresentableType) {
                final StringRepresentableType nullableType = (StringRepresentableType) type;
                value = nullableType.fromStringValue(stringValue);
            } else {
                throw new QueryException("Unsupported discriminator type " + type);
            }
            return new TypedValue(type, value);
        }
    }
    // Otherwise, this is an ordinary value.
    return new TypedValue(getTypeUsingProjection(subcriteria, propertyName), value);
}
Also used : StringRepresentableType(org.hibernate.type.StringRepresentableType) CollectionType(org.hibernate.type.CollectionType) JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) StringRepresentableType(org.hibernate.type.StringRepresentableType) Queryable(org.hibernate.persister.entity.Queryable) TypedValue(org.hibernate.engine.spi.TypedValue)

Aggregations

QueryException (org.hibernate.QueryException)1 TypedValue (org.hibernate.engine.spi.TypedValue)1 Queryable (org.hibernate.persister.entity.Queryable)1 JoinType (org.hibernate.sql.JoinType)1 AssociationType (org.hibernate.type.AssociationType)1 CollectionType (org.hibernate.type.CollectionType)1 StringRepresentableType (org.hibernate.type.StringRepresentableType)1 Type (org.hibernate.type.Type)1