javax.jdo.Query.setResultClass()方法的使用及代码示例

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

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

Query.setResultClass介绍

[英]Specify the type of object in which to return each element of the result of invoking #execute or one of its siblings. If the result is not set or set to null, the result class defaults to the candidate class of the query. If the result consists of one expression, the result class defaults to the type of that expression. If the result consists of more than one expression, the result class defaults to Object[]. The result class may be specified to be one of the java.lang classes Character, Boolean, Byte, Short, Integer, Long, Float, Double, String, or Object[]; or one of the java.math classes BigInteger or BigDecimal; or the java.util class Date; or one of the java.sql classes Date, Time, or Timestamp; or a user-defined class.

If there are multiple result expressions, the result class must be able to hold all elements of the result specification or a JDOUserException is thrown.

If there is only one result expression, the result class must be assignable from the type of the result expression or must be able to hold all elements of the result specification. A single value must be able to be coerced into the specified result class (treating wrapper classes as equivalent to their unwrapped primitive types) or by matching. If the result class does not satisfy these conditions, a JDOUserException is thrown.

A constructor of a result class specified in the setResult method will be used if the results specification matches the parameters of the constructor by position and type. If more than one constructor satisfies the requirements, the JDO implementation chooses one of them. If no constructor satisfies the results requirements, or if the result class is specified via the setResultClass method, the following requirements apply:

  • A user-defined result class must have a no-args constructor and one or more public set or put methods or fields.

  • Each result expression must match one of:

  • a public field that matches the name of the result expression and is of the type (treating wrapper types equivalent to primitive types) of the result expression;

    • or if no public field matches the name and type, a public set method that returns void and matches the name of the result expression and takes a single parameter which is the exact type of the result expression;
    • or if neither of the above applies,a public method must be found with the signature void put(Object, Object). During processing of the results, the first argument is the name of the result expression and the second argument is the value from the query result.
      Portable result classes do not invoke any persistence behavior during their no-args constructor or set methods.
      [中]指定要在其中返回调用#execute结果的每个元素或其一个同级元素的对象类型。如果结果未设置或未设置为null,则结果类默认为查询的候选类。如果结果由一个表达式组成,则结果类默认为该表达式的类型。如果结果由多个表达式组成,则结果类默认为Object[]。结果类可以指定为java语言的一个。lang类包括字符、布尔、字节、短、整数、长、浮点、双精度、字符串或对象[];或者一个java。数学类BigInteger或BigDecimal;或者java。上课日期;或者一个java。sql类日期、时间或时间戳;或用户定义的类。
      如果有多个结果表达式,则结果类必须能够保存结果规范的所有元素,否则会引发JDOUserException。
      如果只有一个结果表达式,那么结果类必须可以从结果表达式的类型中赋值,或者必须能够保存结果规范的所有元素。单个值必须能够被强制到指定的结果类中(将包装类视为等同于其未包装的基本类型),或者通过匹配。如果结果类不满足这些条件,则抛出JDOUserException。
      如果结果规范按位置和类型匹配构造函数的参数,则将使用setResult方法中指定的结果类的构造函数。如果有多个构造函数满足这些要求,JDO实现将选择其中一个。如果没有构造函数满足结果要求,或者如果通过setResultClass方法指定了结果类,则以下要求适用:
      *用户定义的结果类必须具有无参数构造函数和一个或多个公共setput方法或字段。
      *每个结果表达式必须匹配以下其中一个:
      *一个公共字段,它与结果表达式的名称匹配,并且是结果表达式的类型(将包装器类型等同于基元类型);
      *或者,如果没有与名称和类型匹配的公共字段,则使用公共set方法,该方法返回void并与结果表达式的名称匹配,并使用一个与结果表达式的类型完全相同的参数;
      *或者,如果上述两项都不适用,则必须找到签名为[$3$]的公共方法。在处理结果期间,第一个参数是结果表达式的名称,第二个参数是查询结果的值。
      可移植结果类在其无参数构造函数或set方法期间不会调用任何持久性行为。

代码示例

代码示例来源:origin: apache/hive

