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

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

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

Query.getResultList介绍

暂无

代码示例

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

/**
 * Get the results of a {@link CriteriaQuery} query.
 *
 * @param criteria the {@link CriteriaQuery} query to run
 * @return the list of matched query results
 */
protected List<E> list(CriteriaQuery<E> criteria) throws HibernateException {
  return currentSession().createQuery(requireNonNull(criteria)).getResultList();
}

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

/**
 * Convenience method to return a single instance that matches the criteria query,
 * or null if the criteria returns no results.
 *
 * @param criteriaQuery the {@link CriteriaQuery} query to run
 * @return the single result or {@code null}
 * @throws HibernateException if there is more than one matching result
 */
protected E uniqueResult(CriteriaQuery<E> criteriaQuery) throws HibernateException {
  return AbstractProducedQuery.uniqueElement(
    currentSession()
      .createQuery(requireNonNull(criteriaQuery))
      .getResultList()
  );
}

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

@Test
public void testQueryingSingle() {
  doInHibernate( this::sessionFactory, s -> {
    final String base = "from PolymorphicPropertyBase p left join ";
    s.createQuery( base + "p.base b left join b.relation1 " ).getResultList();
    s.createQuery( base + "p.base b left join b.relation2 " ).getResultList();
    s.createQuery( base + "p.baseEmbeddable.embeddedRelation1 b left join b.relation1" ).getResultList();
    s.createQuery( base + "p.baseEmbeddable.embeddedRelation2 b left join b.relation2" ).getResultList();
    s.createQuery( base + "p.baseEmbeddable.embeddedBase b left join b.relation1" ).getResultList();
    s.createQuery( base + "p.baseEmbeddable.embeddedBase b left join b.relation2" ).getResultList();
  } );
}

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

@Test
public void testQueryingMultiple() {
  doInHibernate( this::sessionFactory, s -> {
    final String base = "from PolymorphicPropertyBase p left join ";
    s.createQuery( base + "p.base b left join b.relation1 left join b.relation2" ).getResultList();
    s.createQuery( base + "p.base b left join b.relation2 left join b.relation1" ).getResultList();
    s.createQuery( base + "p.baseEmbeddable.embeddedBase b left join b.relation1 left join b.relation2" ).getResultList();
    s.createQuery( base + "p.baseEmbeddable.embeddedBase b left join b.relation2 left join b.relation1" ).getResultList();
  } );
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testCurrentSession() {
  String firstName = "Tony";
  insertPerson(firstName);
  Query q = sessionFactory.getCurrentSession().createQuery("select p from Person as p");
  List<Person> people = q.getResultList();
  assertEquals(1, people.size());
  assertEquals(firstName, people.get(0).getFirstName());
  assertSame(applicationContext, people.get(0).postLoaded);
}

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

@Test
public void testSelect() {
  doInHibernate( this::sessionFactory, session -> {
    session.createQuery(
      "select a.id " +
      "from A a " +
      "where exists (" +
      "	select 1 " +
      "	from B b " +
      "	where b.id = a.id" +
      ")"
    )
    .getResultList();
  } );
}

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

@Test
public void testOneToOneLazyLoading() {
  doInHibernate( this::sessionFactory, s -> {
    PostDetails post = (PostDetails) s.createQuery("select a from PostDetails a").getResultList().get(0);
    assertFalse(isInitialized(post.post));
  } );
}

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

@Test
public void testPessimisticLockWithUnionThenFollowOnLocking() {
  final Session session = openSession();
  session.beginTransaction();
  sqlStatementInterceptor.getSqlQueries().clear();
  List<Vehicle> vehicles = session.createQuery( "select v from Vehicle v" )
    .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
    .getResultList();
  assertEquals( 3, vehicles.size() );
  assertEquals( 4, sqlStatementInterceptor.getSqlQueries().size() );
  session.getTransaction().commit();
  session.close();
}

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

@Test
public void test() {
  doInHibernate( this::sessionFactory, session -> {
    List<CorporateUser> users = session.createQuery(
      "select u from CorporateUser u where u.emailAddresses = :address", CorporateUser.class )
    .setParameter( "address", new Array(), ArrayType.INSTANCE )
    .getResultList();
    assertTrue( users.isEmpty() );
  } );
}

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

@Test
public void testPessimisticLockWithNamedQueryExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
  final Session session = openSession();
  session.beginTransaction();
  sqlStatementInterceptor.getSqlQueries().clear();
  List<Product> products = session.createNamedQuery(
    "product_by_name", Product.class )
  .getResultList();
  assertEquals( 50, products.size() );
  assertEquals( 51, sqlStatementInterceptor.getSqlQueries().size() );
  session.getTransaction().commit();
  session.close();
}

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

