javax.persistence.Query.getResultStream()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(1.8k)|赞(0)|评价(0)|浏览(519)

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

Query.getResultStream介绍

[英]Execute a SELECT query and return the query results as an untyped java.util.stream.Stream. By default this method delegates to getResultList().stream(), however persistence provider may choose to override this method to provide additional capabilities.
[中]执行SELECT查询,并以非类型化的java.util.stream.Stream返回查询结果。默认情况下,此方法将委托给getResultList().stream(),但是持久性提供程序可以选择重写此方法以提供其他功能。

代码示例

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

@Test
public void deferredQueryWithResultStream() {
  EntityManagerFactory emf = mock(EntityManagerFactory.class);
  EntityManager targetEm = mock(EntityManager.class);
  Query query = mock(Query.class);
  given(emf.createEntityManager()).willReturn(targetEm);
  given(targetEm.createQuery("x")).willReturn(query);
  given(targetEm.isOpen()).willReturn(true);
  EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
  em.createQuery("x").getResultStream();
  verify(query).getResultStream();
  verify(targetEm).close();
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

@Test
public void testStreamExecutionPlan() {
  List<String> executionPlanLines = doInJPA(entityManager -> {
    try(Stream<String> postStream = entityManager
      .createNativeQuery(
        "EXPLAIN ANALYZE " +
        "SELECT p " +
        "FROM post p " +
        "ORDER BY p.created_on DESC")
      .setHint(QueryHints.HINT_FETCH_SIZE, 50)
      .getResultStream()
    ) {
      return postStream.limit(50).collect(Collectors.toList());
    }
  });
  LOGGER.info( "Execution plan: {}",
        executionPlanLines
        .stream()
        .collect(Collectors.joining( "\n" ))
  );
}

相关文章