org.hibernate.Criteria.setProjection()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(192)

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

Criteria.setProjection介绍

[英]Used to specify that the query results will be a projection (scalar in nature). Implicitly specifies the #PROJECTION result transformer.

The individual components contained within the given Projection determines the overall "shape" of the query result.
[中]用于指定查询结果将是投影(本质上是标量)。隐式指定#投影结果转换器。
给定投影中包含的各个组件决定了查询结果的整体“形状”。

代码示例

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

Criteria crit = session.createCriteria(Person.class);
crit.add( Restrictions.isNotNull("birthDate"));
crit.add( Restrictions.eq("isStudent", true));
crit.setProjection(Projections.rowCount());
Integer count = (Integer)crit.uniqueResult();

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

@Override
public Long countContentItems(Criteria c) {
  c.setProjection(Projections.rowCount());
  return (Long) c.uniqueResult();
}

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

@Override
  public Object doInTransaction(TransactionStatus status) {
    PropertyProjection pipelineName = Projections.property("pipelineName");
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PipelineState.class).setProjection(pipelineName).add(
        Restrictions.eq("locked", true));
    criteria.setCacheable(false);
    List<String> list = criteria.list();
    return list;
  }
});

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

@Override
public List<AssignedProductOptionDTO> findAssignedProductOptionsByProductId(Long productId) {
  Session session = em.unwrap(Session.class);
  Criteria criteria = session.createCriteria(SkuProductOptionValueXrefImpl.class);
  List dtoList = criteria
    .createAlias("sku", "sku")
    .createAlias("sku.product", "product")
    .createAlias("productOptionValue", "productOptionValue")
    .createAlias("productOptionValue.productOption", "productOption")
    .setProjection(Projections.distinct(
        Projections.projectionList()
        .add(Projections.property("product.id"), "productId")
        .add(Projections.property("productOption.attributeName"), "productOptionAttrName")
        .add(Projections.property("productOptionValue"), "productOptionValue")
        .add(Projections.property("sku"), "sku")
      )
    ).setResultTransformer(Transformers.aliasToBean(AssignedProductOptionDTO.class))
    .add(Restrictions.eq("product.id", productId))
    .addOrder(Order.asc("productOption.attributeName")).list();
  List<AssignedProductOptionDTO> results = new ArrayList<>();
  for (Object o : dtoList) {
    AssignedProductOptionDTO dto = (AssignedProductOptionDTO) o;
    if (dto.getSku().isActive()) {
      results.add(dto);
    }
  }
  return results;
}

代码示例来源:origin: openmrs/openmrs-core

@Override
public List<T> list() {
  criteria.setProjection(null);
  criteria.setResultTransformer(Criteria.ROOT_ENTITY);
  
  @SuppressWarnings("unchecked")
  List<T> list = criteria.list();
  
  return list;
}

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

Criteria criteria=session.createCriteria(Student.class);
  criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
  criteria.add(Restrictions.ne("enquiryStatus", ENQUIRY.JOINED));
  criteria.setMaxResults(10);
  criteria.setFirstResult((paginate.getStartIndex()-1)*10);
  List<Student> students = criteria.list();
 criteria.setProjection(null);
 criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
 Long resultCount = (Long)criteria.uniqueResult();

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

.setCacheable( isCacheableQuery )
    .setFetchMode( "student", FetchMode.JOIN )
    .setProjection(
        Projections.projectionList()
            .add( Property.forName( "student" ), "student" )
    .list();
t.commit();
s.close();

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

protected Criteria getCriteria(Session s) {
    // should use PassThroughTransformer by default
    return s.createCriteria( Enrolment.class )
        .setProjection( Projections.property( "student" ) )
        .add( Restrictions.eq( "studentNumber", Long.valueOf( yogiExpected.getStudentNumber() ) ) );
  }
};

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

Criteria criteria=session.createCriteria(Student.class);
   criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
   criteria.add(Restrictions.ne("enquiryStatus", ENQUIRY.JOINED));
   criteria.setProjection(Projections.rowCount())
 Long resultCount = (Long)criteria.uniqueResult();

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

Long longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.rowCount()).uniqueResult();
assertEquals(longValue, new Long(1));
longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.count("heightInches")).uniqueResult();
assertEquals(longValue, new Long(1));
Double dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.max( "heightInches" )).uniqueResult();
assertNotNull(dblValue);
longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.max( "id" )).uniqueResult();
assertNotNull(longValue);
dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "heightInches" )).uniqueResult();
assertNotNull(dblValue);
dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "id" )).uniqueResult();
assertNotNull(dblValue);
dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "bigIntegerValue" )).uniqueResult();
assertNotNull(dblValue);
longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.sum( "id" )).uniqueResult();
assertNotNull(longValue);
longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.sum( "intValue" )).uniqueResult();
assertNotNull(longValue);
dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.sum( "heightInches" )).uniqueResult();
assertNotNull(dblValue);

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

