Example usage for org.hibernate.engine.spi TypedValue TypedValue

List of usage examples for org.hibernate.engine.spi TypedValue TypedValue

Introduction

In this page you can find the example usage for org.hibernate.engine.spi TypedValue TypedValue.

Prototype

public TypedValue(final Type type, final Object value) 

Source Link

Usage

From source file:com.blazebit.persistence.integration.hibernate.Hibernate43Access.java

License:Apache License

private String expandParameterList(SessionImplementor session, ParameterMetadata parameterMetadata,
        String query, String name, TypedValue typedList, Map<String, TypedValue> namedParamsCopy) {
    Collection<?> vals = (Collection<?>) typedList.getValue();

    // HHH-1123/*from w ww.jav  a2s  .  com*/
    // Some DBs limit number of IN expressions.  For now, warn...
    final Dialect dialect = session.getFactory().getDialect();
    final int inExprLimit = dialect.getInExpressionCountLimit();
    if (inExprLimit > 0 && vals.size() > inExprLimit) {
        LOG.warning(String.format(
                "Dialect [%s] limits the number of elements in an IN predicate to %s entries.  "
                        + "However, the given parameter list [%s] contained %s entries, which will likely cause failures "
                        + "to execute the query in the database",
                dialect.getClass().getName(), inExprLimit, name, vals.size()));
    }

    Type type = typedList.getType();

    boolean isJpaPositionalParam = parameterMetadata.getNamedParameterDescriptor(name).isJpaStyle();
    String paramPrefix = isJpaPositionalParam ? "?" : ParserHelper.HQL_VARIABLE_PREFIX;
    String placeholder = new StringBuilder(paramPrefix.length() + name.length()).append(paramPrefix)
            .append(name).toString();

    if (query == null) {
        return query;
    }
    int loc = query.indexOf(placeholder);

    if (loc < 0) {
        return query;
    }

    String beforePlaceholder = query.substring(0, loc);
    String afterPlaceholder = query.substring(loc + placeholder.length());

    // check if placeholder is already immediately enclosed in parentheses
    // (ignoring whitespace)
    boolean isEnclosedInParens = StringHelper.getLastNonWhitespaceCharacter(beforePlaceholder) == '('
            && StringHelper.getFirstNonWhitespaceCharacter(afterPlaceholder) == ')';

    if (vals.size() == 1 && isEnclosedInParens) {
        // short-circuit for performance when only 1 value and the
        // placeholder is already enclosed in parentheses...
        namedParamsCopy.put(name, new TypedValue(type, vals.iterator().next()));
        return query;
    }

    StringBuilder list = new StringBuilder(16);
    Iterator<?> iter = vals.iterator();
    int i = 0;
    while (iter.hasNext()) {
        // Variable 'name' can represent a number or contain digit at the end. Surrounding it with
        // characters to avoid ambiguous definition after concatenating value of 'i' counter.
        String alias = (isJpaPositionalParam ? 'x' + name : name) + '_' + i++ + '_';
        if (namedParamsCopy.put(alias, new TypedValue(type, iter.next())) != null) {
            throw new HibernateException(
                    "Repeated usage of alias '" + alias + "' while expanding list parameter.");
        }
        list.append(ParserHelper.HQL_VARIABLE_PREFIX).append(alias);
        if (iter.hasNext()) {
            list.append(", ");
        }
    }
    return StringHelper.replace(beforePlaceholder, afterPlaceholder, placeholder.toString(), list.toString(),
            true, true);
}

From source file:common.util.db.MonthGroupExpression.java

@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    return new TypedValue[] { new TypedValue(criteriaQuery.getIdentifierType(criteria), EntityMode.POJO) };
}

From source file:edu.uiowa.icts.criterion.CastAsVarcharLike.java

License:Apache License

/**
 * {@inheritDoc}/*from ww  w  .  j  a v a2s.c  o  m*/
 *
 * (non-Javadoc)
 * @see org.hibernate.criterion.Criterion#getTypedValues(org.hibernate.Criteria, org.hibernate.criterion.CriteriaQuery)
 */
@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    return new TypedValue[] { new TypedValue(new org.hibernate.type.StringType(),
            MatchMode.START.toMatchString(value.toLowerCase())) };
}

From source file:org.babyfish.hibernate.collection.spi.persistence.AbstractBasePersistence.java

License:Open Source License

protected static <E> Collection<E> getOrphans(XCollection<E> oldElements, XCollection<E> currentElements,
        String entityName, SessionImplementor session) throws HibernateException {

    // short-circuit(s)
    if (currentElements.isEmpty()) {
        return oldElements; // no new elements, the old list contains only Orphans
    }//ww  w  .  j  a  v  a2 s. c o  m
    if (oldElements.size() == 0) {
        return oldElements; // no old elements, so no Orphans neither
    }

    final EntityPersister entityPersister = session.getFactory().getEntityPersister(entityName);
    final Type idType = entityPersister.getIdentifierType();

    // create the collection holding the Orphans
    Collection<E> res = new ArrayList<E>(oldElements.unifiedComparator());

    // collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access
    Set<Serializable> currentIds = new HashSet<Serializable>();
    Set<E> currentSaving = new HashSet<E>(ReferenceEqualityComparator.getInstance());
    for (E current : currentElements) {
        if (current != null && ForeignKeys.isNotTransient(entityName, current, null, session)) {
            EntityEntry ee = session.getPersistenceContext().getEntry(current);
            if (ee != null && ee.getStatus() == Status.SAVING) {
                currentSaving.add(current);
            } else {
                Serializable currentId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, current,
                        session);
                currentIds.add(new TypedValue(idType, currentId));
            }
        }
    }

    // iterate over the *old* list
    for (E old : oldElements) {
        if (!currentSaving.contains(old)) {
            Serializable oldId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, old, session);
            if (!currentIds.contains(new TypedValue(idType, oldId))) {
                res.add(old);
            }
        }
    }

    return res;
}

From source file:org.eulerframework.web.core.extend.hibernate5.InExpressionX.java

License:Apache License

@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) {
    final ArrayList<TypedValue> list = new ArrayList<TypedValue>();
    final Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName);
    if (type.isComponentType()) {
        final CompositeType compositeType = (CompositeType) type;
        final Type[] subTypes = compositeType.getSubtypes();
        for (Object value : values) {
            for (int i = 0; i < subTypes.length; i++) {
                final Object subValue = value == null ? null
                        : compositeType.getPropertyValues(value, EntityMode.POJO)[i];
                list.add(new TypedValue(subTypes[i], subValue));
            }/*from ww w.  j av a2  s.c  o m*/
        }
    } else {
        for (Object value : values) {
            list.add(new TypedValue(type, value));
        }
    }

    return list.toArray(new TypedValue[list.size()]);
}