@Test @TestForIssue( jiraKey = "HHH-12362" ) public void testQueryHintAndComment() { sqlStatementInterceptor.clear(); doInHibernate( this::sessionFactory, s -> { Query query = s.createQuery( "FROM QueryHintTest$Employee e WHERE e.department.name = :departmentName" ) .addQueryHint( "ALL_ROWS" ) .setComment( "My_Query" ) .setParameter( "departmentName", "Sales" ); List results = query.list(); assertEquals(results.size(), 2); } ); sqlStatementInterceptor.assertExecutedCount( 1 ); assertTrue( sqlStatementInterceptor.getSqlQueries().get( 0 ).contains( "/* My_Query */ select /*+ ALL_ROWS */" ) ); sqlStatementInterceptor.clear(); }
@Test public void test_hql_api_basic_usage_example() { doInJPA( this::entityManagerFactory, entityManager -> { Session session = entityManager.unwrap( Session.class ); //tag::hql-api-basic-usage-example[] org.hibernate.query.Query query = session.createQuery( "select p " + "from Person p " + "where p.name like :name" ) // timeout - in seconds .setTimeout( 2 ) // write to L2 caches, but do not read from them .setCacheMode( CacheMode.REFRESH ) // assuming query cache was enabled for the SessionFactory .setCacheable( true ) // add a comment to the generated SQL if enabled via the hibernate.use_sql_comments configuration property .setComment( "+ INDEX(p idx_person_name)" ); //end::hql-api-basic-usage-example[] }); }
protected void setQueryProperties(Query query) { if ( maxResults != null ) { query.setMaxResults( maxResults ); } if ( firstResult != null ) { query.setFirstResult( firstResult ); } if ( cacheable != null ) { query.setCacheable( cacheable ); } if ( cacheRegion != null ) { query.setCacheRegion( cacheRegion ); } if ( comment != null ) { query.setComment( comment ); } if ( flushMode != null ) { query.setFlushMode( flushMode ); } if ( cacheMode != null ) { query.setCacheMode( cacheMode ); } if ( timeout != null ) { query.setTimeout( timeout ); } if ( lockOptions != null && lockOptions.getLockMode() != LockMode.NONE ) { query.setLockMode( REFERENCED_ENTITY_ALIAS, lockOptions.getLockMode() ); } }
.setComment( "this is a test" ) .addQueryHint( "ALL_ROWS" ) .setParameter( "departmentName", "Sales" );
query = s.createQuery( "FROM QueryHintSQLServer2012Test$Employee e WHERE e.department.name = :departmentName" ).setComment( "this is a test" ) .addQueryHint( "MAXDOP 2" ) .setParameter( "departmentName", "Sales" );
protected void initQueryFromNamedDefinition(Query query, NamedQueryDefinition nqd) { // todo : cacheable and readonly should be Boolean rather than boolean... query.setCacheable( nqd.isCacheable() ); query.setCacheRegion( nqd.getCacheRegion() ); query.setReadOnly( nqd.isReadOnly() ); if ( nqd.getTimeout() != null ) { query.setTimeout( nqd.getTimeout() ); } if ( nqd.getFetchSize() != null ) { query.setFetchSize( nqd.getFetchSize() ); } if ( nqd.getCacheMode() != null ) { query.setCacheMode( nqd.getCacheMode() ); } if ( nqd.getComment() != null ) { query.setComment( nqd.getComment() ); } if ( nqd.getFirstResult() != null ) { query.setFirstResult( nqd.getFirstResult() ); } if ( nqd.getMaxResults() != null ) { query.setMaxResults( nqd.getMaxResults() ); } if ( nqd.getFlushMode() != null ) { query.setHibernateFlushMode( nqd.getFlushMode() ); } }
.unwrap(org.hibernate.query.Query.class) .addQueryHint("GATHER_PLAN_STATISTICS") .setComment("POST_WITH_BINGO_COMMENTS") .getResultList();
query.setComment( comment );