javax.persistence.Query.unwrap()方法的使用及代码示例

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

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

Query.unwrap介绍

[英]Return an object of the specified type to allow access to the provider-specific API. If the provider's query implementation does not support the specified class, the PersistenceException is thrown.
[中]返回指定类型的对象,以允许访问特定于提供程序的API。如果提供程序的查询实现不支持指定的类,则会抛出PersistenceException

代码示例

代码示例来源:origin: stackoverflow.com

Query q2 = em.createNativeQuery(
  "select sc_cur_code, sc_amount from sector_costs");
q2.unwrap(SQLQuery.class).addScalar("sc_cur_code", StringType.INSTANCE);

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

/**
   * Returns the actual target {@link Query} instance, even if the provided query is a {@link Proxy} based on
   * {@link org.springframework.orm.jpa.SharedEntityManagerCreator.DeferredQueryInvocationHandler}.
   * 
   * @param query a {@link Query} instance, possibly a Proxy.
   * @return the class of the actual underlying class if it can be determined, the class of the passed in instance
   *         otherwise.
   */
  private static Class<?> unwrapClass(Query query) {
    Class<? extends Query> queryType = query.getClass();
    try {
      return Proxy.isProxyClass(queryType) //
          ? query.unwrap(null).getClass() //
          : queryType;
    } catch (RuntimeException e) {
      LOGGER.warn("Failed to unwrap actual class for Query proxy.", e);
      return queryType;
    }
  }
}

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

protected String getQueryRequestName(Method javaMethod, Object[] args, Query query) {
  final StringBuilder requestName = new StringBuilder();
  final String methodName = javaMethod.getName();
  requestName.append(methodName, "create".length(), methodName.length());
  appendArgs(requestName, args);
  // with help from Christoph Linder
  // and https://antoniogoncalves.org/2012/05/24/how-to-get-the-jpqlsql-string-from-a-criteriaquery-in-jpa/
  if (HIBERNATE_QUERY_CLASS != null && query.getClass().getName().startsWith("org.hibernate.")
      && requestName.lastIndexOf("@") != -1) {
    // in order to have only one request name instead of patterns like
    // Query(org.hibernate.jpa.criteria.CriteriaQueryImpl@3b0cc2dc)
    final Object unwrappedQuery = query.unwrap(HIBERNATE_QUERY_CLASS);
    return unwrappedQuery.toString();
  } else if (ECLIPSELINK_QUERY_CLASS != null
      && query.getClass().getName().startsWith("org.eclipse.")
      && requestName.lastIndexOf("@") != -1) {
    // in order to have only one request name instead of patterns like
    // Query(org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryImpl@6a55299e)
    final Object unwrappedQuery = query.unwrap(ECLIPSELINK_QUERY_CLASS);
    return unwrappedQuery.toString();
  }
  return requestName.toString();
}

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

args[i] = ((Query) arg).unwrap(null);

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

/**
 * Convenience method for {@linkplain Query#getResultList() executing} the Query, applying the
 * given EntityGraph using the specified semantic
 *
 * @param query The JPA Query
 * @param graph The graph to apply
 * @param semantic The semantic to use when applying the graph
 */
@SuppressWarnings("unchecked")
public static List executeList(Query query, EntityGraph graph, GraphSemantic semantic) {
  return query.unwrap( org.hibernate.query.Query.class )
      .applyGraph( (RootGraph) graph, semantic )
      .list();
}

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

/**
 * Convenience method for {@linkplain Query#getResultList() executing} the Query using the
 * given EntityGraph
 *
 * @param query The JPA Query
 * @param graph The graph to apply
 *
 * @apiNote operates on the assumption that the "default" semantic for an
 * entity graph applied to a Query is {@link GraphSemantic#FETCH}.  This is simply
 * knowledge from JPA EG discussions, nothing that is specifically mentioned or
 * discussed in the spec.
 */
@SuppressWarnings({"unused", "unchecked"})
public static List executeList(Query query, EntityGraph graph) {
  return query.unwrap( org.hibernate.query.Query.class )
      .applyFetchGraph( (RootGraph) graph )
      .list();
}

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

/**
 * Creates a new {@link HibernateScrollableResultsIterator} for the given {@link Query}.
 *
 * @param jpaQuery must not be {@literal null}.
 */
