org.hibernate.query.Query.list()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(316)

本文整理了Java中org.hibernate.query.Query.list方法的一些代码示例,展示了Query.list的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.list方法的具体详情如下:
包路径:org.hibernate.query.Query
类名称:Query
方法名:list

Query.list介绍

[英]Return the query results as a List. If the query contains multiple results per row, the results are returned in an instance of Object[].
[中]以列表形式返回查询结果。如果查询每行包含多个结果,则结果将在对象[]的实例中返回。

代码示例

代码示例来源:origin: dropwizard/dropwizard

  1. /**
  2. * Get the results of a query.
  3. *
  4. * @param query the query to run
  5. * @return the list of matched query results
  6. * @see Query#list()
  7. */
  8. protected List<E> list(Query<E> query) throws HibernateException {
  9. return requireNonNull(query).list();
  10. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testNarrow() throws Exception {
  3. Session s = openSession();
  4. Transaction t = s.beginTransaction();
  5. s.createQuery("from E e join e.reverse as b where b.count=1").list();
  6. s.createQuery("from E e join e.as as b where b.count=1").list();
  7. t.commit();
  8. s.close();
  9. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Override
  2. protected void cleanupTestData() throws Exception {
  3. Session s = openSession();
  4. s.beginTransaction();
  5. List list = s.createQuery( "from java.lang.Object" ).list();
  6. for ( Object obj : list ) {
  7. s.delete( obj );
  8. }
  9. s.getTransaction().commit();
  10. s.close();
  11. }

代码示例来源:origin: hibernate/hibernate-orm

  1. private void assertResultSize(String hql, int size) {
  2. Session session = openSession();
  3. Transaction txn = session.beginTransaction();
  4. assertEquals( size, session.createQuery(hql).list().size() );
  5. txn.commit();
  6. session.close();
  7. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testSimpleCaseStatementFixture() {
  3. Session s = openSession();
  4. Transaction t = s.beginTransaction();
  5. s.createQuery( "select case p.name when 'Steve' then 'x' else 'y' end from Person p" )
  6. .list();
  7. t.commit();
  8. s.close();
  9. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @SuppressWarnings("unchecked")
  2. private List<DestinationEntity> findDestinationByIds(List<Integer> ids) {
  3. Session session = openSession();
  4. List<DestinationEntity> list = session
  5. .createQuery( "from DestinationEntity de where de.id in (:ids) order by id" )
  6. .setParameterList( "ids", ids ).list();
  7. session.close();
  8. return list;
  9. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testUsageInSelect() {
  3. Session s = openSession();
  4. s.createQuery( "select I from Item i" ).list();
  5. s.close();
  6. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testUsageInJpaInCollectionSyntax() {
  3. Session s = openSession();
  4. s.createQuery( "SELECT DISTINCT object(i) FROM Item I, IN(i.parts) ip where ip.stockNumber = '123'" ).list();
  5. s.close();
  6. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testUsageInDistinct() {
  3. Session s = openSession();
  4. s.createQuery( "select distinct(I) from Item i" ).list();
  5. s.close();
  6. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void basicTest() {
  3. Session s = openSession();
  4. s.createQuery( "select i from Item i where function( 'substring', i.name, 1, 3 ) = 'abc'" )
  5. .list();
  6. s.close();
  7. }
  8. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testNakedImplicitJoins() {
  3. // note: simply performing syntax and column/table resolution checking in the db
  4. Session s = openSession();
  5. s.beginTransaction();
  6. s.createQuery( "from Animal where mother.father.id = 1" ).list();
  7. s.getTransaction().commit();
  8. s.close();
  9. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testJdkEnumStyleEnumConstant() throws Exception {
  3. Session s = openSession();
  4. s.beginTransaction();
  5. s.createQuery( "from Zoo z where z.classification = org.hibernate.test.hql.Classification.LAME" ).list();
  6. s.getTransaction().commit();
  7. s.close();
  8. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testSimpleCaseStatementWithParamResult() {
  3. Session s = openSession();
  4. Transaction t = s.beginTransaction();
  5. s.createQuery( "select case p.name when 'Steve' then :opt1 else p.name end from Person p" )
  6. .setString( "opt1", "x" )
  7. .list();
  8. t.commit();
  9. s.close();
  10. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. @TestForIssue( jiraKey = "HHH-1689, SQM-30" )
  3. public void testSubQueryAsCaseElseResultExpression() {
  4. final String query = "SELECT CASE WHEN l.id > 1 THEN 1 ELSE (SELECT COUNT(r.id) FROM Root r) END FROM Leaf l";
  5. // simple syntax check
  6. Session s = openSession();
  7. s.beginTransaction();
  8. s.createQuery( query ).list();
  9. s.getTransaction().commit();
  10. s.close();
  11. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. @TestForIssue( jiraKey = "HHH-1689, SQM-30" )
  3. public void testSubQueryAsSimpleCaseWhenExpression() {
  4. final String query = "SELECT CASE l.id WHEN (SELECT COUNT(r.id) FROM Root r) THEN 1 ELSE 0 END FROM Leaf l";
  5. // simple syntax check
  6. Session s = openSession();
  7. s.beginTransaction();
  8. s.createQuery( query ).list();
  9. s.getTransaction().commit();
  10. s.close();
  11. }
  12. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. @FailureExpected( jiraKey = "HHH-4883")
  3. public void testJoinAcrossEmbedded() {
  4. // NOTE : this may or may not work now with HHH-4883 fixed,
  5. // but i cannot do this checking until HHH-4599 is done.
  6. Session session = openSession();
  7. session.beginTransaction();
  8. session.createQuery( "from Person p join p.name.aliases a where a.source = 'FBI'" )
  9. .list();
  10. session.getTransaction().commit();
  11. session.close();
  12. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. @TestForIssue(jiraKey = "HHH-13084")
  3. public void testHql() {
  4. doInHibernate( this::sessionFactory, session -> {
  5. assertEquals( 1, session.createQuery( "from Person p where p.id is null", Person.class ).list().size() );
  6. assertEquals( 2, session.createQuery( "from Person p where p.id is not null", Person.class ).list().size() );
  7. assertEquals( 3L, session.createQuery( "select count( p ) from Person p" ).uniqueResult() );
  8. } );
  9. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testEntityPropertySelect() throws Exception {
  3. createTestBaseData();
  4. Session session = openSession();
  5. Transaction t = session.beginTransaction();
  6. List results = session.createQuery( "select a.mother from Animal as a" ).list();
  7. assertTrue( "Incorrect result return type", results.get( 0 ) instanceof Animal );
  8. t.commit();
  9. session.close();
  10. destroyTestBaseData();
  11. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. @TestForIssue(jiraKey = "HHH-13084")
  3. public void testHql() {
  4. doInHibernate( this::sessionFactory, session -> {
  5. assertEquals( 2, session.createQuery( "from Person p where p.id = 0", Person.class ).list().size() );
  6. assertEquals( 3L, session.createQuery( "select count( p ) from Person p" ).uniqueResult() );
  7. } );
  8. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testPaginationWithHQL() {
  3. doInHibernate( this::sessionFactory, session -> {
  4. for ( int i = 20; i < 30; i++ ) {
  5. session.persist( new Product2( i, "Kit" + i ) );
  6. }
  7. session.flush();
  8. session.clear();
  9. List list = session.createQuery( "from Product2 order by id" ).setFirstResult( 3 ).setMaxResults( 2 ).list();
  10. assertEquals( Arrays.asList( new Product2( 23, "Kit23" ), new Product2( 24, "Kit24" ) ), list );
  11. } );
  12. }

相关文章