本文整理了Java中org.hibernate.query.Query.stream
方法的一些代码示例,展示了Query.stream
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.stream
方法的具体详情如下:
包路径:org.hibernate.query.Query
类名称:Query
方法名:stream
[英]Retrieve a Stream over the query results.
In the initial implementation (5.2) this returns a simple sequential Stream. The plan is to return a a smarter stream in 6.x leveraging the SQM model.
You should call java.util.stream.Stream#close() after processing the stream so that the underlying resources are deallocated right away.
[中]通过查询结果检索流。
在最初的实现(5.2)中,它返回一个简单的顺序流。计划是在6小时内返回一个更智能的流。x利用SQM模型。
你应该叫java。util。流动流#close()在处理流后立即释放底层资源。
代码示例来源:origin: hibernate/hibernate-orm
/**
* JPA 2.2 defines the {@code getResultStream} method so to get a {@link Stream} from the JDBC {@link java.sql.ResultSet}.
*
* Hibernate 5.2 already defines the {@link Query#stream()} method, so {@code getResultStream} can delegate to it.
*
* @return The results Stream
* @since 5.2.11
*/
default Stream<R> getResultStream() {
return stream();
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testSetParameter() throws Exception {
try (Session session = openSession()) {
final Query<TestEntity> query = session.createQuery(
"SELECT e FROM TestEntity e WHERE e.date <= :ts",
TestEntity.class
).setParameter( "ts", new DateAttribute( System.currentTimeMillis() ), TemporalType.TIMESTAMP );
final Stream<TestEntity> stream = query.stream();
assertThat( stream.count(), is( 1L ) );
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testStreamMethod() {
final String entityName = "expected";
insertTestEntity( entityName );
try (Session session = openSession()) {
final CriteriaQuery<TestEntity> query = createTestEntityCriteriaQuery(
entityName,
session
);
final Stream<TestEntity> stream = session.createQuery( query ).stream();
assertThat( stream.count(), is( 1L ) );
}
}
代码示例来源:origin: hibernate/hibernate-orm
Object result = session.createQuery( "From MyEntity" ).stream().findFirst().orElse( null );
assertTyping( MyEntity.class, result );
result = session.createQuery( "From MyEntity", MyEntity.class ).stream().findFirst().orElse( null );
assertTyping( MyEntity.class, result );
session.createQuery( "From MyEntity", MyEntity.class ).stream().forEach( i -> {
assertTyping( MyEntity.class, i );
} );
Stream<Object[]> data = session.createQuery( "SELECT me.id, me.name FROM MyEntity me" ).stream();
data.forEach( i -> {
assertTyping( Integer.class, i[0] );
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void test_hql_api_stream_projection_example() {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::hql-api-stream-projection-example[]
try ( Stream<Object[]> persons = session.createQuery(
"select p.name, p.nickName " +
"from Person p " +
"where p.name like :name" )
.setParameter( "name", "J%" )
.stream() ) {
persons
.map( row -> new PersonNames(
(String) row[0],
(String) row[1] ) )
.forEach( this::process );
}
//end::hql-api-stream-projection-example[]
});
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void test_hql_api_stream_example() {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
//tag::hql-api-stream-example[]
try( Stream<Person> persons = session.createQuery(
"select p " +
"from Person p " +
"where p.name like :name" )
.setParameter( "name", "J%" )
.stream() ) {
Map<Phone, List<Call>> callRegistry = persons
.flatMap( person -> person.getPhones().stream() )
.flatMap( phone -> phone.getCalls().stream() )
.collect( Collectors.groupingBy( Call::getPhone ) );
process(callRegistry);
}
//end::hql-api-stream-example[]
});
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@TestForIssue(jiraKey = "HHH-11743")
public void testTupleStream() {
doInHibernate( this::sessionFactory, session -> {
MyEntity entity = new MyEntity();
entity.id = 2;
entity.name = "an entity";
session.persist( entity );
} );
//test tuple stream using criteria
doInHibernate( this::sessionFactory, session -> {
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = cb.createTupleQuery();
Root<MyEntity> me = criteria.from( MyEntity.class );
criteria.multiselect( me.get( "id" ), me.get( "name" ) );
Stream<Tuple> data = session.createQuery( criteria ).stream();
data.forEach( tuple -> assertTyping( Tuple.class, tuple ) );
} );
//test tuple stream using JPQL
doInHibernate( this::sessionFactory, session -> {
Stream<Tuple> data = session.createQuery( "SELECT me.id, me.name FROM MyEntity me", Tuple.class ).stream();
data.forEach( tuple -> assertTyping( Tuple.class, tuple ) );
} );
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void basicStreamTest() {
Session session = openSession();
session.getTransaction().begin();
// mainly we want to make sure that closing the Stream releases the ScrollableResults too
assertThat( ( (SessionImplementor) session ).getJdbcCoordinator().getLogicalConnection().getResourceRegistry().hasRegisteredResources(), is( false ) );
final Stream<MyEntity> stream = session.createQuery( "from MyEntity", MyEntity.class ).stream();
assertThat( ( (SessionImplementor) session ).getJdbcCoordinator().getLogicalConnection().getResourceRegistry().hasRegisteredResources(), is( true ) );
stream.forEach( System.out::println );
assertThat( ( (SessionImplementor) session ).getJdbcCoordinator().getLogicalConnection().getResourceRegistry().hasRegisteredResources(), is( true ) );
stream.close();
assertThat( ( (SessionImplementor) session ).getJdbcCoordinator().getLogicalConnection().getResourceRegistry().hasRegisteredResources(), is( false ) );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: vladmihalcea/high-performance-java-persistence
private void stream(EntityManager entityManager) {
final AtomicLong sum = new AtomicLong();
try(Stream<Post> postStream = entityManager
.createQuery("select p from Post p", Post.class)
.setMaxResults(resultSetSize)
.unwrap(Query.class)
.stream()) {
postStream.forEach(post -> sum.incrementAndGet());
}
assertEquals(resultSetSize, sum.get());
}
代码示例来源:origin: vladmihalcea/high-performance-java-persistence
private void stream(EntityManager entityManager) {
final AtomicLong sum = new AtomicLong();
try(Stream<Post> postStream = entityManager
.createQuery("select p from Post p", Post.class)
.setMaxResults(resultSetSize)
.setHint(QueryHints.HINT_FETCH_SIZE, Integer.MIN_VALUE)
.unwrap(Query.class)
.stream()) {
postStream.forEach(post -> sum.incrementAndGet());
}
assertEquals(resultSetSize, sum.get());
}
代码示例来源:origin: vladmihalcea/high-performance-java-persistence
@Test
public void testStreamWithoutMaxResult() {
List<Post> posts = doInJPA(entityManager -> {
try(Stream<Post> postStream = entityManager
.createQuery(
"select p " +
"from Post p " +
"order by p.createdOn desc", Post.class)
.unwrap(Query.class)
.stream()
) {
return postStream.limit( 50 ).collect( Collectors.toList() );
}
});
assertEquals(50, posts.size());
}
代码示例来源:origin: org.nuiton.topia/topia-persistence
Query query = prepareQuery(jpaql, parameters);
Stream result = query.stream();
Consumer<T> consumer = row -> {
List singleton = Collections.singletonList(row);
代码示例来源:origin: vladmihalcea/high-performance-java-persistence
@Test
public void testStream() {
List<Post> posts = doInJPA(entityManager -> {
try(Stream<Post> postStream = entityManager
.createQuery(
"select p " +
"from Post p " +
"order by p.createdOn desc", Post.class)
.setHint(QueryHints.HINT_FETCH_SIZE, 50)
.unwrap(Query.class)
.stream()
) {
return postStream
.limit(50)
.collect(Collectors.toList());
}
});
assertEquals(50, posts.size());
}
内容来源于网络,如有侵权,请联系作者删除!