Example usage for org.hibernate.criterion CriteriaSpecification LEFT_JOIN

List of usage examples for org.hibernate.criterion CriteriaSpecification LEFT_JOIN

Introduction

In this page you can find the example usage for org.hibernate.criterion CriteriaSpecification LEFT_JOIN.

Prototype

int LEFT_JOIN

To view the source code for org.hibernate.criterion CriteriaSpecification LEFT_JOIN.

Click Source Link

Document

Specifies joining to an entity based on a left outer join.

Usage

From source file:net.databinder.models.hib.BaseCriteriaBuildAndSort.java

License:Open Source License

protected String processProperty(final Criteria criteria, String property) {
    if (property.contains(".")) {
        // for 'dot' properties we need to add aliases
        // e.g. for the property 'orderbook.order.item.name' we need to add an aliases for 'order' and 'order.item'
        String path[] = property.split("\\.");
        for (int ii = 0; ii < path.length - 1; ii++) {
            StringBuffer sb = new StringBuffer();
            for (int jj = 0; jj <= ii; jj++) {
                if (sb.length() > 0) {
                    sb.append(".");
                }/*from  w w  w  .  j ava 2 s  .co  m*/
                sb.append(path[jj]);
            }
            if (!aliases.contains(path[ii])) {
                aliases.add(path[ii]);
                criteria.createAlias(sb.toString(), path[ii], CriteriaSpecification.LEFT_JOIN);
            }
        }
        // when we have a 'dot' property we want to sort by the sub tables field
        // e.g. for the property 'orderbook.order.item.name' we need to sort by 'item.name'
        if (path.length > 1) {
            property = String.format("%s.%s", path[path.length - 2], path[path.length - 1]);
        } else {
            property = path[path.length - 1];
        }
    }
    return property;
}

From source file:net.databinder.models.hib.CriteriaSorter.java

License:Open Source License

public void build(Criteria criteria) {
    SortParam sort = sortState.getSort();
    String property;//from  w w w  .j a v a  2  s  .co  m
    if (sort != null && sort.getProperty() != null) {
        property = sort.getProperty();
        asc = sort.isAscending();
    } else {
        property = defaultProperty;
    }
    if (property != null) {
        if (property.contains(".")) {
            // for 'dot' properties we need to add aliases
            // e.g. for the property 'orderbook.order.item.name' we need to add an aliases for 'order' and 'order.item'
            String path[] = property.split("\\.");
            for (int ii = 0; ii < path.length - 1; ii++) {
                StringBuffer sb = new StringBuffer();
                for (int jj = 0; jj <= ii; jj++) {
                    if (sb.length() > 0)
                        sb.append(".");
                    sb.append(path[jj]);
                }
                criteria.createAlias(sb.toString(), path[ii], CriteriaSpecification.LEFT_JOIN);
            }
            // when we have a 'dot' property we want to sort by the sub tables field
            // e.g. for the property 'orderbook.order.item.name' we need to sort by 'item.name'
            if (path.length > 1)
                property = String.format("%s.%s", path[path.length - 2], path[path.length - 1]);
            else
                property = path[path.length - 1];
        }
        Order order = asc ? Order.asc(property) : Order.desc(property);
        order = cased ? order : order.ignoreCase();
        criteria.addOrder(order);
    }
}

From source file:net.firejack.platform.core.store.AbstractStore.java

License:Apache License

@SuppressWarnings("unchecked")
public <ES> List<ES> findWithFilter(final List<Criterion> criterions, final Map<String, String> aliases,
        final Projection projection, final List<String> fetchPaths, final Class<ES> projectionClazz,
        final SpecifiedIdsFilter filter, final Paging paging) {
    return getHibernateTemplate().execute(new HibernateCallback<List<ES>>() {
        public List<ES> doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createCriteriaForFilter(session, filter);
            if (paging != null) {
                if (paging.getLimit() != null && paging.getLimit() > -1) {
                    criteria.setMaxResults(paging.getLimit());
                }/*  ww  w .ja v a  2 s .  c  o m*/
                if (paging.getOffset() != null && paging.getOffset() > -1) {
                    criteria.setFirstResult(paging.getOffset());
                }
                List<SortField> sortFields = paging.getSortFields();
                if (sortFields != null && sortFields.size() > 0) {
                    for (SortField sortField : sortFields) {
                        if (StringUtils.isNotBlank(sortField.getSortColumn())) {
                            SortOrder sortDirection = sortField.getSortDirection();
                            Order order = sortDirection == null || sortDirection == SortOrder.ASC
                                    ? Order.asc(sortField.getSortColumn())
                                    : Order.desc(sortField.getSortColumn());
                            criteria.addOrder(order);
                        }
                    }
                }
            }

            if (aliases != null && !aliases.isEmpty()) {
                for (Map.Entry<String, String> alias : aliases.entrySet()) {
                    criteria.createAlias(alias.getKey(), alias.getValue(), CriteriaSpecification.LEFT_JOIN);
                }
            }

            if (criterions != null && !criterions.isEmpty()) {
                for (Criterion restrictions : criterions) {
                    criteria.add(restrictions);
                }
            }

            if (projection != null) {
                criteria.setProjection(projection);
            }

            if (projectionClazz != null) {
                ResultTransformer transformer = Transformers.aliasToBean(projectionClazz);
                criteria.setResultTransformer(transformer);
            } else {
                criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            }

            if (fetchPaths != null) {
                for (String fetchPath : fetchPaths) {
                    criteria.setFetchMode(fetchPath, FetchMode.JOIN);
                }
            }

            return (List<ES>) criteria.list();
        }
    });
}

