Example usage for org.hibernate.criterion Restrictions in

List of usage examples for org.hibernate.criterion Restrictions in

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions in.

Prototype

public static Criterion in(String propertyName, Collection values) 

Source Link

Document

Apply an "in" constraint to the named property.

Usage

From source file:com.reignite.parser.QueryParser.java

License:Open Source License

private Criterion processField(JSONObject where) throws ParserException {
    String field = JSONObject.getNames(where)[0];
    JSONObject exp;/*from   ww w .  j  a  v a2s  . com*/
    try {
        exp = where.getJSONObject(field);
    } catch (JSONException e) {
        throw new ParserException("field expressions must be JSON Object: " + field);
    }
    ExpressionType type = ExpressionType.get(JSONObject.getNames(exp)[0]);
    Object value = null;
    // if the field is a join
    if (field.indexOf(".") > -1) {
        String join = field.substring(0, field.indexOf("."));
        query.createJoin(join);
    }
    switch (type) {
    case IN:
    case NOT_IN:
        try {
            value = createArray(exp.getJSONArray(type.getName()));
        } catch (JSONException e) {
            throw new ParserException("in and not in expressions must be arrays: " + exp);
        }
        break;
    default:
        try {
            value = createValue(exp.get(type.getName()));
        } catch (JSONException e) {
            throw new ParserException("expressions must have a value: " + exp);
        }
    }

    switch (type) {
    case GREATER_THAN:
        return Restrictions.gt(field, value);
    case IN:
        return Restrictions.in(field, (Object[]) value);
    case LESS_THAN:
        return Restrictions.lt(field, value);
    case LIKE:
        MatchMode match = MatchMode.EXACT;
        String toMatch = value.toString();
        if (value.toString().startsWith("%")) {
            match = MatchMode.END;
            toMatch = toMatch.substring(1);
        }
        if (value.toString().endsWith("%")) {
            toMatch = toMatch.substring(0, toMatch.length() - 1);
            if (match == MatchMode.END) {
                match = MatchMode.ANYWHERE;
            } else {
                match = MatchMode.START;
            }
        }
        return Restrictions.ilike(field, toMatch, match);
    case NOT_EQUAL:
        if (value == null) {
            return Restrictions.isNotNull(field);
        }
        return Restrictions.ne(field, value);
    case NOT_IN:
        return Restrictions.not(Restrictions.in(field, (Object[]) value));
    case EQUAL:
    default:
        if (value == null) {
            return Restrictions.isNull(field);
        }
        return Restrictions.eq(field, value);
    }

}

From source file:com.romeikat.datamessie.core.base.cache.NamedEntityName2CategoriesCache.java

License:Open Source License