HibernateScrollableResultsIterator(Query jpaQuery) {
  org.hibernate.query.Query<?> query = jpaQuery.unwrap(org.hibernate.query.Query.class);
  this.scrollableResults = query.setReadOnly(TransactionSynchronizationManager.isCurrentTransactionReadOnly())//
      .scroll(ScrollMode.FORWARD_ONLY);
}

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

/**
 * Convenience method for {@linkplain Query#getResultList() executing} the Query, applying the
 * given EntityGraph using the named semantic using JPA's "hint name" - see
 * {@link GraphSemantic#fromJpaHintName}
 *
 * @param query The JPA Query
 * @param graph The graph to apply
 * @param semanticJpaHintName See {@link GraphSemantic#fromJpaHintName}
 *
 * @return The result list
 */
@SuppressWarnings({"unused", "unchecked"})
public static List executeList(Query query, EntityGraph graph, String semanticJpaHintName) {
  return query.unwrap( org.hibernate.query.Query.class )
      .applyGraph( (RootGraph) graph, GraphSemantic.fromJpaHintName( semanticJpaHintName ) )
      .list();
}

代码示例来源:origin: org.springframework/spring-orm

args[i] = ((Query) arg).unwrap(null);

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

final ProcedureCall unwrapped = query.unwrap( ProcedureCall.class );
if ( unwrapped != null ) {
  addNamedStoredProcedureQuery( name, unwrapped );
org.hibernate.query.Query hibernateQuery = query.unwrap( org.hibernate.query.Query.class );
if ( hibernateQuery != null ) {

代码示例来源: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 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 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: Impetus/Kundera

@Override
public <T> T unwrap(Class<T> arg0)
{
  return query.unwrap(arg0);
}

代码示例来源:origin: xujeff/tianti

@SuppressWarnings("unchecked")
public List<Map<String, Object>> querySqlObjects(String sql, Object params, Integer currentPage,Integer rowsInPage){
  Query qry = em.createNativeQuery(sql);
  SQLQuery s = qry.unwrap(SQLQuery.class);

代码示例来源:origin: zstackio/zstack

@Transactional(readOnly = true)
  long count() {
    String jpql = build(true);
    Query q = dbf.getEntityManager().createQuery(jpql);
    if (logger.isTraceEnabled()) {
      org.hibernate.Query hq = q.unwrap(org.hibernate.Query.class);
      logger.trace(hq.getQueryString());
    }
    setQueryValue(q, root);
    return (Long) q.getSingleResult();
  }
}

代码示例来源:origin: xujeff/tianti

SQLQuery s = qry.unwrap(SQLQuery.class);
if (currentPage != null && pageSize != null) {//判断是否有分页

代码示例来源:origin: xujeff/tianti

SQLQuery s = qry.unwrap(SQLQuery.class);
if (currentPage != null && pageSize != null) {//判断是否有分页

代码示例来源:origin: zstackio/zstack

@Transactional(readOnly = true)
List query() {
  if (msg.isFieldQuery()) {
    validateFields();
  }
  String jpql = build(false);
  Query q = msg.isFieldQuery() ? dbf.getEntityManager().createQuery(jpql, Tuple.class) : dbf.getEntityManager().createQuery(jpql);
  if (logger.isTraceEnabled()) {
    org.hibernate.Query hq = q.unwrap(org.hibernate.Query.class);
    logger.trace(hq.getQueryString());
  }
  setQueryValue(q, root);
  if (msg.getLimit() != null) {
    q.setMaxResults(msg.getLimit());
  }
  if (msg.getStart() != null) {
    q.setFirstResult(msg.getStart());
  }
  List vos = q.getResultList();
  if (msg.isFieldQuery()) {
    return convertFieldsTOPartialInventories(vos);
  } else {
    return convertVOsToInventories(vos);
  }
}

代码示例来源:origin: org.springframework.data/spring-data-jpa

/**
 * Creates a new {@link HibernateScrollableResultsIterator} for the given {@link Query}.
 *
 * @param jpaQuery must not be {@literal null}.
 */
HibernateScrollableResultsIterator(Query jpaQuery) {
  org.hibernate.query.Query<?> query = jpaQuery.unwrap(org.hibernate.query.Query.class);
  this.scrollableResults = query.setReadOnly(TransactionSynchronizationManager.isCurrentTransactionReadOnly())//
      .scroll(ScrollMode.FORWARD_ONLY);
}

相关文章