HHH-11907 - Add the getResultStream() default method in org.hibernate.query.Query
This commit is contained in:
parent
ce8aa04078
commit
8d29a4d588
|
@ -1109,4 +1109,15 @@ public interface Query<R> extends TypedQuery<R>, org.hibernate.Query<R>, CommonQ
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.test.stream.basic;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Tuple;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class JpaStreamTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] { MyEntity.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-11907")
|
||||
public void testQueryStream() {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
MyEntity e= new MyEntity();
|
||||
e.id = 1;
|
||||
e.name = "Test";
|
||||
session.persist( e );
|
||||
} );
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
// Test stream query without type.
|
||||
Object result = session.createQuery( "From MyEntity" ).getResultStream().findFirst().orElse( null );
|
||||
assertTyping( MyEntity.class, result );
|
||||
|
||||
// Test stream query with type.
|
||||
result = session.createQuery( "From MyEntity", MyEntity.class ).getResultStream().findFirst().orElse( null );
|
||||
assertTyping( MyEntity.class, result );
|
||||
|
||||
// Test stream query using forEach
|
||||
session.createQuery( "From MyEntity", MyEntity.class ).getResultStream().forEach( i -> {
|
||||
assertTyping( MyEntity.class, i );
|
||||
} );
|
||||
|
||||
Stream<Object[]> data = session.createQuery( "SELECT me.id, me.name FROM MyEntity me" ).getResultStream();
|
||||
data.forEach( i -> {
|
||||
assertTyping( Integer.class, i[0] );
|
||||
assertTyping( String.class, i[1] );
|
||||
});
|
||||
} );
|
||||
}
|
||||
|
||||
@Entity(name = "MyEntity")
|
||||
@Table(name="MyEntity")
|
||||
public static class MyEntity {
|
||||
@Id
|
||||
public Integer id;
|
||||
public String name;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue