当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

OJB中的多条件查询

    项目还是一样,以一个reservation为例,一个reservation就是一个课程在实际中的体现,它有开始和终止时间还有状态等信息。timeslot就是定义了他的时间,里面有三个字段(timeslotID,start,end,reservationID)。

1。我要查询课程,输入的参数是一个时间段,得到的是一个reservation的列表。我可以这样做

  1. /*
  2.  *  (non-Javadoc)
  3.  * @see org.pie.vls.Application.VLSBase#init()
  4.  */
  5. public void init() {
  6.     try {
  7.         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  8.         broker.clearCache();
  9.         //general criteria, with all the query condition
  10.         Criteria crit = new Criteria();
  11.         Criteria crit1 = null;
  12.         List criteriaList = new Vector();
  13.         //if the reservation id is not 0
  14.         if (resvID > 0) {
  15.             crit.addNotEqualTo("resvID"new Integer(this.resvID));
  16.         }
  17.         Iterator iter = this.reservationTimeslotList.iterator();
  18.         while (iter.hasNext()) {
  19.             //logger.debug ("TIMESLOT -----");
  20.             ReservationTimeslot reservationTimeslot = (ReservationTimeslot) iter.next();
  21.             /**
  22.              * set the query condition list
  23.              * start and end
  24.              */
  25.             startTimeUTC = reservationTimeslot.getStartDateTimeUTC();
  26.             endTimeUTC = reservationTimeslot.getEndDateTimeUTC();
  27.             if (this.selectType == Constants.BETWEEN_RESERVATIONS) {
  28.                 crit1 = new Criteria();
  29.                 //logger.debug ("BETWEEN " + startTimeUTC + " " + endTimeUTC);
  30.                 crit1.addBetween("start", startTimeUTC, endTimeUTC);
  31.                 Criteria critMore1 = new Criteria();
  32.                 critMore1.addBetween("end", startTimeUTC, endTimeUTC);
  33.                 Criteria critMore2 = new Criteria();
  34.                 critMore2.addLessOrEqualThan("start", startTimeUTC);
  35.                 critMore2.addGreaterOrEqualThan("end", endTimeUTC);
  36.                 crit1.addOrCriteria(critMore1);
  37.                 crit1.addOrCriteria(critMore2);
  38.                 }
  39.             criteriaList.add(crit1.copy(truetruetrue));
  40.         }
  41.         if (criteriaList.size() > 0) {
  42.             crit1 = new Criteria();
  43.             for (int i = 0; i < criteriaList.size(); i++) {
  44.                 crit1.addOrCriteria((Criteria) criteriaList.get(i));
  45.             }
  46.             crit.addAndCriteria(crit1);
  47.         }
  48.         logger.debug(crit);
  49.         QueryByCriteria tquery = QueryFactory.newQuery(Timeslot.class, crit);
  50.         this.timeslots = broker.getCollectionByQuery(tquery);
  51.         broker.clearCache();
  52.         broker.close();
  53.     } catch (Throwable t) {
  54.         logger.error(t.getMessage(), t);
  55.     }
  56. }


 

2. 如果我要创建一个reservation,要判断的是当前这个创建的reservation是否与其它的reservation有交叉点的时间,那查询条件就这样修改

  1. broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  2. broker.clearCache();
  3. //general criteria, with all the query condition
  4. Criteria crit = new Criteria();
  5. Criteria crit1 = null;
  6. List criteriaList = new Vector();
  7. if (this.selectType == Constants.TOUCHING_RESERVATIONS) {
  8.     crit1 = new Criteria();
  9.     /**
  10. * <p>the start time is in between <code>this.startTimeUTC</code> and <code>this.endTimeUTC</code>
  11.      */
  12.     crit1.addGreaterOrEqualThan("start", startTimeUTC);
  13.     crit1.addLessOrEqualThan("start", endTimeUTC);
  14.     /**
  15. * <p>the end time is in between <code>this.startTimeUTC</code> and <code>this.endTimeUTC</code>
  16.      */
  17.     Criteria crit2 = new Criteria();
  18.     crit2.addGreaterOrEqualThan("end", startTimeUTC);
  19.     crit2.addLessOrEqualThan("end", endTimeUTC);
  20.     /**
  21.      * <p>the start time is in less than <code>this.startTimeUTC</code> and 
  22.      * the end time is greater than <code>this.endTimeUTC</code>
  23.      */
  24.     Criteria crit3 = new Criteria();
  25.     crit3.addLessOrEqualThan("start", startTimeUTC);
  26.     crit3.addGreaterOrEqualThan("end", endTimeUTC);
  27.     crit1.addOrCriteria(crit2);
  28.     crit1.addOrCriteria(crit3);
  29. }
  30. criteriaList.add(crit1.copy(truetruetrue));
  31. if (criteriaList.size() > 0) {
  32.     crit1 = new Criteria();
  33.     for (int i = 0; i < criteriaList.size(); i++) {
  34.         crit1.addOrCriteria((Criteria) criteriaList.get(i));
  35.     }
  36.     crit.addAndCriteria(crit1);
  37. }
  38. QueryByCriteria tquery = QueryFactory.newQuery(Timeslot.class, crit);
  39. this.timeslots = broker.getCollectionByQuery(tquery);
  40. broker.clearCache();
  41. broker.close();
相关内容
赞助商链接