javax.persistence.criteria.Fetch类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(236)

本文整理了Java中javax.persistence.criteria.Fetch类的一些代码示例,展示了Fetch类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Fetch类的具体详情如下:
包路径:javax.persistence.criteria.Fetch
类名称:Fetch

Fetch介绍

[英]Represents a join-fetched association or attribute.
[中]表示联接获取的关联或属性。

代码示例

代码示例来源:origin: spring-projects/spring-data-jpa

/**
 * Return whether the given {@link From} contains a fetch declaration for the attribute with the given name.
 *
 * @param from the {@link From} to check for fetches.
 * @param attribute the attribute name to check.
 * @return
 */
private static boolean isAlreadyFetched(From<?, ?> from, String attribute) {
  for (Fetch<?, ?> fetch : from.getFetches()) {
    boolean sameName = fetch.getAttribute().getName().equals(attribute);
    if (sameName && fetch.getJoinType().equals(JoinType.LEFT)) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: hibernate/hibernate-orm

@Test
public void test_criteria_from_fetch_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    //tag::criteria-from-fetch-example[]
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Phone> criteria = builder.createQuery( Phone.class );
    Root<Phone> root = criteria.from( Phone.class );
    // Phone.person is a @ManyToOne
    Fetch<Phone, Person> personFetch = root.fetch( Phone_.person );
    // Person.addresses is an @ElementCollection
    Fetch<Person, String> addressesJoin = personFetch.fetch( Person_.addresses );
    criteria.where( builder.isNotEmpty( root.get( Phone_.calls ) ) );
    List<Phone> phones = entityManager.createQuery( criteria ).getResultList();
    //end::criteria-from-fetch-example[]
    assertEquals(2, phones.size());
  });
}

代码示例来源:origin: hibernate/hibernate-orm

@SuppressWarnings({ "unchecked" })
  private void renderFetches(
      StringBuilder jpaqlQuery,
      RenderingContext renderingContext,
      Collection<Fetch> fetches) {
    if ( fetches == null ) {
      return;
    }

    for ( Fetch fetch : fetches ) {
      ( (FromImplementor) fetch ).prepareAlias( renderingContext );
      jpaqlQuery.append( renderJoinType( fetch.getJoinType() ) )
          .append( "fetch " )
          .append( ( (FromImplementor) fetch ).renderTableExpression( renderingContext ) );

      renderFetches( jpaqlQuery, renderingContext, fetch.getFetches() );
    }
  }
}

代码示例来源:origin: org.jboss.pressgang.ccms/pressgang-ccms-query

/**
   * Copy Fetches
   *
   * @param from source Fetch
   * @param to   dest Fetch
   */
  public static void copyFetches(Fetch<?, ?> from, Fetch<?, ?> to) {
    for (Fetch<?, ?> f : from.getFetches()) {
      Fetch<?, ?> toFetch = to.fetch(f.getAttribute().getName());
      // recursively copy fetches
      copyFetches(f, toFetch);
    }
  }
}

代码示例来源:origin: org.omnifaces/omnipersistence

@Override
public Attribute<? super Z, ?> getAttribute() {
  return getWrapped().getAttribute();
}

代码示例来源:origin: katharsis-project/katharsis-framework

private static boolean containsMultiRelationFetch(Set<?> fetches) {
 for (Object fetchObj : fetches) {
  Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj;
  Attribute<?, ?> attr = fetch.getAttribute();
  if (attr.isAssociation() && attr.isCollection())
   return true;
  if (containsMultiRelationFetch(fetch.getFetches()))
   return true;
 }
 return false;
}

代码示例来源:origin: org.omnifaces/omnipersistence

@Override
public JoinType getJoinType() {
  return getWrapped().getJoinType();
}

代码示例来源:origin: org.omnifaces/omnipersistence

@Override
public Set<Fetch<X, ?>> getFetches() {
  return getWrapped().getFetches();
}

