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

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

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

NativeQuery.list介绍

暂无

代码示例

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

@Override
  protected Result check() throws Exception {
    return timeBoundHealthCheck.check(() -> {
      try (Session session = sessionFactory.openSession()) {
        final Transaction txn = session.beginTransaction();
        try {
          session.createNativeQuery(validationQuery).list();
          txn.commit();
        } catch (Exception e) {
          if (txn.getStatus().canRollback()) {
            txn.rollback();
          }
          throw e;
        }
      }
      return Result.healthy();
    });
  }
}

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

private List getCollectionElementRows(int id) {
  return doInHibernate(
      this::sessionFactory, session -> {
        return session.createNativeQuery(
            "SELECT aCollection FROM AnEntity_aCollection where AnEntity_id = " + id
        ).list();
      }
  );
}

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

private List<?> getCollectionElementRows(int id) {
  return doInHibernate(
      this::sessionFactory, session -> {
        return session.createNativeQuery(
            "SELECT aCollection FROM AnEntity_aCollection where AnEntity_id = " + id
        ).list();
      }
  );
}

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

private List getCollectionElementRows(int id) {
  return doInHibernate(
      this::sessionFactory, session -> {
        return session.createNativeQuery(
            "SELECT aCollection FROM AnEntity_aCollection where AnEntity_id = " + id
        ).list();
      }
  );
}

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

private List getCollectionElementRows(int id) {
  return doInHibernate(
      this::sessionFactory, session -> {
        return session.createNativeQuery(
            "SELECT aCollection FROM AnEntity_aCollection where AnEntity_id = " + id
        ).list();
      }
  );
}

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

private List<?> getCollectionElementRows(int id) {
  return doInHibernate(
      this::sessionFactory, session -> {
        return session.createNativeQuery(
            "SELECT aCollection FROM AnEntity_aCollection where AnEntity_id = " + id
        ).list();
      }
  );
}

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

@Test
@FailureExpected(jiraKey = "HHH-2225")
public void testNativeQueryWithFormulaAttributeWithoutAlias() {
  String sql = "select TABLE_NAME , sysdate() from all_tables  where TABLE_NAME = 'AUDIT_ACTIONS' ";
  Session s = openSession();
  s.beginTransaction();
  s.createSQLQuery( sql ).addEntity( "t", AllTables.class ).list();
  s.getTransaction().commit();
  s.close();
}

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

@Test
public void test_sql_hibernate_query_scalar_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-all-columns-scalar-query-example[]
    List<Object[]> persons = session.createNativeQuery(
      "SELECT * FROM Person" )
    .list();
    //end::sql-hibernate-all-columns-scalar-query-example[]
    assertEquals(3, persons.size());
  });
}

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

@Test
@TestForIssue(jiraKey = "HHH-7369")
public void testPaginationWithScalarQuery() throws Exception {
  doInHibernate( this::sessionFactory, session -> {
    for ( int i = 0; i < 10; i++ ) {
      session.persist( new Product2( i, "Kit" + i ) );
    }
    session.flush();
    session.clear();
    List list = session.createNativeQuery( "select id from Product2 where description like 'Kit%' order by id" ).list();
    assertEquals(Integer.class, list.get(0).getClass()); // scalar result is an Integer
    list = session.createNativeQuery( "select id from Product2 where description like 'Kit%' order by id" ).setFirstResult( 2 ).setMaxResults( 2 ).list();
    assertEquals(Integer.class, list.get(0).getClass()); // this fails without patch, as result suddenly has become an array
    // same once again with alias
    list = session.createNativeQuery( "select id as myint from Product2 where description like 'Kit%' order by id asc" ).setFirstResult( 2 ).setMaxResults( 2 ).list();
    assertEquals(Integer.class, list.get(0).getClass());
  } );
}

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

@Test
public void test_sql_hibernate_custom_column_selection_scalar_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-custom-column-selection-scalar-query-example[]
    List<Object[]> persons = session.createNativeQuery(
      "SELECT id, name FROM Person" )
    .list();
    for(Object[] person : persons) {
      Number id = (Number) person[0];
      String name = (String) person[1];
    }
    //end::sql-hibernate-custom-column-selection-scalar-query-example[]
    assertEquals(3, persons.size());
  });
}

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

@Test
public void test_sql_hibernate_entity_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-query-example[]
    List<Person> persons = session.createNativeQuery(
      "SELECT * FROM Person" )
    .addEntity( Person.class )
    .list();
    //end::sql-hibernate-entity-query-example[]
    assertEquals(3, persons.size());
  });
}

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