From source file:net.firejack.platform.core.store.AbstractStore.java

License:Apache License

protected Criteria prepareCriteria(Session session, LinkedList<Criterion> criterionList,
        Map<String, String> aliases, Paging paging, boolean isOr, boolean isLeft) {
    Criteria criteria = session.createCriteria(clazz);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    if (aliases != null && !aliases.isEmpty()) {
        for (Map.Entry<String, String> alias : aliases.entrySet()) {
            criteria.createAlias(alias.getKey(), alias.getValue(),
                    isLeft ? CriteriaSpecification.LEFT_JOIN : CriteriaSpecification.INNER_JOIN);
        }/*from   ww w. jav a2 s .  c  o  m*/
    }

    if (criterionList != null) {
        Criterion left = null;
        for (Criterion criterion : criterionList) {
            left = criterionList.getFirst() == criterion ? criterion
                    : isOr ? Restrictions.or(left, criterion) : Restrictions.and(left, criterion);

        }
        if (left != null)
            criteria.add(left);
    }

    if (paging != null) {
        if (paging.getLimit() != null && paging.getLimit() > -1) {
            criteria.setMaxResults(paging.getLimit());
        }
        if (paging.getOffset() != null && paging.getOffset() > -1) {
            criteria.setFirstResult(paging.getOffset());
        }
        if (paging.getSortFields() != null) {
            for (SortField sortField : paging.getSortFields()) {
                if (sortField.getSortDirection().equals(SortOrder.ASC)) {
                    criteria.addOrder(Order.asc(sortField.getSortColumn()));
                } else {
                    criteria.addOrder(Order.desc(sortField.getSortColumn()));
                }
            }
        }
    }
    return criteria;
}

From source file:net.firejack.platform.core.store.AbstractStore.java

License:Apache License

protected Integer searchCount(final LinkedList<Criterion> criterions, final Map<String, String> aliases,
        final boolean isOr, final boolean isLeft) {
    return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
        @Override/*ww  w. j a  v  a 2s. com*/
        public Integer doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(clazz);

            if (aliases != null && !aliases.isEmpty()) {
                for (Map.Entry<String, String> alias : aliases.entrySet()) {
                    criteria.createAlias(alias.getKey(), alias.getValue(),
                            isLeft ? CriteriaSpecification.LEFT_JOIN : CriteriaSpecification.INNER_JOIN);
                }
            }

            if (criterions != null) {
                Criterion left = null;
                for (Criterion criterion : criterions) {
                    left = criterions.getFirst() == criterion ? criterion
                            : isOr ? Restrictions.or(left, criterion) : Restrictions.and(left, criterion);
                }
                if (left != null)
                    criteria.add(left);
            }

            return ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
        }
    });
}

From source file:net.firejack.platform.core.store.BaseStore.java

License:Apache License