代码示例来源:origin: org.ligoj.bootstrap/bootstrap-business

private Fetch<?, ?> getFetchedAssoc(final FetchParent<?, ?> parent, final JoinType joinType, final String propertyToFetch) {
  // Search within current fetches
  for (final Fetch<?, ?> fetch : parent.getFetches()) {
    if (fetch.getAttribute().getName().equals(propertyToFetch)) {
      return fetch;
    }
  }
  // Create a new one
  return parent.fetch(propertyToFetch, joinType);
}

代码示例来源:origin: katharsis-project/katharsis-framework

private static boolean containsMultiRelationJoin(Set<?> fetches) {
 for (Object fetchObj : fetches) {
  Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj;
  Attribute<?, ?> attr = fetch.getAttribute();
  if (attr.isAssociation() && attr.isCollection())
   return true;
  if (containsMultiRelationFetch(fetch.getFetches()))
   return true;
 }
 return false;
}

代码示例来源:origin: br.eti.clairton/repository

@SuppressWarnings("unchecked")
  protected void fetchToJoin(final From<?, ?> from, final Set<Fetch<?, ?>> fetches) {
    if (fetches != null && !fetches.isEmpty()) {
      for (final Fetch<?, ?> fetch : fetches) {
        @SuppressWarnings("rawtypes")
        final Join join = (Join) fetch;
        final Set<Fetch<?, ?>> fs = (Set<Fetch<?, ?>>) ((Set<?>) fetch.getFetches());
        if (fs.isEmpty()) {
          try {
            from.getJoins().add(join);
          } catch (UnsupportedOperationException e) {
          }
        } else {
          fetchToJoin(join, fs);
        }
      }
      from.getFetches().clear();
    }
  }
}

代码示例来源:origin: org.springframework.data/spring-data-jpa

/**
 * Return whether the given {@link From} contains a fetch declaration for the attribute with the given name.
 *
 * @param from the {@link From} to check for fetches.
 * @param attribute the attribute name to check.
 * @return
 */