private Collection<NamedEntity> getNamedEntites(final SharedSessionContract ssc,
        final Collection<Long> categoryNamedEntityIds) {
    final EntityWithIdQuery<NamedEntity> namedEntityQuery = new EntityWithIdQuery<>(NamedEntity.class);
    namedEntityQuery.addRestriction(Restrictions.in("id", categoryNamedEntityIds));
    final Collection<NamedEntity> namedEntities = namedEntityQuery.listObjects(ssc);
    return namedEntities;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.AbstractEntityWithIdAndVersionDao.java

License:Open Source License

@Override
public TreeMap<Long, Long> getIdsWithVersion(final SharedSessionContract ssc, final Collection<Long> ids) {
    if (ids.isEmpty()) {
        return new TreeMap<Long, Long>();
    }/*from   w ww. j  av a2s .  co m*/

    // Query
    final EntityWithIdQuery<E> query = new EntityWithIdQuery<>(getEntityClass());
    query.addRestriction(Restrictions.in("id", ids));
    query.setResultTransformer(new AliasToBeanResultTransformer(IdAndVersion.class));

    // Done
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"), "id");
    projectionList.add(Projections.property("version"), "version");
    @SuppressWarnings("unchecked")
    final List<IdAndVersion> idsAndVersions = (List<IdAndVersion>) query.listForProjection(ssc, projectionList);

    // Transform into map
    final TreeMap<Long, Long> result = transformIntoMap(idsAndVersions);
    return result;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.AbstractEntityWithIdDao.java

License:Open Source License

@Override
public List<E> getEntities(final SharedSessionContract ssc, final Collection<Long> ids) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }/*from   ww  w  .  ja v a  2s. co m*/

    // Query
    final Criteria criteria = ssc.createCriteria(getEntityClass());
    criteria.add(Restrictions.in("id", ids));
    // Done
    @SuppressWarnings("unchecked")
    final List<E> result = criteria.list();
    return result;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.AbstractEntityWithIdDao.java

License:Open Source License

@Override
public List<Long> getIds(final SharedSessionContract ssc, final Collection<Long> ids) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }/*from ww w  .  j av a 2s  .c  om*/

    // Query
    final EntityWithIdQuery<E> query = new EntityWithIdQuery<>(getEntityClass());
    query.addRestriction(Restrictions.in("id", ids));
    final String defaultSortingProperty = defaultSortingProperty();
    if (defaultSortingProperty != null) {
        query.addOrder(Order.asc(defaultSortingProperty));
    }
    // Done
    final List<Long> result = query.listIds(ssc);
    return result;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.AbstractEntityWithIdDao.java

License:Open Source License

@Override
public List<Long> getIds(final SharedSessionContract ssc, final Collection<Long> ids, final Long firstId,
        final Integer maxResults) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }/*from w  w  w  . ja  v  a2  s  .c  o m*/

    // Query
    final EntityWithIdQuery<E> query = new EntityWithIdQuery<>(getEntityClass());
    query.addRestriction(Restrictions.in("id", ids));
    if (firstId != null) {
        query.addRestriction(Restrictions.ge("id", firstId));
    }
    query.setMaxResults(maxResults);
    query.addOrder(Order.asc("id"));
    // Done
    final List<Long> result = query.listIds(ssc);
    return result;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.SourceDao.java

License:Open Source License

