Java Code Examples for javax.persistence.criteria.CriteriaBuilder#equal()

The following examples show how to use javax.persistence.criteria.CriteriaBuilder#equal() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public List<String> listUnitAttendanceDetailByYearAndMonth( List<String> unitNames, String year, String month)  throws Exception {
	EntityManager em = this.entityManagerContainer().get( AttendanceDetail.class );
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<AttendanceDetail> root = cq.from( AttendanceDetail.class);
	cq.select( root.get(AttendanceDetail_.id ));
	//一般始终为true, id is not null
	Predicate p = cb.equal( root.get(AttendanceDetail_.recordStatus), 1 );
	if( unitNames != null && unitNames.size() > 0 ){
		p = cb.and(p, root.get(AttendanceDetail_.unitName).in(unitNames));
	}
	if( StringUtils.isNotEmpty( year ) ){
		p = cb.and(p, cb.equal( root.get(AttendanceDetail_.yearString), year ));
	}
	if( StringUtils.isNotEmpty( month ) ){
		p = cb.and(p, cb.equal( root.get(AttendanceDetail_.monthString), month ));
	}
	
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 2
public List<Review> listTaskWithPersonAndParentId(String person, String taskId) throws Exception {
	if( StringUtils.isEmpty( person ) ) {
		throw new Exception("person can not be empty!");
	}
	if( StringUtils.isEmpty( taskId ) ) {
		throw new Exception("taskId can not be empty!");
	}
	EntityManager em = this.entityManagerContainer().get( Review.class );
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Review> cq = cb.createQuery( Review.class );
	Root<Review> root = cq.from( Review.class );
	Predicate p = cb.equal( root.get( Review_.permissionObj ), person );
	p = cb.and( p, cb.equal( root.get( Review_.parent ), taskId ));
	p = cb.and( p, cb.equal( root.get( Review_.deleted ), false ));
	return em.createQuery( cq.where(p) ).getResultList();
}
 
Example 3
Source Project: tutorials   File: CustomItemRepositoryImpl.java    License: MIT License 6 votes vote down vote up
@Override
public List<Item> findItemsByColorAndGrade() {

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Item> criteriaQuery = criteriaBuilder.createQuery(Item.class);
    Root<Item> itemRoot = criteriaQuery.from(Item.class);

    Predicate predicateForBlueColor = criteriaBuilder.equal(itemRoot.get("color"), "blue");
    Predicate predicateForRedColor = criteriaBuilder.equal(itemRoot.get("color"), "red");
    Predicate predicateForColor = criteriaBuilder.or(predicateForBlueColor, predicateForRedColor);

    Predicate predicateForGradeA = criteriaBuilder.equal(itemRoot.get("grade"), "A");
    Predicate predicateForGradeB = criteriaBuilder.equal(itemRoot.get("grade"), "B");
    Predicate predicateForGrade = criteriaBuilder.or(predicateForGradeA, predicateForGradeB);

    // final search filter
    Predicate finalPredicate = criteriaBuilder.and(predicateForColor, predicateForGrade);

    criteriaQuery.where(finalPredicate);

    List<Item> items = entityManager.createQuery(criteriaQuery)
        .getResultList();
    return items;
}
 
