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

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

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

NativeQuery.setResultTransformer介绍

暂无

代码示例

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

@Test
public void test_sql_hibernate_dto_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-dto-query-example[]
    List<PersonSummaryDTO> dtos = session.createNativeQuery(
      "SELECT p.id as \"id\", p.name as \"name\" " +
      "FROM Person p")
    .setResultTransformer( Transformers.aliasToBean( PersonSummaryDTO.class ) )
    .list();
    //end::sql-hibernate-dto-query-example[]
    assertEquals(3, dtos.size());
  });
}

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

@Test
public void test_sql_hibernate_entity_associations_query_one_to_many_join_example_1() {
  try {
    doInJPA( this::entityManagerFactory, entityManager -> {
      Session session = entityManager.unwrap( Session.class );
      List<Phone> phones = session.createNativeQuery(
        "SELECT * " +
        "FROM Phone ph " +
        "JOIN phone_call c ON c.phone_id = ph.id" )
      .addEntity("phone", Phone.class )
      .addJoin( "c", "phone.calls")
      .setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY )
      .list();
      for(Phone phone : phones) {
        List<Call> calls = phone.getCalls();
      }
      assertEquals(2, phones.size());
    });
  }
  catch (Exception e) {
    log.error( "HHH-10504", e );
    //See issue https://hibernate.atlassian.net/browse/HHH-10504
  }
}

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

.addEntity("org", Organization.class)
.addJoin("emp", "org.employments")
.setResultTransformer( DistinctRootEntityResultTransformer.INSTANCE )
.list();
assertEquals( l.size(), 2 );

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

@Test
public void test_sql_hibernate_entity_associations_query_many_to_one_join_result_transformer_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    Session session = entityManager.unwrap( Session.class );
    //tag::sql-hibernate-entity-associations-query-many-to-one-join-result-transformer-example[]
    List<Person> persons = session.createNativeQuery(
      "SELECT * " +
      "FROM Phone ph " +
      "JOIN Person pr ON ph.person_id = pr.id" )
    .addEntity("phone", Phone.class )
    .addJoin( "pr", "phone.person")
    .setResultTransformer( Criteria.ROOT_ENTITY )
    .list();
    for(Person person : persons) {
      person.getPhones();
    }
    //end::sql-hibernate-entity-associations-query-many-to-one-join-result-transformer-example[]
    assertEquals(3, persons.size());
  });
}

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

.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
.list();
assertEquals( 1,list.size() );
assertEquals(8, o.length);
list = s.createSQLQuery( getEmploymentSQL() ).setResultTransformer( new UpperCasedAliasToEntityMapResultTransformer() ).list();
assertEquals(1, list.size());
m = (Map) list.get(0);

代码示例来源:origin: xautlx/s2jh4net

/**
 * 基于Native SQL和分页(不含排序,排序直接在native sql中定义)对象查询数据集合
 *
 * @param pageable 分页(不含排序,排序直接在native sql中定义)对象
 * @param sql      Native SQL(自行组装好动态条件和排序的原生SQL语句,不含order by部分)
 * @param orderby  order by部分
 * @return Map结构的集合分页对象
 */
@Transactional(readOnly = true)
public Page<Map> findByPageNativeSQL(Pageable pageable, String sql, String orderby) {
  Query query = null;
  if (StringUtils.isNotBlank(orderby)) {
    query = entityManager.createNativeQuery(sql + " " + orderby);
  } else {
    query = entityManager.createNativeQuery(sql);
  }
  query.unwrap(NativeQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
  Query queryCount = entityManager.createNativeQuery("select count(*) from (" + sql + ") cnt");
  query.setFirstResult(Long.valueOf(pageable.getOffset()).intValue());
  query.setMaxResults(pageable.getPageSize());
  Object count = queryCount.getSingleResult();
  return new PageImpl(query.getResultList(), pageable, Long.valueOf(count.toString()));
}

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

public List<ChangeStudyDTO> findByUser(String username) {
  getSessionFactory().getStatistics().logSummary();
  String query = " select s.study_id as \"studyId\", concat(s.study_env_uuid, ps.study_env_uuid) as \"studyEnvUuid\", s.study_env_site_uuid as \"siteEnvUuid\" \n" +
      "\t      from study s left join (select study_id from study_user_role tmp where tmp.status_id = 1 and tmp.role_name != 'admin' and tmp.user_name=:username) sur on (s.study_id=sur.study_id)\n" +
      "\t      left join study ps on (s.parent_study_id = ps.study_id)";
  Query q = getCurrentSession().createNativeQuery(query).setResultTransformer(Transformers.aliasToBean(ChangeStudyDTO.class));
  q.setParameter("username", username);
  return q.list();
}

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

public List<ChangeStudyDTO> findAllUsersByStudy(int studyId) {
  getSessionFactory().getStatistics().logSummary();
  String query = " select u.user_name as \"username\", concat(s.study_env_uuid, ps.study_env_uuid) as \"studyEnvUuid\", s.study_env_site_uuid as \"siteEnvUuid\" \n" +
      "from user_account u, study s left join " +
      "(select study_id from study_user_role tmp where tmp.status_id = 1) sur on (s.study_id=sur.study_id) \n" +
      "left join study ps on (s.parent_study_id = ps.study_id) where s.study_id=:studyId and sur.user_name=u.user_name and u.status_id=1";
  Query q = getCurrentSession().createNativeQuery(query).setResultTransformer(Transformers.aliasToBean(ChangeStudyDTO.class));
  q.setParameter("studyId", studyId);
  return q.list();
}

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

@Test
  public void test() {

    List<PostComment> comments = doInJPA(entityManager -> {
      return (List<PostComment>) entityManager.createNativeQuery(
          "SELECT * " +
          "FROM PostComment c " +
          "WHERE c.status = :status " +
          "CONNECT BY PRIOR c.id = c.parent_id " +
          "START WITH c.parent_id IS NULL AND lower(c.description) like :token ")
        .setParameter("status", Status.APPROVED.name())
        .setParameter("token", "high-performance%")
        .unwrap(NativeQuery.class)
        .addEntity(PostComment.class)
        .setResultTransformer(PostCommentTreeTransformer.INSTANCE)
        .list();
    });
    assertEquals(1, comments.size());
  }
}

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