public List<Long> getIds(final SharedSessionContract ssc, final Long projectId, final Boolean visible) {
    if (projectId == null) {
        return Collections.emptyList();
    }//w w w .  java 2  s .  co  m

    // Query: Project2Source
    final EntityQuery<Project2Source> project2SourceQuery = new EntityQuery<>(Project2Source.class);
    project2SourceQuery.addRestriction(Restrictions.eq("projectId", projectId));
    final Collection<Long> sourceIds = project2SourceQuery.listIdsForProperty(ssc, "sourceId");
    if (sourceIds.isEmpty()) {
        return Collections.emptyList();
    }

    // Query: Source
    final EntityWithIdQuery<Source> sourceQuery = new EntityWithIdQuery<>(Source.class);
    sourceQuery.addRestriction(Restrictions.in("id", sourceIds));
    if (visible != null) {
        sourceQuery.addRestriction(Restrictions.eq("visible", visible));
    }

    // Done
    final List<Long> sourceIds2 = sourceQuery.listIds(ssc);
    return sourceIds2;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.SourceDao.java

License:Open Source License

public SourceDto getAsDto(final SharedSessionContract ssc, final Long userId, final Long id) {
    if (id == null) {
        return null;
    }// www .j  a v  a2s  . c om

    // Restrict to user
    final Collection<Long> projectIdsForUser = projectDao.getIdsForUser(ssc, userId);
    if (projectIdsForUser.isEmpty()) {
        return null;
    }

    // Query: Project2Source
    final EntityQuery<Project2Source> project2SourceQuery = new EntityQuery<>(Project2Source.class);
    project2SourceQuery.addRestriction(Restrictions.in("projectId", projectIdsForUser));
    final Collection<Long> sourceIds = project2SourceQuery.listIdsForProperty(ssc, "sourceId");
    if (sourceIds.isEmpty()) {
        return null;
    }

    // Query: Source
    final EntityWithIdQuery<Source> sourceQuery = new EntityWithIdQuery<>(Source.class);
    sourceQuery.addRestriction(Restrictions.idEq(id));
    sourceQuery.addRestriction(Restrictions.in("id", sourceIds));

    final Source source = sourceQuery.uniqueObject(ssc);
    return sourceToDto(ssc, source);
}

From source file:com.romeikat.datamessie.core.base.dao.impl.SourceDao.java

License:Open Source License

public List<SourceDto> getAsDtos(final SharedSessionContract ssc, final Long userId, final Long projectId,
        final Boolean visible) {
    if (projectId == null) {
        return Collections.emptyList();
    }/*from w  ww.  j  ava2  s  .  co m*/

    // Restrict to user
    final Collection<Long> projectIdsForUser = projectDao.getIdsForUser(ssc, userId);
    if (projectIdsForUser.isEmpty()) {
        return Collections.emptyList();
    }

    // Query: Project2Source
    final EntityQuery<Project2Source> project2SourceQuery = new EntityQuery<>(Project2Source.class);
    project2SourceQuery.addRestriction(Restrictions.eq("projectId", projectId));
    final Collection<Long> sourceIds = project2SourceQuery.listIdsForProperty(ssc, "sourceId");
    if (sourceIds.isEmpty()) {
        return Collections.emptyList();
    }

    // Query: Source
    final EntityWithIdQuery<Source> sourceQuery = new EntityWithIdQuery<>(Source.class);
    sourceQuery.addRestriction(Restrictions.in("id", sourceIds));
    if (visible != null) {
        sourceQuery.addRestriction(Restrictions.eq("visible", visible));
    }
    sourceQuery.addOrder(Order.asc("name"));

    // Done
    final List<Source> sources = sourceQuery.listObjects(ssc);

    // Transform
    final List<SourceDto> entitesAsDtos = Lists.transform(sources, s -> sourceToDto(ssc, s));
    return Lists.newArrayList(entitesAsDtos);
}

From source file:com.romeikat.datamessie.core.base.dao.impl.SourceDao.java

License:Open Source License

public List<SourceOverviewDto> getAsOverviewDtos(final SharedSessionContract ssc, final Long userId,
        final Long projectId, final Boolean visible, final Long first, final Long count) {
    if (projectId == null) {
        return Collections.emptyList();
    }/*from  w w w  .  j  av  a 2  s .co  m*/

    // Restrict to user
    final Collection<Long> projectIdsForUser = projectDao.getIdsForUser(ssc, userId);
    if (projectIdsForUser.isEmpty()) {
        return Collections.emptyList();
    }

    // Query: Project2Source
    final EntityQuery<Project2Source> project2SourceQuery = new EntityQuery<>(Project2Source.class);
    project2SourceQuery.addRestriction(Restrictions.eq("projectId", projectId));
    project2SourceQuery.addRestriction(Restrictions.in("projectId", projectIdsForUser));
    final Collection<Long> sourceIds = project2SourceQuery.listIdsForProperty(ssc, "sourceId");
    if (sourceIds.isEmpty()) {
        return Collections.emptyList();
    }

    // Query: Source
    final EntityWithIdQuery<Source> sourceQuery = new EntityWithIdQuery<>(Source.class);
    sourceQuery.addRestriction(Restrictions.in("id", sourceIds));
    if (visible != null) {
        sourceQuery.addRestriction(Restrictions.eq("visible", visible));
    }
    sourceQuery.setFirstResult(first == null ? null : first.intValue());
    sourceQuery.setMaxResults(count == null ? null : count.intValue());
    sourceQuery.addOrder(Order.asc("name"));

    // Done
    final List<Source> sources = sourceQuery.listObjects(ssc);

    // Transform
    final List<SourceOverviewDto> dtos = Lists.transform(sources, s -> sourceToOverviewDto(ssc, s));
    return Lists.newArrayList(dtos);
}