@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(); }
@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
@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(); } }
@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(); }
@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() ); } ); }
@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(); }
@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(); }
@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 ")); }); }
@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 ")); }); }
@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(); }
@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 )); } ); }
@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(); }
@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(); }
@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(); }
@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 ); } ); }
@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(); }
@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 ) ); } ); }
@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 ); } ); }
@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() ); } }
@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() ); } }