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

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

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

NativeQuery.getResultList介绍

暂无

代码示例

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

@Test
public void test_sql_hibernate_multiple_scalar_values_dto_hibernate_named_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-multiple-scalar-values-dto-hibernate-named-query-example[]
    List<PersonPhoneCount> personNames = session.getNamedNativeQuery(
      "get_person_phone_count")
    .getResultList();
    //end::sql-hibernate-multiple-scalar-values-dto-hibernate-named-query-example[]
    assertEquals(2, personNames.size());
    assertEquals(1, personNames.stream().filter( person -> person.getName().equals( "John Doe" ) ).map( PersonPhoneCount::getPhoneCount ).findAny().get().intValue());
    assertEquals(2, personNames.stream().filter( person -> person.getName().equals( "Mrs. John Doe" ) ).map( PersonPhoneCount::getPhoneCount ).findAny().get().intValue());
  });
}

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

@Test
public void setParameterWithWrongTypeShouldNotThrowIllegalArgumentException() {
  doInJPA(this::entityManagerFactory, entityManager -> {
    entityManager.createNativeQuery(
      "select id " +
      "from Event " +
      "where readings = :readings" )
    .unwrap( NativeQuery.class )
    .setParameter( "readings", new String[]{null, "a"}, StringArrayType.INSTANCE )
    .getResultList();
  });
}

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

@Test
public void testNativeSQL() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    List<UUID> books = entityManager.createNativeQuery(
      "select b.id as id " +
      "from Book b " +
      "where b.id = :id")
    .setParameter( "id", book.id )
    .unwrap( NativeQuery.class )
    .addScalar( "id", PostgresUUIDType.INSTANCE )
    .getResultList();
    assertEquals(1, books.size());
  } );
}

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

@Test
public void testNativeSQL() {
  doInHibernate( this::sessionFactory, session -> {
    List<Array> emails = session.createNativeQuery(
      "select u.emailAddresses from CorporateUser u where u.userName = :name" )
    .setParameter( "name", "Vlad" )
    .getResultList();
    assertEquals( 1, emails.size() );
  } );
}

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

@Test
public void testNativeSQLAddScalar() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    List<Inet> inets = entityManager.createNativeQuery(
      "select e.ip as ip " +
      "from Event e " +
      "where e.id = :id" )
    .setParameter( "id", 1L )
    .unwrap( NativeQuery.class )
    .addScalar( "ip", InetType.INSTANCE )
    .getResultList();
    assertEquals( 1, inets.size() );
    assertEquals( "192.168.0.123/24", inets.get( 0 ).getAddress() );
  } );
}

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

@Test
@TestForIssue(jiraKey = "HHH-8916")
public void testPaginationWithCTEQueryNoOffset() {
  // This used to throw SQLServerException: Incorrect syntax near 'SEL'
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 0; i < 20; ++i ) {
      session.persist( new Product2( i, "Product" + i ) );
    }
    session.flush();
    session.clear();
    List results = session
        .createNativeQuery( "WITH a AS (SELECT description FROM Product2) SELECT description FROM a" )
        .setMaxResults( 10 )
        .getResultList();
    assertEquals( 10, results.size() );
    assertEquals( String.class, results.get( 0 ).getClass() );
  } );
}

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

@Test
@TestForIssue(jiraKey = "HHH-8916")
public void testPaginationWithCTEQueryNoOffsetNewLine() {
  // This used to throw SQLServerException: Incorrect syntax near 'SEL'
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 0; i < 20; ++i ) {
      session.persist( new Product2( i, "Product" + i ) );
    }
    session.flush();
    session.clear();
    List results = session
        .createNativeQuery(
          "WITH a AS (\n" +
          "\tSELECT description \n" +
          "\tFROM Product2\n" +
          ") \n" +
          "SELECT description FROM a" )
        .setMaxResults( 10 )
        .getResultList();
    assertEquals( 10, results.size() );
    assertEquals( String.class, results.get( 0 ).getClass() );
  } );
}

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

@Test
@TestForIssue(jiraKey = "HHH-8916")
public void testPaginationWithCTEQueryWithOffset() {
  // This used to throw a StringIndexOutOfBoundsException
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 0; i < 20; ++i ) {
      session.persist( new Product2( i, "Product" + i ) );
    }
    session.flush();
    session.clear();
    List results = session
        .createNativeQuery( "WITH a AS (SELECT id, description FROM Product2) SELECT id, description FROM a" )
        .setFirstResult( 5 )
        .setMaxResults( 10 )
        .getResultList();
    assertEquals( 10, results.size() );
    final Object[] row = (Object[]) results.get( 0 );
    assertEquals( 2, row.length );
    assertEquals( Integer.class, row[ 0 ].getClass() );
    assertEquals( String.class, row[ 1 ].getClass() );
  } );
}

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

@Test
@TestForIssue(jiraKey = "HHH-8916")
public void testPaginationWithCTEQueryWithOffsetAndOrderBy() {
  // This used to throw a StringIndexOutOfBoundsException
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 0; i < 20; ++i ) {
      session.persist( new Product2( i, "Product" + i ) );
    }
    session.flush();
    session.clear();
    List results = session
        .createNativeQuery( "WITH a AS (SELECT id, description FROM Product2) SELECT id, description FROM a ORDER BY id DESC" )
        .setFirstResult( 5 )
        .setMaxResults( 10 )
        .getResultList();
    assertEquals( 10, results.size() );
    final Object[] row = (Object[]) results.get( 0 );
    assertEquals( 2, row.length );
    assertEquals( Integer.class, row[ 0 ].getClass() );
    assertEquals( String.class, row[ 1 ].getClass() );
    assertEquals( 14, row[0] );
    assertEquals( "Product14", row[1] );
  } );
}

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

@Test
public void testResultListWithAddEntityWithoutProjection() {
  inTransaction( session -> {
    List<Movie> movies = session.createNativeQuery( getNativeQueryWithoutProjection() )
      .addEntity( Movie.class )
      .getResultList();
    assertThat( movies.get( 0 ) ).isEqualTo( originalMovie );
  } );
}

代码示例来源:origin: vladmihalcea/high-performance-java-persistence

.addEntity("post", Post.class)
.addJoin("tag", "post.tags")
.getResultList();

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

@Test
@TestForIssue( jiraKey = "OGM-1375" )
public void testResultListWithAddEntityWithProjection() {
  thrown.expect( PersistenceException.class );
  thrown.expectMessage( PROJECTION_ADD_ENTITY_MESSAGE );
  inTransaction( session -> {
    session.createNativeQuery( getNativeQueryWithProjectionYearAuthor() )
      .addEntity( Movie.class )
      .getResultList();
  } );
}

相关文章