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

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

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

Criteria.add介绍

[英]Add a Criterion to constrain the results to be retrieved.
[中]添加条件以约束要检索的结果。

代码示例

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

Criteria c = session.createCriteria(CountryDTO.class);
c.add(Restrictions.eq("type", type));
c.add(status == null ? Restrictions.isNull("status") : Restrictions.eq("status", status));
List result = c.list();

代码示例来源:origin: iluwatar/java-design-patterns

@Override
 public Spell findByName(String name) {
  Transaction tx = null;
  Spell result = null;
  try (Session session = getSessionFactory().openSession()) {
   tx = session.beginTransaction();
   Criteria criteria = session.createCriteria(persistentClass);
   criteria.add(Restrictions.eq("name", name));
   result = (Spell) criteria.uniqueResult();
   tx.commit();
  } catch (Exception e) {
   if (tx != null) {
    tx.rollback();
   }
   throw e;
  }
  return result;
 }
}

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

Criteria crit = getSession().createCriteria(Discussion.class);
crit.createAlias("participants", "p");

crit.add(Restrictions.eq("p.elements", portalUsername));

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

/**
 * Check if authority exists.
 *
 * @param authority the authority
 * @return true if authority exists
 */
public boolean isAuthorityExists(String authority) {
 Criteria criteria = getSession().createCriteria(Authority.class);
 criteria.add(Restrictions.eq(AUTHORITY_PROPERTY, authority));
 List<Authority> resultList = criteria.list();
 return !resultList.isEmpty();
}

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

@Override
@SuppressWarnings({"unchecked", "deprecation"})
public <T> List<T> findByExample(
    @Nullable final String entityName, final T exampleEntity, final int firstResult, final int maxResults)
    throws DataAccessException {
  Assert.notNull(exampleEntity, "Example entity must not be null");
  return nonNull(executeWithNativeSession((HibernateCallback<List<T>>) session -> {
    Criteria executableCriteria = (entityName != null ?
        session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass()));
    executableCriteria.add(Example.create(exampleEntity));
    prepareCriteria(executableCriteria);
    if (firstResult >= 0) {
      executableCriteria.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
      executableCriteria.setMaxResults(maxResults);
    }
    return executableCriteria.list();
  }));
}

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

@Override
  public Object doInTransaction(TransactionStatus transactionStatus) {
    return sessionFactory.getCurrentSession()
        .createCriteria(PipelineState.class)
        .add(Restrictions.eq("pipelineName", pipelineName))
        .setCacheable(false).uniqueResult();
  }
});

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

/**
 * Find user by username.
 *
 * @param userName the username of user
 * @return user
 */
public User findByUserName(String userName) {
 Criteria criteria = getCriteria();
 criteria.add(Restrictions.eq(USERNAME_PROPERTY, userName));
 return (User) criteria.uniqueResult();
}

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

protected Criteria getCriteria(Session s) {
    // should use RootEntityTransformer by default
    return s.createCriteria( Student.class, "s" )
        .add( Restrictions.eq( "studentNumber", shermanExpected.getStudentNumber() ) );
    }
};

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

@Override
public boolean validateName(String tenantId, String ecfId, String name) {
 LOG.debug("Validating by tenant id [{}], ecf id [{}], name [{}]", tenantId, ecfId, name);
 Criteria criteria = getCriteria();
 criteria.createAlias(TENANT_PROPERTY, TENANT_ALIAS);
 criteria.add(Restrictions.and(
   Restrictions.eq(TENANT_REFERENCE, Long.valueOf(tenantId)),
   Restrictions.eq(NAME_PROPERTY, name)));
 if (isNotBlank(ecfId)) {
  criteria = criteria.add(Restrictions.ne(ID_PROPERTY, Long.valueOf(ecfId)));
 }
 List<EventClassFamily> eventClassFamilies = findListByCriteria(criteria);
 boolean result = eventClassFamilies == null || eventClassFamilies.isEmpty();
 LOG.debug("[{},{},{}] Validating result: {}", tenantId, ecfId, name, result);
 return result;
}

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