@Test
public void testMixAndMatchEntityScalar() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  Speech speech = new Speech();
  speech.setLength( new Double( 23d ) );
  speech.setName( "Mine" );
  s.persist( speech );
  s.flush();
  s.clear();
  List l = s.createSQLQuery( "select name, id, flength, name as scalarName from Speech" )
      .setResultSetMapping( "speech" )
      .list();
  assertEquals( l.size(), 1 );
  t.rollback();
  s.close();
}

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

@Test
public void test_sql_hibernate_entity_query_explicit_result_set_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-query-explicit-result-set-example[]
    List<Person> persons = session.createNativeQuery(
      "SELECT id, name, nickName, address, createdOn, version " +
      "FROM Person" )
    .addEntity( Person.class )
    .list();
    //end::sql-hibernate-entity-query-explicit-result-set-example[]
    assertEquals(3, persons.size());
  });
}

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

@Test
public void test_sql_hibernate_entity_associations_query_many_to_one_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-associations-query-many-to-one-example[]
    List<Phone> phones = session.createNativeQuery(
      "SELECT id, phone_number, phone_type, person_id " +
      "FROM Phone" )
    .addEntity( Phone.class )
    .list();
    //end::sql-hibernate-entity-associations-query-many-to-one-example[]
    assertEquals(3, phones.size());
  });
}

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

@Test
public void test_sql_hibernate_inheritance_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-inheritance-query-example[]
    List<CreditCardPayment> payments = session.createNativeQuery(
      "SELECT * " +
      "FROM Payment p " +
      "JOIN CreditCardPayment cp on cp.id = p.id" )
    .addEntity( CreditCardPayment.class )
    .list();
    //end::sql-hibernate-inheritance-query-example[]
    assertEquals(1, payments.size());
  });
}

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

@Test
  @Priority(6)
  public void testEnumRepresentation() {
    Session session = getSession();

    @SuppressWarnings("unchecked")
    List<Object[]> values = session
        .createNativeQuery( "SELECT enum1 e1, enum2 e2 FROM ENUM_ENTITY_AUD ORDER BY rev ASC" )
        .addScalar( "e1", IntegerType.INSTANCE )
        .addScalar( "e2", IntegerType.INSTANCE )
        .list();
    session.close();

    Assert.assertNotNull( values );
    Assert.assertEquals( 2, values.size() );
    Assert.assertArrayEquals( new Object[]{ 0, 0 }, values.get( 0 ) );
    Assert.assertArrayEquals( new Object[]{ 1, 1 }, values.get( 1 ) );
  }
}

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

@Test
public void test_sql_hibernate_multi_entity_query_alias_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-multi-entity-query-alias-example[]
    List<Object> entities = session.createNativeQuery(
      "SELECT {pr.*}, {pt.*} " +
      "FROM Person pr, Partner pt " +
      "WHERE pr.name = pt.name" )
    .addEntity( "pr", Person.class)
    .addEntity( "pt", Partner.class)
    .list();
    //end::sql-hibernate-multi-entity-query-alias-example[]
    assertEquals(1, entities.size());
  });
}

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

@Test
public void test_sql_hibernate_query_parameters_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-query-parameters-example[]
    List<Person> persons = session.createNativeQuery(
      "SELECT * " +
      "FROM Person " +
      "WHERE name like :name" )
    .addEntity( Person.class )
    .setParameter("name", "J%")
    .list();
    //end::sql-hibernate-query-parameters-example[]
    assertEquals(1, persons.size());
  });
}

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

@Test
@TestForIssue(jiraKey = "HHH-7368")
public void testPaginationWithTrailingSemicolon() throws Exception {
  doInHibernate( this::sessionFactory, session -> {
    session.createNativeQuery( "select id from Product2 where description like 'Kit%' order by id;" )
        .setFirstResult( 2 ).setMaxResults( 2 ).list();
  } );
}

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

@Test
public void testManualSynchronization() {
  Session s = openSession();
  s.beginTransaction();
  sessionFactory().getStatistics().clear();
  // create an Organization...
  Organization jboss = new Organization( "JBoss" );
  s.persist( jboss );
  // now query on Employment, this should not cause an auto-flush
  s.createSQLQuery( getEmploymentSQL() ).addSynchronizedQuerySpace( "ABC" ).list();
  assertEquals( 0, sessionFactory().getStatistics().getEntityInsertCount() );
  // now try to query on Employment but this time add Organization as a synchronized query space...
  s.createSQLQuery( getEmploymentSQL() ).addSynchronizedEntityClass( Organization.class ).list();
  assertEquals( 1, sessionFactory().getStatistics().getEntityInsertCount() );
  // clean up
  s.delete( jboss );
  s.getTransaction().commit();
  s.close();
}

相关文章