@Test
@RequiresDialect( H2Dialect.class )
public void testCaseClause() {
  doInHibernate( this::sessionFactory, session -> {
    Criteria criteria = session.createCriteria( TableA.class );
    final String TABLE_B_ALIAS = "tableBAlias";
    final String TABLE_C_ALIAS = "tableCAlias";
    Criterion tableCRestriction = Restrictions.eq( TABLE_C_ALIAS + ".tableCBoolean", false );
    criteria.createAlias(
        TABLE_B_ALIAS + ".tableCs",
        TABLE_C_ALIAS,
        JoinType.LEFT_OUTER_JOIN,
        tableCRestriction
    );
    Criterion tableBRestriction = Restrictions.eq( TABLE_B_ALIAS + ".tableBDate", new Date() );
    criteria.createAlias( "tableBs", TABLE_B_ALIAS, JoinType.LEFT_OUTER_JOIN, tableBRestriction );
    criteria.add( Restrictions.eq( "tableACharacter", "c" ) );
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add( Projections.property( "tableACharacter" ) );
    criteria.setProjection( projectionList );
    criteria.list();
  } );
}

代码示例来源:origin: openmrs/openmrs-core

criteria.setProjection(Projections.property("concept"));
  criteria.add(Restrictions.eq("term.code", code).ignoreCase());
} else {
  criteria.add(Restrictions.eq("term.code", code));
  criteria.add(Restrictions.or(Restrictions.eq("source.name", sourceName).ignoreCase(), Restrictions.eq(
    "source.hl7Code", sourceName).ignoreCase()));
} else {
return (List<Concept>) criteria.list();

代码示例来源:origin: openmrs/openmrs-core

/**
 * @see org.openmrs.api.db.ObsDAO#getObservationCount(List, List, List, List, List, List, Integer, Date, Date, List, boolean, String)
 */
@Override
public Long getObservationCount(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
    List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, Integer obsGroupId,
    Date fromDate, Date toDate, List<ConceptName> valueCodedNameAnswers, boolean includeVoidedObs,
    String accessionNumber) throws DAOException {
  Criteria criteria = createGetObservationsCriteria(whom, encounters, questions, answers, personTypes, locations,
    null, null, obsGroupId, fromDate, toDate, valueCodedNameAnswers, includeVoidedObs, accessionNumber);
  criteria.setProjection(Projections.rowCount());
  return (Long) criteria.list().get(0);
}

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

.setProjection(
    Projections.projectionList()
        .add( Projections.groupProperty( "c.id" ) )
.setFirstResult( 1 ).setMaxResults( 3 ).list();

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

protected Criteria getCriteria(Session s) {
    // should use PassThroughTransformer by default
    return s.createCriteria( Enrolment.class, "e" )
        .setProjection( Projections.property( "e.semester" ) )
        .add( Restrictions.eq( "e.studentNumber", shermanEnrolmentExpected.getStudentNumber() ) );
  }
};

代码示例来源:origin: kaaproject/kaa

@Override
public boolean validateApplicationEventFamilyMap(String appId, String ecfId, int version) {
 LOG.debug("Validating application event family map by application id [{}], ecf id [{}], "
      + "version [{}]", appId, ecfId, version);
 Criteria criteria = getCriteria();
 criteria.createAlias(APPLICATION_PROPERTY, APPLICATION_ALIAS);
 criteria.createAlias(ECF_PROPERTY, ECF_ALIAS);
 criteria.add(Restrictions.and(
   Restrictions.eq(APPLICATION_REFERENCE, Long.valueOf(appId)),
   Restrictions.eq(ECF_REFERENCE, Long.valueOf(ecfId)),
   Restrictions.eq(VERSION_PROPERTY, version)));
 Long count = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
 boolean result = count != null ? count == 0 : false;
 LOG.debug("[{},{},{}] Validation result: {}.", appId, ecfId, version, result);
 return result;
}

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

@Test
public void testSaveOrUpdateManaged() {
  Session s = openSession();
  Transaction tx = s.beginTransaction();
  NumberedNode root = new NumberedNode( "root" );
  s.saveOrUpdate( root );
  tx.commit();
  tx = s.beginTransaction();
  NumberedNode child = new NumberedNode( "child" );
  root.addChild( child );
  s.saveOrUpdate( root );
  assertFalse( s.contains( child ) );
  s.flush();
  assertTrue( s.contains( child ) );
  tx.commit();
  assertTrue( root.getChildren().contains( child ) );
  assertEquals( root.getChildren().size(), 1 );
  tx = s.beginTransaction();
  assertEquals(
      Long.valueOf( 2 ),
      s.createCriteria( NumberedNode.class )
          .setProjection( Projections.rowCount() )
          .uniqueResult()
  );
  s.delete( root );
  s.delete( child );
  tx.commit();
  s.close();
}

代码示例来源:origin: openmrs/openmrs-core

/**
 * @see ConceptDAO#getConceptAttributeCount(ConceptAttributeType)
 */
@Override
public long getConceptAttributeCount(ConceptAttributeType conceptAttributeType) {
  Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConceptAttribute.class);
  criteria.add(Restrictions.eq("attributeType", conceptAttributeType));
  criteria.setProjection(Projections.rowCount());
  return (Long) criteria.list().get(0);
}

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

Criteria crit = session.createCriteria(House.class);
crit.add(Expression.ge("size", 20));
crit.setMaxResults(10);
crit.addOrder(Order.desc("size"));
crit.setFetchMode("resident", FetchMode.JOIN);
crit.setProjection(Projections.property("resident"));
return crit.list();

代码示例来源:origin: bill1012/AdminEAP

public <T> List<T> findByCriteria(DetachedCriteria criteria) {
  return criteria.getExecutableCriteria(getCurrentSession()).setProjection(null)
      .setResultTransformer(CriteriaSpecification.ROOT_ENTITY).list();
}

相关文章