Example usage for javax.persistence.criteria AbstractQuery from

List of usage examples for javax.persistence.criteria AbstractQuery from

Introduction

In this page you can find the example usage for javax.persistence.criteria AbstractQuery from.

Prototype

<X> Root<X> from(EntityType<X> entity);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

From source file:org.batoo.jpa.core.impl.criteria.jpql.JpqlQuery.java

/**
 * Creates the from fragment of the query.
 * //from  www. j a v a  2s. c  o  m
 * @param cb
 *            the criteria builder
 * @param q
 *            the query
 * @param from
 *            the from metadata
 * 
 * @since 2.0.0
 */
private void constructFrom(CriteriaBuilderImpl cb, AbstractQuery<?> q, Tree froms) {
    for (int i = 0; i < froms.getChildCount(); i++) {
        final Tree from = froms.getChild(i);
        // root query from
        if (from.getType() == JpqlParser.ST_FROM) {
            final Aliased fromDef = new Aliased(from.getChild(0));

            final EntityTypeImpl<Object> entity = this.getEntity(fromDef.getQualified().toString());

            final RootImpl<Object> r = (RootImpl<Object>) q.from(entity);
            r.alias(fromDef.getAlias());

            this.putAlias((BaseQuery<?>) q, from, fromDef, r);

            this.constructJoins(cb, q, r, from.getChild(1));

            if (from.getChild(from.getChildCount() - 1).getType() == JpqlParser.LALL_PROPERTIES) {
                for (final AssociationMappingImpl<?, ?, ?> association : entity.getAssociations()) {
                    if (!association.isEager()) {
                        final Iterator<String> pathIterator = Splitter.on(".").split(association.getPath())
                                .iterator();

                        // Drop the root part
                        pathIterator.next();

                        Fetch<?, ?> fetch = null;
                        while (pathIterator.hasNext()) {
                            fetch = fetch == null ? r.fetch(pathIterator.next())
                                    : fetch.fetch(pathIterator.next());
                        }
                    }
                }
            }
        }

        // in collection form
        else if (from.getType() == JpqlParser.ST_COLL) {
            final Aliased aliased = new Aliased(from.getChild(1));

            AbstractFrom<?, ?> parent = this.getAliased(q, from.getChild(0).getText());

            int depth = 0;
            for (final String segment : aliased.getQualified().getSegments()) {
                if ((depth > 0) && (parent instanceof PluralJoin)) {
                    throw new PersistenceException(
                            "Cannot qualify, only embeddable joins within the path allowed, " + "line "
                                    + from.getLine() + ":" + from.getCharPositionInLine());
                }

                parent = parent.join(segment, JoinType.LEFT);

                depth++;
            }

            parent.alias(aliased.getAlias());

            this.putAlias((BaseQueryImpl<?>) q, from.getChild(1), aliased, parent);
        }

        // sub query from
        else {
            final Aliased fromDef = new Aliased(from);
            final EntityTypeImpl<Object> entity = this.getEntity(fromDef.getQualified().toString());

            final RootImpl<Object> r = (RootImpl<Object>) q.from(entity);
            r.alias(fromDef.getAlias());

            this.putAlias((BaseQuery<?>) q, from, fromDef, r);
        }
    }
}