public <ES> List<ES> findAllWithFilter(final Integer offset, final Integer limit,
        final List<Criterion> criterions, final Map<String, String> aliases, final SpecifiedIdsFilter filter,
        final Projection projection, final Class<ES> projectionClazz, final List<String> fetchPaths,
        final Order... orders) {
    return getHibernateTemplate().execute(new HibernateCallback<List<ES>>() {
        public List<ES> doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createCriteriaForFilter(session, filter);
            if (limit != null && limit > -1) {
                criteria.setMaxResults(limit);
            }//  w  w w  .jav a2 s  .  c  o  m
            if (offset != null && offset > -1) {
                criteria.setFirstResult(offset);
            }

            if (aliases != null && !aliases.isEmpty()) {
                for (Map.Entry<String, String> alias : aliases.entrySet()) {
                    criteria.createAlias(alias.getKey(), alias.getValue(), CriteriaSpecification.LEFT_JOIN);
                }
            }

            if (criterions != null && !criterions.isEmpty()) {
                for (Criterion restrictions : criterions) {
                    criteria.add(restrictions);
                }
            }

            if (projection != null) {
                criteria.setProjection(projection);
            }

            if (projectionClazz != null) {
                ResultTransformer transformer = Transformers.aliasToBean(projectionClazz);
                criteria.setResultTransformer(transformer);
            } else {
                criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            }

            if (fetchPaths != null) {
                for (String fetchPath : fetchPaths) {
                    criteria.setFetchMode(fetchPath, FetchMode.JOIN);
                }
            }

            if (orders != null) {
                for (Order order : orders) {
                    if (order != null) {
                        criteria.addOrder(order);
                    }
                }
            }

            return (List<ES>) criteria.list();
        }
    });
}

From source file:net.firejack.platform.core.store.BaseStore.java

License:Apache License

public Integer findCountWithFilter(final List<Criterion> criterions, final Map<String, String> aliases,
        final SpecifiedIdsFilter filter, final Projection projection) {
    return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
        public Integer doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createCriteriaForFilter(session, filter);

            if (aliases != null && !aliases.isEmpty()) {
                for (Map.Entry<String, String> alias : aliases.entrySet()) {
                    criteria.createAlias(alias.getKey(), alias.getValue(), CriteriaSpecification.LEFT_JOIN);
                }//from w  ww .  ja v a2 s .c  o  m
            }

            if (criterions != null && !criterions.isEmpty()) {
                for (Criterion restrictions : criterions) {
                    criteria.add(restrictions);
                }
            }

            if (projection != null) {
                criteria.setProjection(projection);
            }

            ScrollableResults scroll = criteria.scroll();
            return scroll.last() ? scroll.getRowNumber() + 1 : 0;
        }
    });
}

From source file:net.firejack.platform.core.store.BaseStore.java

License:Apache License

/**
 *
 * @param criterions//  w w w  .  j  a  va 2s.  c om
 * @param aliases
 * @param filter
 * @return
 */
@Transactional(readOnly = true)
public E findByCriteria(final List<Criterion> criterions, final Map<String, String> aliases,
        final SpecifiedIdsFilter filter) {
    return getHibernateTemplate().execute(new HibernateCallback<E>() {
        public E doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createCriteriaForFilter(session, filter);

            if (aliases != null && !aliases.isEmpty()) {
                for (Map.Entry<String, String> alias : aliases.entrySet()) {
                    criteria.createAlias(alias.getKey(), alias.getValue(), CriteriaSpecification.LEFT_JOIN);
                }
            }

            if (criterions != null && !criterions.isEmpty()) {
                for (Criterion restrictions : criterions) {
                    criteria.add(restrictions);
                }
            }
            List results = criteria.list();
            return (E) (!results.isEmpty() ? results.get(0) : null);
        }
    });
}

From source file:net.firejack.platform.core.store.registry.NavigationElementStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)//w  ww  .j ava  2 s  .  co m
public List<NavigationElementModel> findAllByLikeLookupPrefix(final String lookupPrefix) {
    if (StringUtils.isBlank(lookupPrefix)) {
        return Collections.emptyList();
    }
    return getHibernateTemplate().execute(new HibernateCallback<List<NavigationElementModel>>() {
        public List<NavigationElementModel> doInHibernate(Session session)
                throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(getClazz());
            criteria.add(Restrictions.like("lookup", lookupPrefix + "%"));
            criteria.createCriteria("main", "main", CriteriaSpecification.LEFT_JOIN);
            return (List<NavigationElementModel>) criteria.list();
        }
    });
}

From source file:net.firejack.platform.core.store.registry.RegistryNodeStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)/*w w w . j a  va  2s  .co  m*/
public List<R> findChildrenByParentIdAndTypes(final Long registryNodeId, final List<String> discriminatorValues,
        final SpecifiedIdsFilter<Long> filter) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createCriteriaForFilter(session, filter);
            if (registryNodeId == null) {
                criteria.add(Restrictions.isNull("parent"));
            } else {
                criteria.add(Restrictions.eq("parent.id", registryNodeId));
            }
            criteria.add(Restrictions.in("class", discriminatorValues));
            criteria.createAlias("main", "main", CriteriaSpecification.LEFT_JOIN);
            criteria.addOrder(Order.asc("sortPosition"));
            criteria.addOrder(Order.asc("name"));
            return (List<R>) criteria.list();
        }
    });
}