Example 4
public List<String> listByTopUnitRecordDateString(String topUnitName, String sDate) throws Exception{
	if( topUnitName == null || topUnitName.isEmpty() ){
		logger.error( new TopUnitNamesEmptyException() );
		return null;
	}
	
	EntityManager em = this.entityManagerContainer().get( StatisticTopUnitForDay.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root< StatisticTopUnitForDay> root = cq.from( StatisticTopUnitForDay.class);
	Predicate p = cb.equal( root.get( StatisticTopUnitForDay_.topUnitName), topUnitName);
	if( sDate == null || sDate.isEmpty() ){
		logger.error( new StatisticDateEmptyException() );
	}else{
		p = cb.and( p, cb.equal( root.get( StatisticTopUnitForDay_.statisticDate), sDate));
	}
	cq.select(root.get( StatisticTopUnitForDay_.id));
	return em.createQuery(cq.where(p)).setMaxResults(62).getResultList();
}
 
Example 5
/**
 * 根据条件查询汇报ID列表
 * @param workId
 * @param activityName
 * @param processStatus
 * @param processIdentity
 * @return
 * @throws Exception 
 */
public List<String> listByWorkId(String workId, String activityName, String processStatus, String processorIdentity ) throws Exception {
	if( workId == null || workId.isEmpty() ){
		throw new Exception( "workId is empty, system can not excute query!" );
	}
	EntityManager em = this.entityManagerContainer().get(OkrWorkReportBaseInfo.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<OkrWorkReportBaseInfo> root = cq.from( OkrWorkReportBaseInfo.class);
	Predicate p = cb.equal( root.get( OkrWorkReportBaseInfo_.workId), workId );
	p = cb.and( p, cb.equal( root.get( OkrWorkReportBaseInfo_.status ), "正常" ) );
	if( processorIdentity != null && !processorIdentity.isEmpty() ){
		p = cb.and( p, cb.isMember( processorIdentity, root.get( OkrWorkReportBaseInfo_.currentProcessorIdentityList )));
	}
	if( activityName != null && !activityName.isEmpty() ){
		p = cb.and( p, cb.equal( root.get( OkrWorkReportBaseInfo_.activityName ), activityName ) );
	}
	if( processStatus != null && !processStatus.isEmpty() ){
		p = cb.and( p, cb.equal( root.get( OkrWorkReportBaseInfo_.processStatus ), processStatus ) );
	}
	cq.select(root.get( OkrWorkReportBaseInfo_.id) );
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 6
ActionResult<List<NameValueCountPair>> execute(EffectivePerson effectivePerson) throws Exception {
	try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
		ActionResult<List<NameValueCountPair>> result = new ActionResult<>();
		Business business = new Business(emc);
		List<NameValueCountPair> wraps = new ArrayList<>();
		EntityManager em = emc.get(Task.class);
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<String> cq = cb.createQuery(String.class);
		Root<Task> root = cq.from(Task.class);
		Predicate p = cb.equal(root.get(Task_.person), effectivePerson.getDistinguishedName());
		cq.select(root.get(Task_.application)).where(p).distinct(true);
		List<String> list = em.createQuery(cq).getResultList();
		for (String str : list) {
			this.addNameValueCountPair(business, effectivePerson, str, wraps);
		}
		SortTools.asc(wraps, false, "name");
		result.setData(wraps);
		return result;
	}
}
 
Example 7
Source Project: onedev   File: NumberCriteria.java    License: MIT License 6 votes vote down vote up
@Override
public Predicate getPredicate(Root<PullRequest> root, CriteriaBuilder builder) {
	Path<Long> attribute = root.get(PullRequest.PROP_NUMBER);
	Predicate numberPredicate;
	
	if (operator == PullRequestQueryLexer.Is)
		numberPredicate = builder.equal(attribute, number.getNumber());
	else if (operator == PullRequestQueryLexer.IsGreaterThan)
		numberPredicate = builder.greaterThan(attribute, number.getNumber());
	else
		numberPredicate = builder.lessThan(attribute, number.getNumber());
	
	return builder.and(
			builder.equal(root.get(PullRequest.PROP_TARGET_PROJECT), number.getProject()),
			numberPredicate);
}
 
Example 8
private List<NameValueCountPair> listWorkStatus(Business business, Application application) throws Exception {
	List<NameValueCountPair> wos = new ArrayList<>();
	EntityManager em = business.entityManagerContainer().get(Work.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<WorkStatus> cq = cb.createQuery(WorkStatus.class);
	Root<Work> root = cq.from(Work.class);
	Predicate p = cb.equal(root.get(Work_.application), application.getId());
	cq.select(root.get(Work_.workStatus)).where(p).distinct(true);
	List<WorkStatus> list = em.createQuery(cq).getResultList();
	for (WorkStatus status : list) {
		NameValueCountPair o = new NameValueCountPair();
		o.setValue(status);
		o.setName(status);
		wos.add(o);
	}
	SortTools.asc(wos, "name");
	return wos;
}
 
Example 9
public List<String> listIdsByCode(String code) throws Exception {
	if( code == null || code.isEmpty() ){
		return null;
	}
	EntityManager em = this.entityManagerContainer().get(SystemConfig.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<SystemConfig> root = cq.from(SystemConfig.class);
	Predicate p = cb.equal(root.get(SystemConfig_.configCode),  code);
	cq.select(root.get(SystemConfig_.id));
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 10
public List<String> listWithCalendarId(String calendarId) throws Exception {
	if( StringUtils.isEmpty( calendarId ) ){
		return null;
	}
	EntityManager em = this.entityManagerContainer().get(Calendar_EventRepeatMaster.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<Calendar_EventRepeatMaster> root = cq.from(Calendar_EventRepeatMaster.class);
	Predicate p = cb.equal( root.get( Calendar_EventRepeatMaster_.calendarId), calendarId);
	cq.select(root.get(Calendar_EventRepeatMaster_.id));
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 11
public List<View> listWithQueryObject(String queryId) throws Exception {
	EntityManager em = this.entityManagerContainer().get(View.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<View> cq = cb.createQuery(View.class);
	Root<View> root = cq.from(View.class);
	Predicate p = cb.equal(root.get(View_.query), queryId);
	cq.select(root).where(p);
	List<View> os = em.createQuery(cq).getResultList();
	return os;
}
 
Example 12
/**
 * 根据工作信息ID,获取汇报基础信息ID列表
 * @param workId
 * @return
 * @throws Exception 
 */
//@MethodDescribe( "根据工作信息ID,获取汇报基础信息ID列表" )
public List<String> listByWorkId(String workId) throws Exception {
	EntityManager em = this.entityManagerContainer().get(OkrWorkReportBaseInfo.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<OkrWorkReportBaseInfo> root = cq.from( OkrWorkReportBaseInfo.class);
	Predicate p = cb.equal( root.get(OkrWorkReportBaseInfo_.workId), workId );
	cq.select(root.get( OkrWorkReportBaseInfo_.id) );
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 13
private String get(Business business, Portal portal, String flag) throws Exception {
	EntityManager em = business.entityManagerContainer().get(File.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<File> root = cq.from(File.class);
	Predicate p = cb.equal(root.get(File_.name), flag);
	p = cb.or(p, cb.equal(root.get(File_.alias), flag));
	p = cb.or(p, cb.equal(root.get(File_.id), flag));
	p = cb.and(p, cb.equal(root.get(File_.portal), portal.getId()));
	List<String> list = em.createQuery(cq.select(root.get(File_.id)).where(p)).setMaxResults(1).getResultList();
	return list.isEmpty() ? null : list.get(0);
}
 
Example 14
/**
 * 查询当前用户会话扩展
 * @param person
 * @param conversationId
 * @return
 * @throws Exception
 */
public IMConversationExt getConversationExt(String person, String conversationId) throws Exception {
	EntityManager em = this.entityManagerContainer().get(IMConversationExt.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<IMConversationExt> cq = cb.createQuery(IMConversationExt.class);
	Root<IMConversationExt> root = cq.from(IMConversationExt.class);
	Predicate p = cb.equal(root.get(IMConversationExt_.person), person);
	p = cb.and(p, cb.equal(root.get(IMConversationExt_.conversationId), conversationId));
	cq.select(root).where(p);
	List<IMConversationExt> list = em.createQuery(cq).getResultList();
	if (list != null && !list.isEmpty()) {
		return list.get(0);
	}
	return null;
}
 
Example 15
public Long countVoteRecordForSubject( String subjectId, String voteOptionId ) throws Exception {
	EntityManager em = this.entityManagerContainer().get( BBSVoteRecord.class );
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<BBSVoteRecord> root = cq.from( BBSVoteRecord.class);
	Predicate p = cb.equal( root.get( BBSVoteRecord_.subjectId ), subjectId );
	if( StringUtils.isNotEmpty( voteOptionId ) ){
		p = cb.and( p, cb.equal( root.get( BBSVoteRecord_.optionId ), voteOptionId ));
	}
	cq.select( cb.count( root ) );
	return em.createQuery(cq.where(p)).getSingleResult();
}
 
Example 16
public Long countWithPersonWithWorkCompleted(String person, WorkCompleted workCompleted) throws Exception {
	EntityManager em = this.entityManagerContainer().get(Read.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<Long> cq = cb.createQuery(Long.class);
	Root<Read> root = cq.from(Read.class);
	Predicate p = cb.equal(root.get(Read_.workCompleted), workCompleted.getId());
	p = cb.and(p, cb.equal(root.get(Read_.person), person));
	cq.select(cb.count(root)).where(p);
	return em.createQuery(cq).getSingleResult();
}
 
Example 17
public List<String> listWithJob(String job) throws Exception {
	EntityManager em = this.entityManagerContainer().get(Read.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<String> cq = cb.createQuery(String.class);
	Root<Read> root = cq.from(Read.class);
	Predicate p = cb.equal(root.get(Read_.job), job);
	cq.select(root.get(Read_.id)).where(p);
	return em.createQuery(cq).getResultList();
}
 
Example 18
public List<DocumentViewRecord> listRecordsByPerson( String personName, Integer maxCount ) throws Exception {
	EntityManager em = this.entityManagerContainer().get( DocumentViewRecord.class );
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<DocumentViewRecord> cq = cb.createQuery( DocumentViewRecord.class );
	Root<DocumentViewRecord> root = cq.from( DocumentViewRecord.class );
	Predicate p = cb.equal( root.get( DocumentViewRecord_.viewerName ), personName );
	cq.orderBy( cb.desc( root.get( DocumentViewRecord_.createTime ) ) );
	return em.createQuery( cq.where(p) ).setMaxResults( maxCount ).getResultList();
}
 
Example 19
public List<TaskList> listWithTaskGroup(String taskGroupId) throws Exception {
	if( StringUtils.isEmpty( taskGroupId ) ){
		throw new Exception("taskGroupId can not be empty!");
	}
	EntityManager em = this.entityManagerContainer().get(TaskList.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<TaskList> cq = cb.createQuery(TaskList.class);
	Root<TaskList> root = cq.from(TaskList.class);
	Predicate p = cb.equal( root.get(TaskList_.taskGroup ), taskGroupId );
	cq.orderBy( cb.asc( root.get( TaskList_.order ) ) );
	return em.createQuery(cq.where(p)).getResultList();
}
 
Example 20
public List<OkrWorkPerson> listDetailWorkPerson(String id, WorkCommonSearchFilter wrapIn) throws Exception {
	if (id == null || id.isEmpty()) {
		throw new Exception("id is null!");
	}
	EntityManager em = this.entityManagerContainer().get(OkrWorkPerson.class);
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<OkrWorkPerson> cq = cb.createQuery(OkrWorkPerson.class);
	Root<OkrWorkPerson> root = cq.from(OkrWorkPerson.class);
	Predicate p = cb.equal(root.get(OkrWorkPerson_.recordType), "具体工作");
	p = cb.and(p, cb.equal(root.get(OkrWorkPerson_.workId), id));
	// 干系人姓名列表
	if (null != wrapIn.getEmployeeNames() && wrapIn.getEmployeeNames().size() > 0) {
		p = cb.and(p, root.get(OkrWorkPerson_.employeeName).in(wrapIn.getEmployeeNames()));
	}
	// 干系人姓名列表
	if (null != wrapIn.getEmployeeIdentities() && wrapIn.getEmployeeIdentities().size() > 0) {
		p = cb.and(p, root.get(OkrWorkPerson_.employeeIdentity).in(wrapIn.getEmployeeIdentities()));
	}
	// 处理身份
	if (null != wrapIn.getProcessIdentities() && wrapIn.getProcessIdentities().size() > 0) {
		p = cb.and(p, root.get(OkrWorkPerson_.processIdentity).in(wrapIn.getProcessIdentities()));
	}
	// 干系人所属组织名称列表
	if (null != wrapIn.getUnitNames() && wrapIn.getUnitNames().size() > 0) {
		p = cb.and(p, root.get(OkrWorkPerson_.unitName).in(wrapIn.getUnitNames()));
	}
	// 干系人所属顶层组织名称列表
	if (null != wrapIn.getTopUnitNames() && wrapIn.getTopUnitNames().size() > 0) {
		p = cb.and(p, root.get(OkrWorkPerson_.topUnitName).in(wrapIn.getTopUnitNames()));
	}
	// 工作类别
	if (null != wrapIn.getWorkTypes() && wrapIn.getWorkTypes().size() > 0) {
		p = cb.and(p, root.get(OkrWorkPerson_.workType).in(wrapIn.getWorkTypes()));
	}
	// 工作时长类型:短期工作|长期工作
	if (null != wrapIn.getWorkDateTimeType() && wrapIn.getWorkDateTimeType().isEmpty()) {
		p = cb.and(p, cb.equal(root.get(OkrWorkPerson_.workDateTimeType), wrapIn.getWorkDateTimeType()));
	}
	// 工作处理状态
	if (null != wrapIn.getWorkProcessStatuses() && wrapIn.getWorkProcessStatuses().size() > 0) {
		p = cb.and(p, root.get(OkrWorkPerson_.workProcessStatus).in(wrapIn.getWorkProcessStatuses()));
	}
	if (null != wrapIn.getInfoStatuses() && wrapIn.getInfoStatuses().size() > 0) {
		p = cb.and(p, root.get(OkrWorkPerson_.status).in(wrapIn.getInfoStatuses()));
	}
	// 部署年份
	if (null != wrapIn.getDeployYear() && !wrapIn.getDeployYear().isEmpty()) {
		p = cb.and(p, cb.equal(root.get(OkrWorkPerson_.deployYear), wrapIn.getDeployYear()));
	}
	// 部署月份
	if (null != wrapIn.getDeployMonth() && !wrapIn.getDeployMonth().isEmpty()) {
		p = cb.and(p, cb.equal(root.get(OkrWorkPerson_.deployMonth), wrapIn.getDeployMonth()));
	}

	return em.createQuery(cq.where(p)).getResultList();
}