@Test
public void shouldNotRetrieveSubSubSubEntityWithCriteria() {
  session = openSession();
  try {
    SubSubSubEntity loaded = (SubSubSubEntity) session.createCriteria( SubSubSubEntity.class )
        .add( Restrictions.idEq( subSubEntityId ) )
        .uniqueResult();
    assertNull( loaded );
  }
  finally {
    session.close();
  }
}

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

protected List<T> findListByCriterionWithAlias(String path, String alias,
                        JoinType type, Criterion criterion) {
 String className = getSimpleClassName();
 LOG.trace("Searching {} entities by criterion [{}] ", className, criterion);
 Criteria criteria = getCriteria();
 if (type == null) {
  criteria.createAlias(path, alias);
 } else {
  criteria.createAlias(path, alias, type);
 }
 criteria.add(criterion);
 List<T> resultList = criteria.list();
 if (resultList == null) {
  resultList = Collections.emptyList();
 }
 return resultList;
}

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

protected T findOneByCriterionWithAlias(String path, String alias, Criterion criterion) {
 String className = getSimpleClassName();
 LOG.trace("Searching {} entity by criterion [{}] ", className, criterion);
 Criteria criteria = getCriteria();
 criteria.createAlias(path, alias);
 criteria.add(criterion);
 return (T) criteria.uniqueResult();
}

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

private Criteria getCriteria(Session s) {
  Criteria crit = s.createCriteria( A.class, "anAlias" );
  crit.add( Restrictions.naturalId().set( "name", "name1" ) );
  crit.setFlushMode( FlushMode.COMMIT );
  crit.setCacheable( true );
  return crit;
}

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

public EnvironmentVariables load(final Long entityId, final EnvironmentVariableType type) {
  List<EnvironmentVariable> result = (List<EnvironmentVariable>) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EnvironmentVariable.class).add(Restrictions.eq("entityId", entityId)).add(
        Restrictions.eq("entityType", type.toString())).addOrder(Order.asc("id"));
    criteria.setCacheable(true);
    return criteria.list();
  });
  return new EnvironmentVariables(result);
}

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

@Override
public List<CtlSchema> findAllByMetaInfoId(String metaInfoId) {
 LOG.debug("Searching available version of ctl schema by meta info id [{}]", metaInfoId);
 Criteria criteria = getCriteria()
   .createAlias(CTL_SCHEMA_META_INFO_PROPERTY, CTL_SCHEMA_META_INFO_ALIAS)
   .add(Restrictions.eq(CTL_SCHEMA_META_INFO_ALIAS_ID, Long.valueOf(metaInfoId)))
   .addOrder(Order.asc(CTL_SCHEMA_VERSION));
 List<CtlSchema> schemas = findListByCriteria(criteria);
 if (LOG.isTraceEnabled()) {
  LOG.trace("Search result: [{}].", Arrays.toString(schemas.toArray()));
 } else {
  LOG.debug("Search result: [{}].", schemas.size());
 }
 return schemas;
}

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

@Test
  public void testEnablingJoinFetchProfileAgainstSelfReferentialAssociation() {
    Session s = openSession();
    s.beginTransaction();
    s.enableFetchProfile( Employee.FETCH_PROFILE_TREE );
    s.createCriteria( Employee.class )
        .add( Restrictions.isNull( "manager" ) )
        .list();
    s.getTransaction().commit();
    s.close();
  }
}

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

@Override
public VersionInfo findByComponentName(final String name) {
  return (VersionInfo) transactionTemplate.execute((TransactionCallback) transactionStatus -> sessionFactory.getCurrentSession()
      .createCriteria(VersionInfo.class)
      .add(Restrictions.eq("componentName", name))
      .setCacheable(true).uniqueResult());
}

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

/**
 * Find user by password reset hash.
 *
 * @param passwordResetHash the password reset hash
 * @return user
 */
public User findByPasswordResetHash(String passwordResetHash) {
 Criteria criteria = getCriteria();
 criteria.add(Restrictions.eq(PASSWORD_RESET_HASH_PROPERTY, passwordResetHash));
 return (User) criteria.uniqueResult();
}

相关文章