OJB中的多条件查询
2008-01-05 08:26:31 来源:WEB开发网核心提示: 项目还是一样,以一个reservation为例,OJB中的多条件查询,一个reservation就是一个课程在实际中的体现,它有开始和终止时间还有状态等信息,我要查询课程,输入的参数是一个时间段,timeslot就是定义了他的时间,里面有三个字段(timeslotID,start,end,reservationID
项目还是一样,以一个reservation为例,一个reservation就是一个课程在实际中的体现,它有开始和终止时间还有状态等信息。timeslot就是定义了他的时间,里面有三个字段(timeslotID,start,end,reservationID)。
1。我要查询课程,输入的参数是一个时间段,得到的是一个reservation的列表。我可以这样做
1。我要查询课程,输入的参数是一个时间段,得到的是一个reservation的列表。我可以这样做
- /*
- * (non-javadoc)
- * @see org.pie.vls.application.VLSBase#init()
- */
- public void init() {
- try {
- broker = PersistenceBrokerFactory.defaultPersistenceBroker();
- broker.clearCache();
- //general criteria, with all the query condition
- Criteria crit = new Criteria();
- Criteria crit1 = null;
- List criteriaList = new Vector();
- //if the reservation id is not 0
- if (resvID > 0) {
- crit.addNotEqualTo("resvID", new Integer(this.resvID));
- }
- Iterator iter = this.reservationTimeslotList.iterator();
- while (iter.hasNext()) {
- //logger.debug ("TIMESLOT -----");
- ReservationTimeslot reservationTimeslot = (ReservationTimeslot) iter.next();
- /**
- * set the query condition list
- * start and end
- */
- startTimeUTC = reservationTimeslot.getStartDateTimeUTC();
- endTimeUTC = reservationTimeslot.getEndDateTimeUTC();
- if (this.selectType == Constants.BETWEEN_RESERVATIONS) {
- crit1 = new Criteria();
- //logger.debug ("BETWEEN " + startTimeUTC + " " + endTimeUTC);
- crit1.addBetween("start", startTimeUTC, endTimeUTC);
- Criteria critMore1 = new Criteria();
- critMore1.addBetween("end", startTimeUTC, endTimeUTC);
- Criteria critMore2 = new Criteria();
- critMore2.addLessOrEqualThan("start", startTimeUTC);
- critMore2.addGreaterOrEqualThan("end", endTimeUTC);
- crit1.addOrCriteria(critMore1);
- crit1.addOrCriteria(critMore2);
- }
- criteriaList.add(crit1.copy(true, true, true));
- }
- if (criteriaList.size() > 0) {
- crit1 = new Criteria();
- for (int i = 0; i < criteriaList.size(); i++) {
- crit1.addOrCriteria((Criteria) criteriaList.get(i));
- }
- crit.addAndCriteria(crit1);
- }
- logger.debug(crit);
- QueryByCriteria tquery = QueryFactory.newQuery(Timeslot.class, crit);
- this.timeslots = broker.getCollectionByQuery(tquery);
- broker.clearCache();
- broker.close();
- } catch (Throwable t) {
- logger.error(t.getMessage(), t);
- }
- }
[]
赞助商链接