Package javax.persistence.criteria

Examples of javax.persistence.criteria.Root


    try {
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Tuple> cq = cb.createTupleQuery();

      Root root = cq.from(config.getEntityClass());
      Type idType = root.getModel().getIdType();
      SingularAttribute idAttr = root.getModel().getId(
          idType.getJavaType());

      cq.multiselect(root.get(idAttr));
      List<Tuple> tuples = em.createQuery(cq).getResultList();

      Set<Object> keys = new HashSet<Object>();
      for (Tuple t : tuples) {
        Object id = t.get(0);
View Full Code Here


  private boolean hasImplicitSelection() {
    if ( getRoots().size() != 1 ) {
      return false;
    }

    Root root = getRoots().iterator().next();
        Class<?> javaType = root.getModel().getJavaType();
        if ( javaType != null && javaType != returnType ) {
      return false;
    }

    // if we get here, the query defined no selection but defined a single root of the same type as the
View Full Code Here

  private boolean hasImplicitSelection() {
    if ( getRoots().size() != 1 ) {
      return false;
    }

    Root root = getRoots().iterator().next();
    if ( root.getModel().getJavaType() != returnType ) {
      return false;
    }

    // if we get here, the query defined no selection but defined a single root of the same type as the
    // criteria query return, so we use that as the implicit selection
View Full Code Here

  private boolean hasImplicitSelection() {
    if ( getRoots().size() != 1 ) {
      return false;
    }

    Root root = getRoots().iterator().next();
    if ( root.getModel().getJavaType() != returnType ) {
      return false;
    }

    // if we get here, the query defined no selection but defined a single root of the same type as the
    // criteria query return, so we use that as the implicit selection
View Full Code Here

  private boolean hasImplicitSelection() {
    if ( getRoots().size() != 1 ) {
      return false;
    }

    Root root = getRoots().iterator().next();
        Class<?> javaType = root.getModel().getJavaType();
        if ( javaType != null && javaType != returnType ) {
      return false;
    }

    // if we get here, the query defined no selection but defined a single root of the same type as the
View Full Code Here

        }
        if (query.getReferenceClass() == null){
            if (this.where != null && ((InternalSelection) this.where).getCurrentNode() != null && ((InternalSelection) this.where).getCurrentNode().getBuilder() != null && ((InternalSelection) this.where).getCurrentNode().getBuilder().getQueryClass() != null) {
                query.setReferenceClass(((InternalSelection) this.where).getCurrentNode().getBuilder().getQueryClass());
            } else if (roots != null && ! roots.isEmpty()){
                Root root = this.getRoots().iterator().next();
                query.setReferenceClass(root.getJavaType());
            }
        }

        if (selection == null) {
            //the builder in the where clause  may not be the correct builder for this query.  Search for a root that matches the query type.
            if (roots != null && ! roots.isEmpty()){
                for (Root root : this.getRoots()){
                    if (root.getJavaType().equals(this.queryType)){
                        query.setExpressionBuilder(((RootImpl) root).getCurrentNode().getBuilder());
                        break;
                    }
                }
            }
View Full Code Here

              for (Class<?> clazz : polyClasses) {
                ManagedType mt = mm.managedType(clazz);
                try {
                    Attribute attr = mt.getAttribute(piece);
                    if (attr != null) {
                        Root additionalRoot = criteria.from(clazz);
                        restrictions.add(builder.equal(path, additionalRoot));
                        path = additionalRoot.get(piece);
                        found = true;
                        break;
                    }
                } catch (IllegalArgumentException e2) {
                    // Do nothing - we'll try the next class and see if it has the attribute
View Full Code Here

            criteria.select((Selection<? extends T>) root);
        }

        //find only customers that do not have any orders, otherwise a purge would fail because of referential integrity
        Subquery<Long> subquery = criteria.subquery(Long.class);
        Root orderRoot = subquery.from(OrderImpl.class);
        subquery.select(builder.count(orderRoot));
        subquery.where(builder.equal(orderRoot.get("customer"),root));

        List<Predicate> restrictions = new ArrayList<Predicate>();
        restrictions.add(builder.equal(subquery, 0L));
        if (registered != null) {
            if (registered) {
View Full Code Here

                        else {
                            /*
                             * Create subquery to find by attribute
                             */
                            Subquery subquery = cq.subquery(attr.getJavaType());
                            Root fromAttr = subquery.from(attr.getJavaType());
                            subquery.select(fromAttr.get(NamingConstants.ID));

                            if (var instanceof String && ((String)var).contains("%")) {
                                /*
                                 * This is a like query
                                 */
                                subquery.where(builder.like(builder.lower(fromAttr.get(fields[1])), var.toString().toLowerCase()));  
                            }
                            else {
                                /*
                                 * This is strong equals query
                                 */
                                subquery.where(builder.equal(fromAttr.get(fields[1]), var));
                            }

                            /*
                             * Add where to main query
                             */
 
View Full Code Here

                                       List attributesFilter,
                                       String joinColumn,
                                       List<Predicate> preList)
    {
        Subquery<A> subquery = cq.subquery(attributesClass);
        Root fromAttr = subquery.from(attributesClass);

        subquery.select(fromAttr.get(joinColumn).get(NamingConstants.ID));
        subquery.groupBy(fromAttr.get(joinColumn).get(NamingConstants.ID));
        subquery.having(builder.equal(builder.count(fromAttr.get(joinColumn).get(NamingConstants.ID)), attributesFilter.size()));

        Predicate wherePredicates = null;

        for (Object a : attributesFilter) {
            if (!(a instanceof AbstractAttribute))
                continue;

            AbstractAttribute attr = (AbstractAttribute)a;
            Predicate pp = null;
            switch (attr.getDataType()) {
                case STRING:
                    pp = builder.and (
                            builder.equal(fromAttr.get(NamingConstants.NAME), attr.getName()),
                            builder.equal(
                                    fromAttr.get(NamingConstants.STRING_VALUE),
                                    attr.getStringValue())
                        );
                    break;
                   
                case NUMBER:
                    pp = builder.and (
                            builder.equal(fromAttr.get(NamingConstants.NAME), attr.getName()),
                            builder.equal(
                                    fromAttr.get(NamingConstants.NUMBER_VALUE),
                                    attr.getNumberValue())
                        );
                    break;
                   
                case DATE:
                    pp = builder.and (
                            builder.equal(fromAttr.get(NamingConstants.NAME), attr.getName()),
                            builder.equal(
                                    fromAttr.get(NamingConstants.DATE_VALUE),
                                    attr.getDateValue())
                        );
                    break;
            }
           
View Full Code Here

TOP

Related Classes of javax.persistence.criteria.Root

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.