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

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

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

NativeQuery.addScalar介绍

[英]Declare a scalar query result.

Functions like in hbm.xml or javax.persistence.ColumnResult in annotations
[中]声明标量查询结果。
功能类似于hbm。xml或javax。坚持不懈列会产生注释

代码示例

代码示例来源: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 test_sql_hibernate_query_scalar_explicit_result_set_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-scalar-query-explicit-result-set-example[]
    List<Object[]> persons = session.createNativeQuery(
      "SELECT * FROM Person" )
    .addScalar( "id", LongType.INSTANCE )
    .addScalar( "name", StringType.INSTANCE )
    .list();
    for(Object[] person : persons) {
      Long id = (Long) person[0];
      String name = (String) person[1];
    }
    //end::sql-hibernate-scalar-query-explicit-result-set-example[]
    assertEquals(3, persons.size());
  });
}

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

@Test
public void test_sql_hibernate_query_scalar_partial_explicit_result_set_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-scalar-query-partial-explicit-result-set-example[]
    List<Object[]> persons = session.createNativeQuery(
      "SELECT * FROM Person" )
    .addScalar( "id", LongType.INSTANCE )
    .addScalar( "name" )
    .list();
    for(Object[] person : persons) {
      Long id = (Long) person[0];
      String name = (String) person[1];
    }
    //end::sql-hibernate-scalar-query-partial-explicit-result-set-example[]
    assertEquals(3, persons.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 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

private Integer getCurrentAuditUniqueGroupId() {
  return TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
    final Session session = entityManager.unwrap( Session.class );
    final Query query = session.createSQLQuery(
        "SELECT uniqueGroup_id FROM GroupMember_AUD ORDER BY rev DESC" ).addScalar(
        "uniqueGroup_id",
        IntegerType.INSTANCE
    ).setMaxResults( 1 );
    final Object result = query.getSingleResult();
    assertNotNull( result );
    return (Integer) result;
  } );
}

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

.addEntity("org", Organization.class)
    .addJoin("emp", "org.employments")
    .addScalar("regionCode", StringType.INSTANCE)
    .list();
assertEquals( 2, l.size() );

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

assertEquals(8, m.size());
list = s.createSQLQuery( getEmploymentSQLMixedScalarEntity() ).addScalar( "employerid" ).addEntity( Employment.class ).list();
assertEquals(1, list.size());
o = (Object[]) list.get(0);

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

/**
 * Declare a scalar query result.
 * <p/>
 * Functions like {@code <return-scalar/>} in {@code hbm.xml} or
 * {@link javax.persistence.ColumnResult} in annotations
 *
 * @param columnAlias The column alias in the result-set to be processed
 *         as a scalar result
 * @param type The Hibernate type as which to treat the value.
 *
 * @return {@code this}, for method chaining
 */
default NativeQuery<T> addScalar(String columnAlias, Type type) {
  return addScalar( columnAlias, (BasicValuedExpressableType) type );
}

代码示例来源:origin: Evolveum/midpoint

private void dumpOrgClosureTypeTable(Session session, String tableName) {
  NativeQuery q = session.createNativeQuery("select descendant_oid, ancestor_oid, val from " + tableName)
      .addScalar("descendant_oid", StringType.INSTANCE)
      .addScalar("ancestor_oid", StringType.INSTANCE)
      .addScalar("val", IntegerType.INSTANCE);
  List<Object[]> list = q.list();
  LOGGER.trace("{} ({} rows):", tableName, list.size());
  for (Object[] row : list) {
    LOGGER.trace(" - [d={}, a={}, val={}]", row);
  }
}

代码示例来源:origin: Evolveum/midpoint

private int quickCheck(Session session) {
  NativeQuery q = session.createNativeQuery(
      "select count(m_org.oid) as problems from m_org left join m_org_closure cl " +
          "on cl.descendant_oid = m_org.oid and cl.ancestor_oid = m_org.oid " +
          "where cl.descendant_oid is null").addScalar("problems", IntegerType.INSTANCE);
  List problemsList = q.list();
  if (problemsList == null || problemsList.size() != 1) {
    throw new IllegalStateException("Unexpected return value from the closure check query: " + problemsList + " (a 1-item list of Integer expected)");
  }
  return (int) problemsList.get(0);
}
//endregion

代码示例来源:origin: Evolveum/midpoint

private void checkForCycles(List<Edge> edges, Session session) {
  String queryText = "select descendant_oid, ancestor_oid from " + CLOSURE_TABLE_NAME + " where " + getWhereClauseForCycleCheck(edges);
  NativeQuery query = session.createNativeQuery(queryText)
      .addScalar("descendant_oid", StringType.INSTANCE)
      .addScalar("ancestor_oid", StringType.INSTANCE);
  long start = System.currentTimeMillis();
  List list = query.list();
  LOGGER.trace("Cycles checked in {} ms, {} conflicts found", System.currentTimeMillis()-start, list.size());
  if (!list.isEmpty()) {
    throw new IllegalArgumentException("Modification couldn't be executed, because a cycle in org structure graph would be created. Cycle-creating edges being added: " + formatList(list));
  }
}

代码示例来源:origin: jpos/jPOS-EE

public int getMaxIdLength() {
    String queryString = "select max(length(id)) as maxidlen from sysconfig";
    if (prefix != null)
      queryString += " where id like :query";
    NativeQuery query =  db.session().createNativeQuery(queryString);
    if (prefix != null)
      query.setParameter ("query", prefix + "%");
    return (int) query.addScalar("maxidlen",  IntegerType.INSTANCE).getSingleResult();
  }
}

代码示例来源:origin: Evolveum/midpoint

LOGGER.info("Reading from existing org closure table");
NativeQuery selectQuery = session.createNativeQuery("SELECT descendant_oid, ancestor_oid, val from " + CLOSURE_TABLE_NAME)
    .addScalar("descendant_oid", StringType.INSTANCE)
    .addScalar("ancestor_oid", StringType.INSTANCE)
    .addScalar("val", IntegerType.INSTANCE);
existingEntries = selectQuery.list();
LOGGER.info("{} entries read", existingEntries.size());
LOGGER.info("Reading from recomputed org closure table");
NativeQuery selectQuery = session.createNativeQuery("SELECT descendant_oid, ancestor_oid, val from " + CLOSURE_TABLE_NAME)
    .addScalar("descendant_oid", StringType.INSTANCE)
    .addScalar("ancestor_oid", StringType.INSTANCE)
    .addScalar("val", IntegerType.INSTANCE);
List recomputedEntries = selectQuery.list();
LOGGER.info("{} entries read", recomputedEntries.size());

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

@Test
  public void testFetchJsonProperty() {
    doInJPA(entityManager -> {
      JsonNode properties = (JsonNode) entityManager
      .createNativeQuery(
        "SELECT properties " +
        "FROM book " +
        "WHERE isbn = :isbn")
      .setParameter("isbn", "978-9730228236")
      .unwrap(org.hibernate.query.NativeQuery.class)
      .addScalar("properties", JsonNodeBinaryType.INSTANCE)
      .getSingleResult();

      assertEquals(
        "High-Performance Java Persistence",
        properties.get("title").asText()
      );
    });
  }
}

代码示例来源:origin: Evolveum/midpoint

.addScalar("descendant_oid", StringType.INSTANCE)
    .addScalar("ancestor_oid", StringType.INSTANCE)
    .addScalar("val", LongType.INSTANCE);
List<Object[]> list = q.list();
LOGGER.info("OrgClosure has {} rows", list.size());

相关文章