本文整理了Java中org.hibernate.query.Query.setMaxResults
方法的一些代码示例,展示了Query.setMaxResults
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.setMaxResults
方法的具体详情如下:
包路径:org.hibernate.query.Query
类名称:Query
方法名:setMaxResults
暂无
代码示例来源:origin: hibernate/hibernate-orm
@SuppressWarnings("unchecked")
private List<BatchJob> nextFiveBatchJobs(Session session, Integer maxResult) {
Query query = session.createQuery(
"select j from BatchJob j", BatchJob.class )
.setMaxResults( maxResult )
.unwrap( Query.class );
applySkipLocked(query);
return query.list();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testQuerySetMaxResults() {
final Session session = sessionFactory().openSession();
final Query qry = session.createQuery( "select i from Item i" );
session.close();
assertThat( session.isOpen(), CoreMatchers.is ( false ) );
try {
qry.setMaxResults( 1 );
fail( "Expecting call to fail" );
}
catch (IllegalStateException expected) {
}
}
@Test
代码示例来源:origin: hibernate/hibernate-orm
@SuppressWarnings( {"unchecked"})
@Test
public void testDistinctSelectWithJoin() {
feedDatabase();
Session s = openSession();
List<Entry> entries = s.createQuery("select distinct e from Entry e join e.tags t where t.surrogate != null order by e.name").setFirstResult(10).setMaxResults(5).list();
// System.out.println(entries);
Entry firstEntry = entries.remove(0);
assertFalse("The list of entries should not contain dublicated Entry objects as we've done a distinct select", entries.contains(firstEntry));
s.close();
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@RequiresDialectFeature(
value = DialectChecks.SupportLimitAndOffsetCheck.class,
comment = "dialect does not support offset and limit combo"
)
public void testSimpleSelectWithLimitAndOffset() throws Exception {
// just checking correctness of param binding code...
Session session = openSession();
Transaction t = session.beginTransaction();
session.createQuery( "from Animal" )
.setFirstResult( 2 )
.setMaxResults( 1 )
.list();
t.commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testLimitZero() throws Exception {
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
Iterator iter = s.createQuery( "from Person p" )
.setMaxResults( 0 )
.iterate();
int count = 0;
while ( iter.hasNext() ) {
iter.next();
count++;
}
assertEquals( 0, count );
final List list = s.createQuery( "select p from Person p" )
.setMaxResults( 0 )
.setFirstResult( 2 )
.list();
assertTrue( list.isEmpty() );
} );
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithMaxResultsThenNoFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 1, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithMaxResultsAndOrderByThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p order by p.id", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@TestForIssue( jiraKey = "HHH-11726" )
public void testDistinctPassThroughFalse() {
doInHibernate( this::sessionFactory, session -> {
sqlStatementInterceptor.getSqlQueries().clear();
List<Person> persons = session.createQuery(
"select distinct p from Person p left join fetch p.phones ")
.setHint(QueryHints.HINT_PASS_DISTINCT_THROUGH, false)
.setMaxResults(5)
.getResultList();
assertEquals(1, persons.size());
String sqlQuery = sqlStatementInterceptor.getSqlQueries().getLast();
assertFalse(sqlQuery.contains(" distinct "));
});
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@TestForIssue( jiraKey = "HHH-11726" )
public void testDistinctPassThroughTrue() {
doInHibernate( this::sessionFactory, session -> {
sqlStatementInterceptor.getSqlQueries().clear();
List<Person> persons = session.createQuery(
"select distinct p from Person p left join fetch p.phones ")
.setHint(QueryHints.HINT_PASS_DISTINCT_THROUGH, true)
.setMaxResults(5)
.getResultList();
assertEquals(1, persons.size());
String sqlQuery = sqlStatementInterceptor.getSqlQueries().getLast();
assertTrue(sqlQuery.contains(" distinct "));
});
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@SkipForDialect(
value = IngresDialect.class,
jiraKey = "HHH-4961",
comment = "Ingres does not support this scoping in 9.3"
)
public void testPaginationWithPolymorphicQuery() {
Session s = openSession();
s.beginTransaction();
Human h = new Human();
h.setName( new Name( "Steve", null, "Ebersole" ) );
s.save( h );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
List results = s.createQuery( "from java.lang.Object" ).setMaxResults( 2 ).list();
assertEquals( 1, results.size() );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.delete( h );
s.getTransaction().commit();
s.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPaginationWithHQLProjection() {
doInHibernate( this::sessionFactory, session -> {
for ( int i = 10; i < 20; i++ ) {
session.persist( new Product2( i, "Kit" + i ) );
}
session.flush();
session.clear();
List list = session.createQuery(
"select id, description as descr, (select max(id) from Product2) as maximum from Product2"
).setFirstResult( 2 ).setMaxResults( 2 ).list();
assertEquals( 19, ( (Object[]) list.get( 1 ) )[2] );
list = session.createQuery( "select id, description, (select max(id) from Product2) from Product2 order by id" )
.setFirstResult( 2 ).setMaxResults( 2 ).list();
assertEquals( 2, list.size() );
assertArrayEquals( new Object[] {12, "Kit12", 19}, (Object[]) list.get( 0 ));
assertArrayEquals( new Object[] {13, "Kit13", 19}, (Object[]) list.get( 1 ));
} );
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithFirstResultsThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.setFirstResult( 40 )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p order by p.id", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( true ) )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithDistinctWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select distinct p from Product p where p.id > 40" )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( true ) )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@TestForIssue(jiraKey = "HHH-7370")
public void testPaginationWithMaxOnly() {
doInHibernate( this::sessionFactory, session -> {
for ( int i = 30; i < 40; i++ ) {
session.persist( new Product2( i, "Kit" + i ) );
}
session.flush();
session.clear();
List list = session.createQuery( "from Product2 order by id" ).setFirstResult( 0 ).setMaxResults( 2 ).list();
assertEquals( Arrays.asList( new Product2( 30, "Kit30" ), new Product2( 31, "Kit31" ) ), list );
list = session.createQuery( "select distinct p from Product2 p order by p.id" ).setMaxResults( 1 ).list();
assertEquals( Collections.singletonList( new Product2( 30, "Kit30" ) ), list );
} );
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithFirstResultsWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( true ) )
.setFirstResult( 40 )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@TestForIssue(jiraKey = "HHH-7781")
public void testPaginationWithCastOperator() {
doInHibernate( this::sessionFactory, session -> {
for ( int i = 40; i < 50; i++ ) {
session.persist( new Product2( i, "Kit" + i ) );
}
session.flush();
session.clear();
List<Object[]> list = session.createQuery( "select p.id, cast(p.id as string) as string_id from Product2 p order by p.id" )
.setFirstResult( 1 ).setMaxResults( 2 ).list();
assertEquals( 2, list.size() );
assertArrayEquals( new Object[] { 41, "41" }, list.get( 0 ) );
assertArrayEquals( new Object[] { 42, "42" }, list.get( 1 ) );
} );
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPaginationWithHQL() {
doInHibernate( this::sessionFactory, session -> {
for ( int i = 20; i < 30; i++ ) {
session.persist( new Product2( i, "Kit" + i ) );
}
session.flush();
session.clear();
List list = session.createQuery( "from Product2 order by id" ).setFirstResult( 3 ).setMaxResults( 2 ).list();
assertEquals( Arrays.asList( new Product2( 23, "Kit23" ), new Product2( 24, "Kit24" ) ), list );
} );
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyDisablingFollowOnLockingThenFails() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
try {
List<Product> products =
session.createQuery(
"select p from Product p order by p.id",
Product.class
)
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( false ) )
.setMaxResults( 10 )
.getResultList();
fail( "Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled" );
}
catch ( PersistenceException expected ) {
assertEquals(
SQLGrammarException.class,
expected.getCause().getClass()
);
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithFirstResultsWhileExplicitlyDisablingFollowOnLockingThenFails() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
try {
List<Product> products =
session.createQuery(
"select p from Product p", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( false ) )
.setFirstResult( 40 )
.setMaxResults( 10 )
.getResultList();
fail( "Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled" );
}
catch ( PersistenceException expected ) {
assertEquals(
SQLGrammarException.class,
expected.getCause().getClass()
);
}
}
内容来源于网络,如有侵权,请联系作者删除!