query.declareImports("import java.lang.String");
query.setResult("tableName");
query.setResultClass(java.lang.String.class);
if (maxTables >= 0) {
 query.setRange(0, maxTables);

代码示例来源:origin: apache/hive

/**
 * Gets partition names from the table via ORM (JDOQL) name filter.
 * @param dbName Database name.
 * @param tblName Table name.
 * @param partNames Partition names to get the objects for.
 * @return Resulting partitions.
 */
private List<Partition> getPartitionsViaOrmFilter(String catName,
  String dbName, String tblName, List<String> partNames) throws MetaException {
 if (partNames.isEmpty()) {
  return new ArrayList<>();
 }
 ObjectPair<Query, Map<String, String>> queryWithParams =
   getPartQueryWithParams(catName, dbName, tblName, partNames);
 Query query = queryWithParams.getFirst();
 query.setResultClass(MPartition.class);
 query.setClass(MPartition.class);
 query.setOrdering("partitionName ascending");
 @SuppressWarnings("unchecked")
 List<MPartition> mparts = (List<MPartition>)query.executeWithMap(queryWithParams.getSecond());
 List<Partition> partitions = convertToParts(catName, dbName, tblName, mparts);
 if (query != null) {
  query.closeAll();
 }
 return partitions;
}

代码示例来源:origin: apache/sentry

@VisibleForTesting
List<MPath> getMPaths() throws Exception {
 return tm.executeTransaction(pm -> {
  long currentSnapshotID = getCurrentAuthzPathsSnapshotID(pm);
  Query query = pm.newQuery("SQL",
    "SELECT p.PATH_NAME FROM AUTHZ_PATH p " +
      "JOIN AUTHZ_PATHS_MAPPING a ON a.AUTHZ_OBJ_ID = p.AUTHZ_OBJ_ID " +
      "WHERE a.AUTHZ_SNAPSHOT_ID = ?"
  );
  query.setResultClass(MPath.class);
  return (List<MPath>) query.execute(currentSnapshotID);
 });
}

代码示例来源:origin: edu.berkeley.cs.shark/hive-metastore

query.declareImports("import java.lang.String");
query.setResult("tableName");
query.setResultClass(java.lang.String.class);
if (maxTables >= 0) {
 query.setRange(0, maxTables);

代码示例来源:origin: org.spark-project.hive/hive-metastore

query.declareImports("import java.lang.String");
query.setResult("tableName");
query.setResultClass(java.lang.String.class);
if (maxTables >= 0) {
 query.setRange(0, maxTables);

代码示例来源:origin: org.apache.hive/hive-standalone-metastore

query.declareImports("import java.lang.String");
query.setResult("tableName");
query.setResultClass(java.lang.String.class);
if (maxTables >= 0) {
 query.setRange(0, maxTables);

代码示例来源:origin: com.mysema.querydsl/querydsl-jdo

private Query createQuery(boolean forCount) {
  SQLSerializer serializer = new SQLSerializer(configuration);
  if (union != null) {
    serializer.serializeUnion(union, queryMixin.getMetadata(), unionAll);
  } else {
    serializer.serialize(queryMixin.getMetadata(), forCount);
  }
  // create Query
  if (logger.isDebugEnabled()) {
    logger.debug(serializer.toString());
  }
  Query query = persistenceManager.newQuery("javax.jdo.query.SQL",serializer.toString());
  orderedConstants = serializer.getConstants();
  queries.add(query);
  if (!forCount) {
    List<? extends Expression<?>> projection = queryMixin.getMetadata().getProjection();
    if (projection.get(0) instanceof FactoryExpression) {
      this.projection = (FactoryExpression<?>)projection.get(0);
    }
  } else {
    query.setResultClass(Long.class);
  }
  return query;
}

代码示例来源:origin: com.facebook.presto.hive/hive-apache

query.declareImports("import java.lang.String");
query.setResult("tableName");
query.setResultClass(java.lang.String.class);
if (maxTables >= 0) {
 query.setRange(0, maxTables);

代码示例来源:origin: com.facebook.presto.hive/hive-apache

private List<MPartition> getMPartitionsViaOrmFilter(String dbName,
  String tblName, List<String> partNames, Out<Query> out) {
 ObjectPair<Query, Map<String, String>> queryWithParams =
   getPartQueryWithParams(dbName, tblName, partNames);
 Query query = out.val = queryWithParams.getFirst();
 query.setResultClass(MPartition.class);
 query.setClass(MPartition.class);
 query.setOrdering("partitionName ascending");
 @SuppressWarnings("unchecked")
 List<MPartition> result = (List<MPartition>)query.executeWithMap(queryWithParams.getSecond());
 return result;
}

代码示例来源:origin: org.spark-project.hive/hive-metastore

private List<MPartition> getMPartitionsViaOrmFilter(String dbName,
  String tblName, List<String> partNames, Out<Query> out) {
 ObjectPair<Query, Map<String, String>> queryWithParams =
   getPartQueryWithParams(dbName, tblName, partNames);
 Query query = out.val = queryWithParams.getFirst();
 query.setResultClass(MPartition.class);
 query.setClass(MPartition.class);
 query.setOrdering("partitionName ascending");
 @SuppressWarnings("unchecked")
 List<MPartition> result = (List<MPartition>)query.executeWithMap(queryWithParams.getSecond());
 return result;
}

代码示例来源:origin: org.apache.hive/hive-standalone-metastore

/**
 * Gets partition names from the table via ORM (JDOQL) name filter.
 * @param dbName Database name.
 * @param tblName Table name.
 * @param partNames Partition names to get the objects for.
 * @return Resulting partitions.
 */
private List<Partition> getPartitionsViaOrmFilter(String catName,
  String dbName, String tblName, List<String> partNames) throws MetaException {
 if (partNames.isEmpty()) {
  return new ArrayList<>();
 }
 ObjectPair<Query, Map<String, String>> queryWithParams =
   getPartQueryWithParams(catName, dbName, tblName, partNames);
 Query query = queryWithParams.getFirst();
 query.setResultClass(MPartition.class);
 query.setClass(MPartition.class);
 query.setOrdering("partitionName ascending");
 @SuppressWarnings("unchecked")
 List<MPartition> mparts = (List<MPartition>)query.executeWithMap(queryWithParams.getSecond());
 List<Partition> partitions = convertToParts(catName, dbName, tblName, mparts);
 if (query != null) {
  query.closeAll();
 }
 return partitions;
}

代码示例来源:origin: org.apache.continuum/continuum-store

public Map<Integer, ProjectGroupSummary> getProjectsSummary()
{
  PersistenceManager pm = getPersistenceManager();
  Transaction tx = pm.currentTransaction();
  try
  {
    tx.begin();
    Extent extent = pm.getExtent( Project.class );
    Query query = pm.newQuery( extent );
    query.setResult( "projectGroup.id as projectGroupId, state as projectState, count(state) as size" );
    query.setResultClass( ProjectSummaryResult.class );
    query.setGrouping( "projectGroup.id, state" );
    List<ProjectSummaryResult> results = (List<ProjectSummaryResult>) query.execute();
    Map<Integer, ProjectGroupSummary> summaries = processProjectGroupSummary( results );
    tx.commit();
    return summaries;
  }
  finally
  {
    rollback( tx );
  }
}

代码示例来源:origin: tzaeschke/zoodb

q.setResultClass(EmpWrapper.class);
Collection<EmpWrapper> infos = (Collection<EmpWrapper>) q.execute (new Float (30000.));
Iterator<EmpWrapper> it = infos.iterator();

代码示例来源:origin: tzaeschke/zoodb

q.setResultClass(EmpInfo.class);
Collection<EmpInfo> infos = (Collection<EmpInfo>) q.execute (new Float (30000.));
Iterator<EmpInfo> it = infos.iterator();

代码示例来源:origin: org.datanucleus/datanucleus-api-jdo

query.setResultClass(resultCls);

代码示例来源:origin: tzaeschke/zoodb

q.declareParameters ("String deptName");
q.setResult("name, salary, boss as reportsTo");
q.setResultClass(Info.class);
Collection<Info> names = (Collection<Info>) q.execute("R&D");
Iterator<Info> it = names.iterator();

代码示例来源:origin: tzaeschke/zoodb

q.declareParameters ("String deptName");
q.setResult("new Info(name, salary, boss)");
q.setResultClass(Info.class);
Collection<Info> names = (Collection<Info>) q.execute("R&D");
Iterator<Info> it = names.iterator();

代码示例来源:origin: tzaeschke/zoodb

/**
   * 14.10.14 Selection of a Single Field
   * This query returns a single field of a single Employee.
   */
  @Test
  public void testQuery_14_10_14() {
    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();

    Query q = pm.newQuery (Employee.class, "name == empName");
    q.declareParameters ("String empName");
    q.setResult("salary");
    q.setResultClass(Float.class);
    q.setUnique(true);
    Float salary = (Float) q.execute ("Michael");
//            <query name="single">
//            [!CDATA[
//            select unique new Float(salary)
//            where dept.name == :deptName
//            ]]
//            </query>
    assertNotNull(salary);
    //assertEquals(40001., salary);
    assertTrue(Math.abs(40001. - salary) < 0.00001);
    TestTools.closePM(pm);
  }

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

query.setResultClass(resultCls);

相关文章