@Test
public void testNativeQueryResultTransformer() {
  doInJPA( entityManager -> {
    List postDTOs = entityManager.createNativeQuery(
      "select " +
      "       p.id as \"id\", " +
      "       p.title as \"title\" " +
      "from Post p " +
      "where p.created_on > :fromTimestamp")
    .setParameter( "fromTimestamp", Timestamp.from(
      LocalDateTime.of( 2016, 1, 1, 0, 0, 0 ).toInstant( ZoneOffset.UTC ) ))
    .unwrap( org.hibernate.query.NativeQuery.class )
    .setResultTransformer( Transformers.aliasToBean( PostDTO.class ) )
    .getResultList();
    assertEquals( 1, postDTOs.size() );
  } );
}

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

@Test
  public void test() {

    List<PostComment> comments = doInJPA(entityManager -> {
      return (List<PostComment>) entityManager.createNativeQuery(
          "WITH RECURSIVE comment_tree(id, parent_id, description, status) AS ( " +
          "    SELECT c.id, c.parent_id, c.description, status " +
          "    FROM PostComment c " +
          "    WHERE LOWER(c.description) LIKE :token AND c.status = :status " +
          "    UNION ALL " +
          "    SELECT c.id, c.parent_id, c.description, c.status " +
          "    FROM PostComment c " +
          "    INNER JOIN comment_tree ct on ct.id = c.parent_id " +
          "    WHERE c.status = :status " +
          ") " +
          "SELECT id, parent_id, description, status " +
          "FROM comment_tree ")
        .setParameter("status", Status.APPROVED.name())
        .setParameter("token", "high-performance%")
        .unwrap(NativeQuery.class)
        .addEntity(PostComment.class)
        .setResultTransformer(PostCommentTreeTransformer.INSTANCE)
        .list();
    });
    assertEquals(1, comments.size());
  }
}

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

.setResultTransformer(
  new DistinctPostResultTransformer(entityManager)

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

.setResultTransformer(
  new DistinctPostResultTransformer(entityManager)

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

"GROUP BY forum")
.setFlushMode(FlushMode.ALWAYS)
.setResultTransformer( Transformers.aliasToBean(ForumCount.class))
.list();

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

.addSynchronizedEntityClass(Board.class)
.addSynchronizedEntityClass(Post.class)
.setResultTransformer( Transformers.aliasToBean(ForumCount.class))
.list();

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

"GROUP BY forum")
.setFlushMode(FlushMode.ALWAYS)
.setResultTransformer( Transformers.aliasToBean(ForumCount.class))
.list();

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

.setParameter("postId", postId)
.setParameter("rank", rank)
.setResultTransformer(new PostCommentScoreResultTransformer())
.list();
return postCommentScores;

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

.unwrap(NativeQuery.class)
.setParameter("postId", postId).setParameter("rank", rank)
.setResultTransformer(new PostCommentScoreResultTransformer())
.list();
return postCommentScores;

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

.unwrap(NativeQuery.class)
.setParameter("postId", postId).setParameter("rank", rank)
.setResultTransformer(new PostCommentScoreResultTransformer())
.list();
return postCommentScores;

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

.addSynchronizedEntityClass(Board.class)
.addSynchronizedEntityClass(Post.class)
.setResultTransformer( Transformers.aliasToBean(ForumCount.class))
.list();

相关文章