/** * Get the results of a query. * * @param query the query to run * @return the list of matched query results * @see Query#list() */ protected List<E> list(Query<E> query) throws HibernateException { return requireNonNull(query).list(); }
@Override protected void cleanupTestData() throws Exception { Session s = openSession(); s.beginTransaction(); List list = s.createQuery( "from java.lang.Object" ).list(); for ( Object obj : list ) { s.delete( obj ); } s.getTransaction().commit(); s.close(); }
private void assertResultSize(String hql, int size) { Session session = openSession(); Transaction txn = session.beginTransaction(); assertEquals( size, session.createQuery(hql).list().size() ); txn.commit(); session.close(); }
@Test public void testSimpleCaseStatementFixture() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery( "select case p.name when 'Steve' then 'x' else 'y' end from Person p" ) .list(); t.commit(); s.close(); }
@SuppressWarnings("unchecked") private List<DestinationEntity> findDestinationByIds(List<Integer> ids) { Session session = openSession(); List<DestinationEntity> list = session .createQuery( "from DestinationEntity de where de.id in (:ids) order by id" ) .setParameterList( "ids", ids ).list(); session.close(); return list; }
@Test public void testUsageInSelect() { Session s = openSession(); s.createQuery( "select I from Item i" ).list(); s.close(); }
@Test public void testUsageInJpaInCollectionSyntax() { Session s = openSession(); s.createQuery( "SELECT DISTINCT object(i) FROM Item I, IN(i.parts) ip where ip.stockNumber = '123'" ).list(); s.close(); }
@Test public void testUsageInDistinct() { Session s = openSession(); s.createQuery( "select distinct(I) from Item i" ).list(); s.close(); }
@Test public void basicTest() { Session s = openSession(); s.createQuery( "select i from Item i where function( 'substring', i.name, 1, 3 ) = 'abc'" ) .list(); s.close(); } }
@Test public void testNakedImplicitJoins() { // note: simply performing syntax and column/table resolution checking in the db Session s = openSession(); s.beginTransaction(); s.createQuery( "from Animal where mother.father.id = 1" ).list(); s.getTransaction().commit(); s.close(); }
@Test public void testJdkEnumStyleEnumConstant() throws Exception { Session s = openSession(); s.beginTransaction(); s.createQuery( "from Zoo z where z.classification = org.hibernate.test.hql.Classification.LAME" ).list(); s.getTransaction().commit(); s.close(); }
@Test public void testSimpleCaseStatementWithParamResult() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery( "select case p.name when 'Steve' then :opt1 else p.name end from Person p" ) .setString( "opt1", "x" ) .list(); t.commit(); s.close(); }
@Test @TestForIssue( jiraKey = "HHH-1689, SQM-30" ) public void testSubQueryAsCaseElseResultExpression() { final String query = "SELECT CASE WHEN l.id > 1 THEN 1 ELSE (SELECT COUNT(r.id) FROM Root r) END FROM Leaf l"; // simple syntax check Session s = openSession(); s.beginTransaction(); s.createQuery( query ).list(); s.getTransaction().commit(); s.close(); }
@Test @TestForIssue( jiraKey = "HHH-1689, SQM-30" ) public void testSubQueryAsSimpleCaseWhenExpression() { final String query = "SELECT CASE l.id WHEN (SELECT COUNT(r.id) FROM Root r) THEN 1 ELSE 0 END FROM Leaf l"; // simple syntax check Session s = openSession(); s.beginTransaction(); s.createQuery( query ).list(); s.getTransaction().commit(); s.close(); } }
@Test @FailureExpected( jiraKey = "HHH-4883") public void testJoinAcrossEmbedded() { // NOTE : this may or may not work now with HHH-4883 fixed, // but i cannot do this checking until HHH-4599 is done. Session session = openSession(); session.beginTransaction(); session.createQuery( "from Person p join p.name.aliases a where a.source = 'FBI'" ) .list(); session.getTransaction().commit(); session.close(); }
@Test @TestForIssue(jiraKey = "HHH-13084") public void testHql() { doInHibernate( this::sessionFactory, session -> { assertEquals( 1, session.createQuery( "from Person p where p.id is null", Person.class ).list().size() ); assertEquals( 2, session.createQuery( "from Person p where p.id is not null", Person.class ).list().size() ); assertEquals( 3L, session.createQuery( "select count( p ) from Person p" ).uniqueResult() ); } ); }
@Test public void testEntityPropertySelect() throws Exception { createTestBaseData(); Session session = openSession(); Transaction t = session.beginTransaction(); List results = session.createQuery( "select a.mother from Animal as a" ).list(); assertTrue( "Incorrect result return type", results.get( 0 ) instanceof Animal ); t.commit(); session.close(); destroyTestBaseData(); }
@Test @TestForIssue(jiraKey = "HHH-13084") public void testHql() { doInHibernate( this::sessionFactory, session -> { assertEquals( 2, session.createQuery( "from Person p where p.id = 0", Person.class ).list().size() ); assertEquals( 3L, session.createQuery( "select count( p ) from Person p" ).uniqueResult() ); } ); }
@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 ); } ); }