下面列出了org.hibernate.criterion.Restrictions#in ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Criterion toHibernateCriterion() {
Criterion restriction = null;
switch (getMatch()) {
case LIKE:
restriction = Restrictions.like(getName(), (String) getValue(), MatchMode.ANYWHERE);
break;
case ILIKE:
restriction = Restrictions.like(getName(), (String) getValue(), MatchMode.ANYWHERE).ignoreCase();
break;
case NOT_EQ:
restriction = Restrictions.ne(getName(), getValue());
break;
case IN:
restriction = Restrictions.in(getName(), (Object[]) getValue());
break;
case NOT_IN:
restriction = Restrictions.not(Restrictions.in(getName(), (Object[]) getValue()));
break;
default:
restriction = Restrictions.eq(getName(), getValue());
break;
}
return restriction;
}
@Override
protected Collection<Job> handleFindPending(Long departmentId, Boolean daily, Boolean weekly, Boolean monthly) throws Exception {
org.hibernate.Criteria jobCriteria = createJobCriteria();
if (departmentId != null) {
jobCriteria.createCriteria("modifiedUser").add(Restrictions.eq("department.id", departmentId.longValue()));
}
if (daily != null || weekly != null || monthly != null) {
org.hibernate.Criteria typeCriteria = jobCriteria.createCriteria("type");
if (daily != null) {
typeCriteria.add(Restrictions.eq("daily", daily.booleanValue()));
}
if (weekly != null) {
typeCriteria.add(Restrictions.eq("weekly", weekly.booleanValue()));
}
if (monthly != null) {
typeCriteria.add(Restrictions.eq("monthly", monthly.booleanValue()));
}
}
Restrictions.in("status", new Object[] {
JobStatus.CREATED,
JobStatus.FAILED,
JobStatus.OK
});
jobCriteria.addOrder(Order.asc("id"));
return jobCriteria.list();
}
public static Criterion CriterionInRestrictionSplitter(String property, Collection<?> values) {
Objects.requireNonNull(property);
Objects.requireNonNull(values);
Criterion criterion = null;
List<?> list = new ArrayList<>(values);
int listSize = list.size();
for (int i = 0; i < listSize; i += MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST) {
List<?> subList;
if (listSize > i + MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST) {
subList = list.subList(i, (i + MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST));
} else {
subList = list.subList(i, listSize);
}
if (criterion == null) {
criterion = Restrictions.in(property, subList);
} else {
criterion = Restrictions.or(criterion, Restrictions.in(property, subList));
}
}
return criterion;
}
public static Criterion CriterionInRestrictionSplitter(String property, Collection<?> values) {
Objects.requireNonNull(property);
Objects.requireNonNull(values);
Criterion criterion = null;
List<?> list = new ArrayList<>(values);
int listSize = list.size();
for (int i = 0; i < listSize; i += MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST) {
List<?> subList;
if (listSize > i + MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST) {
subList = list.subList(i, (i + MAX_NUMBER_OF_SQL_PARAMETERS_IN_LIST));
} else {
subList = list.subList(i, listSize);
}
if (criterion == null) {
criterion = Restrictions.in(property, subList);
} else {
criterion = Restrictions.or(criterion, Restrictions.in(property, subList));
}
}
return criterion;
}
@Override
public Criterion getHibernateCriterion( QueryPath queryPath )
{
Property property = queryPath.getProperty();
if ( property.isCollection() )
{
return Restrictions.in( queryPath.getPath(), getValue( Collection.class, queryPath.getProperty().getItemKlass(), args.get( 0 ) ) );
}
return Restrictions.in( queryPath.getPath(), getValue( Collection.class, queryPath.getProperty().getKlass(), args.get( 0 ) ) );
}
/** {@inheritDoc} */
@Override
public Object visit(Id filter, Object userData) {
String idName;
try {
idName = featureModel.getEntityMetadata().getIdentifierPropertyName();
} catch (LayerException e) {
log.warn("Cannot read idName, defaulting to 'id'", e);
idName = HIBERNATE_ID;
}
Collection<?> c = (Collection<?>) castLiteral(filter.getIdentifiers(), idName);
return Restrictions.in(idName, c);
}
@Override
public List<DocumentDTO> getDocumentForNominalLabelAndCrisis(List<Long> nominalLabelID, Long crisisId) {
List<DocumentDTO> dtoList = new ArrayList<DocumentDTO>();
if (nominalLabelID != null) {
String aliasTable = "documentNominalLabels";
String aliasTableKeyField = "documentNominalLabels.id.nominalLabelId";
Criteria criteria = null;
try {
Criterion criterion = Restrictions.conjunction()
.add(Restrictions.eq("collection.id", crisisId))
.add(Restrictions.eq("hasHumanLabels", true));
Criterion aliasCriterion = Restrictions.in(aliasTableKeyField, nominalLabelID);
// get just the documentIDs
Projection projection = Projections.property("documentId");
//List<Document> docList = this.getByCriteriaWithInnerJoinByOrder(criterion, "DESC", orderBy, null, aliasTable, aliasCriterion);
criteria = createCriteria(criterion, null, null, null, aliasTable, aliasCriterion, null, JoinType.INNER_JOIN);
List<Document> docList = criteria.list();
if (docList != null && !docList.isEmpty()) {
for (Document doc : docList) {
DocumentDTO dto = new DocumentDTO(doc);
dtoList.add(dto);
}
}
} catch (Exception e) {
logger.error("getDocumentCountForNominalLabelAndCrisis failed, criteria = " + criteria.toString(), e);
}
}
return dtoList;
}
private Criterion getTypesCriterion(List<String> types) {
return Restrictions.in("type", types);
}
private Criterion getTypesCriterion(List<String> types) {
return Restrictions.in("type", types);
}
@Override
public List<BookDO> getList(final BaseSearchFilter filter)
{
final BookFilter myFilter;
if (filter instanceof BookFilter) {
myFilter = (BookFilter) filter;
} else {
myFilter = new BookFilter(filter);
}
final QueryFilter queryFilter = new QueryFilter(myFilter);
if (StringUtils.isBlank(myFilter.getSearchString()) == true) {
Collection<BookStatus> col = null;
if (myFilter.isPresent() == true || myFilter.isMissed() == true || myFilter.isDisposed() == true) {
col = new ArrayList<BookStatus>();
if (myFilter.isPresent() == true) {
// Book must be have status 'present'.
col.add(BookStatus.PRESENT);
}
if (myFilter.isMissed() == true) {
// Book must be have status 'missed'.
col.add(BookStatus.MISSED);
}
if (myFilter.isDisposed() == true) {
// Book must be have status 'disposed'.
col.add(BookStatus.DISPOSED);
}
}
myFilter.setIgnoreDeleted(false);
if (col != null) {
final Criterion inCrit = Restrictions.in("status", col);
if (myFilter.isDeleted() == true) {
queryFilter.add(Restrictions.or(inCrit, Restrictions.eq("deleted", true)));
myFilter.setIgnoreDeleted(true);
} else {
queryFilter.add(inCrit);
}
}
}
queryFilter.addOrder(Order.desc("created"));
queryFilter.addOrder(Order.asc("authors"));
return getList(queryFilter);
}
public Criterion buildRestriction(String propertyName, Object[] values) {
return Restrictions.in(propertyName,values);
}
/**
* find by ids.
*
* @param entityClass
* Class
* @param ids
* List
* @param <T>
* generic
* @return List
*/
@Transactional(readOnly = true)
public <T> List<T> findByIds(Class<T> entityClass, List ids) {
Assert.notEmpty(ids);
String idName = this.getIdName(entityClass);
Criterion criterion = Restrictions.in(idName, ids);
return this.find(entityClass, criterion);
}
/**
* 按属性条件参数创建Criterion,辅助函数.
*
* @param propertyName
* String
* @param propertyValue
* Object
* @param matchType
* MatchType
* @return Criterion
*/
public static Criterion buildCriterion(String propertyName,
Object propertyValue, MatchType matchType) {
Assert.hasText(propertyName, "propertyName不能为空");
Criterion criterion = null;
// 根据MatchType构造criterion
switch (matchType) {
case EQ:
criterion = Restrictions.eq(propertyName, propertyValue);
break;
case NOT:
criterion = Restrictions.ne(propertyName, propertyValue);
break;
case LIKE:
criterion = Restrictions.like(propertyName, (String) propertyValue,
MatchMode.ANYWHERE);
break;
case LE:
criterion = Restrictions.le(propertyName, propertyValue);
break;
case LT:
criterion = Restrictions.lt(propertyName, propertyValue);
break;
case GE:
criterion = Restrictions.ge(propertyName, propertyValue);
break;
case GT:
criterion = Restrictions.gt(propertyName, propertyValue);
break;
case IN:
criterion = Restrictions.in(propertyName,
(Collection) propertyValue);
break;
case INL:
criterion = Restrictions.isNull(propertyName);
break;
case NNL:
criterion = Restrictions.isNotNull(propertyName);
break;
default:
criterion = Restrictions.eq(propertyName, propertyValue);
break;
}
return criterion;
}