private static boolean isAlreadyFetched(From<?, ?> from, String attribute) {
  for (Fetch<?, ?> fetch : from.getFetches()) {
    boolean sameName = fetch.getAttribute().getName().equals(attribute);
    if (sameName && fetch.getJoinType().equals(JoinType.LEFT)) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: org.omnifaces/omnipersistence

@Override
@SuppressWarnings("hiding")
public <X, Y> Fetch<X, Y> fetch(String attributeName) {
  return getWrapped().fetch(attributeName);
}

代码示例来源:origin: org.omnifaces/omnipersistence

private static void collectJoins(Path<?> path, Map<String, Path<?>> joins) {
  if (path instanceof From) {
    ((From<?, ?>) path).getJoins().forEach(join -> collectJoins(join, joins));
  }
  if (path instanceof FetchParent) {
    try {
      ((FetchParent<?, ?>) path).getFetches().stream().filter(fetch -> fetch instanceof Path).forEach(fetch -> collectJoins((Path<?>) fetch, joins));
    }
    catch (NullPointerException openJPAWillThrowThisOnEmptyFetches) {
      // Ignore and continue.
    }
  }
  if (path instanceof Join) {
    joins.put(((Join<?, ?>) path).getAttribute().getName(), path);
  }
  else if (path instanceof Fetch) {
    joins.put(((Fetch<?, ?>) path).getAttribute().getName(), path);
  }
}

代码示例来源:origin: org.hibernate/com.springsource.org.hibernate.ejb

@SuppressWarnings({ "unchecked" })
  private void renderFetches(
      StringBuilder jpaqlQuery,
      CriteriaQueryCompiler.RenderingContext renderingContext,
      Collection<Fetch> fetches) {
    if ( fetches == null ) {
      return;
    }

    for ( Fetch fetch : fetches ) {
      ( (FromImplementor) fetch ).prepareAlias( renderingContext );
      jpaqlQuery.append( renderJoinType( fetch.getJoinType() ) )
          .append( "fetch " )
          .append( ( (FromImplementor) fetch ).renderTableExpression( renderingContext ) );

      renderFetches( jpaqlQuery, renderingContext, fetch.getFetches() );
    }
  }
}

代码示例来源:origin: org.apache.openejb.patch/openjpa

protected void evalFetchJoin(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) {
  List<String> iPaths = new ArrayList<String>();
  List<String> oPaths = new ArrayList<String>();
  Set<Root<?>> roots = q.getRoots();
  for (Root root : roots) {
    Set<Fetch> fetches = root.getFetches();
    if (fetches == null)
      continue;
    for (Fetch<?,?> fetch : fetches) {
      String fPath = ((Members.Member<?, ?>)fetch.getAttribute()).fmd.getFullName(false);
      oPaths.add(fPath);
      if (fetch.getJoinType() == JoinType.INNER) {
        iPaths.add(fPath);
      } 
    }
  }
  if (!iPaths.isEmpty()) {
    exps.fetchInnerPaths = iPaths.toArray(new String[iPaths.size()]);
  }
  if (!oPaths.isEmpty()) {
    exps.fetchPaths = oPaths.toArray(new String[oPaths.size()]);
  }
}

代码示例来源:origin: org.jasig.portal/uPortal-web

/** Add all the fetches needed for completely loading the object graph */
protected void addFetches(final Root<PortletEntityImpl> definitionRoot) {
  definitionRoot
      .fetch(PortletEntityImpl_.portletPreferences, JoinType.LEFT)
      .fetch(PortletPreferencesImpl_.portletPreferences, JoinType.LEFT)
      .fetch(PortletPreferenceImpl_.values, JoinType.LEFT);
  definitionRoot.fetch(PortletEntityImpl_.windowStates, JoinType.LEFT);
}

代码示例来源:origin: jaxio/jpa-query-by-example

@SuppressWarnings({"unchecked", "rawtypes"})
  protected void fetches(SearchParameters sp, Root<E> root) {
    for (List<Attribute<?, ?>> args : sp.getFetches()) {
      FetchParent<?, ?> from = root;
      for (Attribute<?, ?> arg : args) {
        boolean found = false;
        for (Fetch<?, ?> fetch : from.getFetches()) {
          if (arg.equals(fetch.getAttribute())) {
            from = fetch;
            found = true;
            break;
          }
        }
        if (!found) {
          if (arg instanceof PluralAttribute) {
            from = from.fetch((PluralAttribute) arg, JoinType.LEFT);
          } else {
            from = from.fetch((SingularAttribute) arg, JoinType.LEFT);
          }
        }
      }
    }
  }
}

代码示例来源:origin: org.apache.openejb.patch/openjpa-persistence

protected void evalFetchJoin(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) {
  List<String> iPaths = new ArrayList<String>();
  List<String> oPaths = new ArrayList<String>();
  Set<Root<?>> roots = q.getRoots();
  for (Root root : roots) {
    Set<Fetch> fetches = root.getFetches();
    if (fetches == null)
      continue;
    for (Fetch<?,?> fetch : fetches) {
      String fPath = ((Members.Member<?, ?>)fetch.getAttribute()).fmd.getFullName(false);
      oPaths.add(fPath);
      if (fetch.getJoinType() == JoinType.INNER) {
        iPaths.add(fPath);
      } 
    }
  }
  if (!iPaths.isEmpty()) {
    exps.fetchInnerPaths = iPaths.toArray(new String[iPaths.size()]);
  }
  if (!oPaths.isEmpty()) {
    exps.fetchPaths = oPaths.toArray(new String[oPaths.size()]);
  }
}

代码示例来源:origin: org.omnifaces/omnipersistence

@Override
public <Y> Fetch<X, Y> fetch(PluralAttribute<? super X, ?, Y> attribute, JoinType jt) {
  return getWrapped().fetch(attribute, jt);
}

相关文章