Example usage for org.hibernate.type TypeResolver basic

List of usage examples for org.hibernate.type TypeResolver basic

Introduction

In this page you can find the example usage for org.hibernate.type TypeResolver basic.

Prototype

public BasicType basic(String name) 

Source Link

Document

Locate a Hibernate BasicType basic type given (one of) its registration names.

Usage

From source file:br.com.tcc.common.support.GenericEnumUserType.java

/**
 * {@inheritDoc}/*from   www .  j av a2  s.c  o  m*/
 * @param parameters
 *            {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public void setParameterValues(Properties parameters) {
    String enumClassName = parameters.getProperty(ENUM_CLASS_PARAM_NAME);
    try {
        enumClass = (Class<? extends Enum<?>>) Class.forName(enumClassName).asSubclass(Enum.class);
    } catch (ClassNotFoundException cfne) {
        throw new HibernateException((new StringBuilder()).append("Enum class named '").append(enumClass)
                .append("'not found").toString(), cfne);
    }

    String identifierMethodName = parameters.getProperty(IDENTIFIER_METHOD_PARAM_NAME,
            DEFAULT_IDENTIFIER_METHOD_NAME);

    try {
        identifierMethod = enumClass.getMethod(identifierMethodName, new Class[FIRST_ITEM]);
        identifierType = identifierMethod.getReturnType();
    } catch (NoSuchMethodException e) {
        throw new HibernateException((new StringBuilder()).append(MSG_NO_METHOD_NAMED)
                .append(identifierMethodName).append(MSG_FOUND).toString(), e);
    } catch (SecurityException e) {
        throw new HibernateException((new StringBuilder()).append(MSG_NO_METHOD_NAMED)
                .append(identifierMethodName).append(MSG_FOUND).toString(), e);
    }

    TypeResolver tr = new TypeResolver();
    type = (AbstractSingleColumnStandardBasicType) tr.basic(identifierType.getName());

    if (type == null) {
        throw new HibernateException((new StringBuilder()).append("Unsupported identifier type ")
                .append(identifierType.getName()).toString());
    }

    sqlTypes = new int[] { type.sqlType() };

    String valueOfMethodName = parameters.getProperty(VALUE_OF_METHOD_PARAM_NAME, DEFAULT_VALUE_OF_METHOD_NAME);

    try {
        valueOfMethod = enumClass.getMethod(valueOfMethodName, new Class[] { identifierType });
    } catch (NoSuchMethodException e) {
        throw new HibernateException((new StringBuilder()).append(MSG_NO_METHOD_NAMED).append(valueOfMethodName)
                .append(MSG_FOUND).toString(), e);
    } catch (SecurityException e) {
        throw new HibernateException((new StringBuilder()).append(MSG_NO_METHOD_NAMED).append(valueOfMethodName)
                .append(MSG_FOUND).toString(), e);
    }
}

From source file:org.ambraproject.hibernate.GenericEnumUserType.java

License:Apache License

@Override
public void setParameterValues(Properties parameters) {
    String enumClassName = parameters.getProperty("enumClass");
    try {// ww w.  j  av a  2 s.c o m
        enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
    } catch (ClassNotFoundException exception) {
        throw new HibernateException("Enum class not found", exception);
    }

    String identifierMethodName = parameters.getProperty("identifierMethod", defaultIdentifierMethodName);

    try {
        identifierMethod = enumClass.getMethod(identifierMethodName, new Class[0]);
        identifierType = identifierMethod.getReturnType();
    } catch (Exception exception) {
        throw new HibernateException("Failed to optain identifier method", exception);
    }

    TypeResolver tr = new TypeResolver();
    type = (AbstractSingleColumnStandardBasicType) tr.basic(identifierType.getName());
    if (type == null) {
        throw new HibernateException("Unsupported identifier type " + identifierType.getName());
    }
    sqlTypes = new int[] { type.sqlType() };

    String valueOfMethodName = parameters.getProperty("valueOfMethod", defaultValueOfMethodName);

    try {
        valueOfMethod = enumClass.getMethod(valueOfMethodName, new Class[] { identifierType });
    } catch (Exception exception) {
        throw new HibernateException("Failed to optain valueOf method", exception);
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateQuery.java

License:Apache License

org.hibernate.criterion.Criterion getRestrictionForFunctionCall(FunctionCallingCriterion criterion,
        PersistentEntity entity) {//from  w  ww  . ja  v a 2 s  . com
    org.hibernate.criterion.Criterion sqlRestriction;

    SessionFactory sessionFactory = ((IHibernateTemplate) session.getNativeInterface()).getSessionFactory();
    String property = criterion.getProperty();
    Criterion datastoreCriterion = criterion.getPropertyCriterion();
    PersistentProperty pp = entity.getPropertyByName(property);

    if (pp == null)
        throw new InvalidDataAccessResourceUsageException(
                "Cannot execute function defined in query [" + criterion.getFunctionName()
                        + "] on non-existent property [" + property + "] of [" + entity.getJavaClass() + "]");

    String functionName = criterion.getFunctionName();

    Dialect dialect = getDialect(sessionFactory);
    SQLFunction sqlFunction = dialect.getFunctions().get(functionName);
    if (sqlFunction != null) {
        TypeResolver typeResolver = getTypeResolver(sessionFactory);
        BasicType basic = typeResolver.basic(pp.getType().getName());
        if (basic != null && datastoreCriterion instanceof PropertyCriterion) {

            PropertyCriterion pc = (PropertyCriterion) datastoreCriterion;
            final org.hibernate.criterion.Criterion hibernateCriterion = createHibernateCriterionAdapter(
                    getEntity(), datastoreCriterion, alias).toHibernateCriterion(this);
            if (hibernateCriterion instanceof SimpleExpression) {
                SimpleExpression expr = (SimpleExpression) hibernateCriterion;
                Object op = ReflectionUtils.getField(opField, expr);
                PropertyMapping mapping = getEntityPersister(entity.getJavaClass().getName(), sessionFactory);
                String[] columns;
                if (alias != null) {
                    columns = mapping.toColumns(alias, property);
                } else {
                    columns = mapping.toColumns(property);
                }
                String root = render(basic, Arrays.asList(columns), sessionFactory, sqlFunction);
                Object value = pc.getValue();
                if (value != null) {
                    sqlRestriction = Restrictions.sqlRestriction(root + op + "?", value,
                            typeResolver.basic(value.getClass().getName()));
                } else {
                    sqlRestriction = Restrictions.sqlRestriction(root + op + "?", value, basic);
                }
            } else {
                throw new InvalidDataAccessResourceUsageException(
                        "Unsupported function [" + functionName + "] defined in query for property [" + property
                                + "] with type [" + pp.getType() + "]");
            }
        } else {
            throw new InvalidDataAccessResourceUsageException("Unsupported function [" + functionName
                    + "] defined in query for property [" + property + "] with type [" + pp.getType() + "]");
        }
    } else {
        throw new InvalidDataAccessResourceUsageException(
                "Unsupported function defined in query [" + functionName + "]");
    }
    return sqlRestriction;
}

From source file:org.grails.orm.hibernate.query.AbstractHibernateQuery.java

License:Apache License

org.hibernate.criterion.Criterion getRestrictionForFunctionCall(FunctionCallingCriterion criterion,
        PersistentEntity entity) {// w  w  w. ja  v a2s  . c  o  m
    org.hibernate.criterion.Criterion sqlRestriction;

    SessionFactory sessionFactory = ((IHibernateTemplate) session.getNativeInterface()).getSessionFactory();
    String property = criterion.getProperty();
    Criterion datastoreCriterion = criterion.getPropertyCriterion();
    PersistentProperty pp = entity.getPropertyByName(property);

    if (pp == null)
        throw new InvalidDataAccessResourceUsageException(
                "Cannot execute function defined in query [" + criterion.getFunctionName()
                        + "] on non-existent property [" + property + "] of [" + entity.getJavaClass() + "]");

    String functionName = criterion.getFunctionName();

    Dialect dialect = getDialect(sessionFactory);
    SQLFunction sqlFunction = dialect.getFunctions().get(functionName);
    if (sqlFunction != null) {
        TypeResolver typeResolver = getTypeResolver(sessionFactory);
        BasicType basic = typeResolver.basic(pp.getType().getName());
        if (basic != null && datastoreCriterion instanceof PropertyCriterion) {

            PropertyCriterion pc = (PropertyCriterion) datastoreCriterion;
            final org.hibernate.criterion.Criterion hibernateCriterion = getHibernateCriterionAdapter()
                    .toHibernateCriterion(this, datastoreCriterion, alias);
            if (hibernateCriterion instanceof SimpleExpression) {
                SimpleExpression expr = (SimpleExpression) hibernateCriterion;
                Object op = ReflectionUtils.getField(opField, expr);
                PropertyMapping mapping = getEntityPersister(entity.getJavaClass().getName(), sessionFactory);
                String[] columns;
                if (alias != null) {
                    columns = mapping.toColumns(alias, property);
                } else {
                    columns = mapping.toColumns(property);
                }
                String root = render(basic, Arrays.asList(columns), sessionFactory, sqlFunction);
                Object value = pc.getValue();
                if (value != null) {
                    sqlRestriction = Restrictions.sqlRestriction(root + op + "?", value,
                            typeResolver.basic(value.getClass().getName()));
                } else {
                    sqlRestriction = Restrictions.sqlRestriction(root + op + "?", value, basic);
                }
            } else {
                throw new InvalidDataAccessResourceUsageException(
                        "Unsupported function [" + functionName + "] defined in query for property [" + property
                                + "] with type [" + pp.getType() + "]");
            }
        } else {
            throw new InvalidDataAccessResourceUsageException("Unsupported function [" + functionName
                    + "] defined in query for property [" + property + "] with type [" + pp.getType() + "]");
        }
    } else {
        throw new InvalidDataAccessResourceUsageException(
                "Unsupported function defined in query [" + functionName + "]");
    }
    return sqlRestriction;
}

From source file:org.openxma.dsl.platform.hibernate.type.GenericEnumUserType.java

License:Open Source License

public void setParameterValues(Properties parameters) {
    String enumClassName = parameters.getProperty(ENUM);
    try {/*from  ww w. j  ava  2s  .co  m*/
        enumClass = ReflectHelper.classForName(enumClassName, this.getClass()).asSubclass(Enum.class);
    } catch (ClassNotFoundException exception) {
        throw new HibernateException("Enum class not found", exception);
    }
    String identifierMethodName = parameters.getProperty("identifierMethod", DEFAULT_IDENTIFIER_METHOD_NAME);

    try {
        identifierMethod = enumClass.getMethod(identifierMethodName, new Class[0]);
        identifierType = identifierMethod.getReturnType();
    } catch (Exception e) {
        throw new HibernateException(e);
    }
    String valueOfMethodName = parameters.getProperty("valueOfMethod", DEFAULT_VALUE_OF_METHOD_NAME);
    try {
        valueOfMethod = enumClass.getMethod(valueOfMethodName, new Class[] { identifierType });
    } catch (Exception e) {
        throw new HibernateException("Failed to obtain valueOf method", e);
    }
    String type = parameters.getProperty(TYPE);
    TypeResolver tr = new TypeResolver();
    int[] sqlTypes = null;
    BasicType basic = null;
    if (type != null) {
        basic = tr.basic(type);
    } else {
        basic = tr.basic(identifierType.getSimpleName().toLowerCase());
    }
    sqlTypes = basic.sqlTypes(null);
    sqlType = sqlTypes[0];
}