@Test
public void testMakeEnvelope() {
  Long addressId = insertEvent( c( 10, 5 ) );
  doInHibernate( this::sessionFactory, session -> {
    List<Event> events = session.createQuery(
        "select e " +
            "from Event e " +
            "where within(e.location, makeenvelope(0, 0, 11, 11, -1 )) = true", Event.class )
        .getResultList();
    assertEquals( 1, events.size() );
  } );
}

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

@Test
public void testPessimisticLockWithCountDistinctThenFollowOnLocking() {
  final Session session = openSession();
  session.beginTransaction();
  sqlStatementInterceptor.getSqlQueries().clear();
  List<Product> products = session.createQuery(
    "select p from Product p where ( select count(distinct p1.id) from Product p1 ) > 0 ", Product.class )
  .setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ).setFollowOnLocking( false ) )
  .getResultList();
  assertEquals( 50, products.size() );
  assertEquals( 1, sqlStatementInterceptor.getSqlQueries().size() );
  session.getTransaction().commit();
  session.close();
}

代码示例来源: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
public void testStoredProcedureOutParameter() {
  doInHibernate( this::sessionFactory, session -> {
    List<Object[]> persons = session
        .createNamedQuery(
            "getPerson")
        .setParameter(1, 1L)
        .getResultList();
    assertEquals(1, persons.size());
  } );
}

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

@Test
public void testBuffer() {
  Long addressId = insertEvent( c( 10, 5 ) );
  doInHibernate( this::sessionFactory, session -> {
    List<Event> events = session.createQuery(
        "select e " +
            "from Event e " +
            "where within( e.location, buffer(:window, 100)) = true", Event.class )
        .setParameter( "window", window )
        .getResultList();
    assertEquals( 1, events.size() );
  } );
}

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

@Test
public void HHH_10463_TestCoalesce() {
  doInHibernate( this::sessionFactory, session -> {
    Query query = session.createQuery( "from Person p where p.name = coalesce(:name , p.name) ");
    query.setParameter("name", "Johannes");
    List<Person> resultList = query.getResultList();
    assertThat(resultList, hasItem(person));
  } );
}

代码示例来源: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
@RequiresDialect( PostgreSQL81Dialect.class )
public void HHH_10463_NullInCoalesce_PostgreSQL_Workaround() {
  doInHibernate( this::sessionFactory, session -> {
    Query query = session.createQuery("from Person p where p.name = coalesce(cast( :name as string) , p.name) ");
    query.setParameter("name", null);
    List<Person> resultList = query.getResultList();
    assertThat(resultList, hasItem(person));
  } );
}

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

@Test
@SkipForDialect( jiraKey = "HHH-10463", value =  PostgreSQL81Dialect.class)
@SkipForDialect( jiraKey = "HHH-10463", value =  Oracle8iDialect.class)
public void HHH_10463_NullInCoalesce() {
  doInHibernate( this::sessionFactory, session -> {
    Query query = session.createQuery("from Person p where p.name = coalesce(:name, p.name) ");
    query.setParameter("name", null);
    List<Person> resultList = query.getResultList();
    assertThat(resultList, hasItem(person));
  } );
}

相关文章