首页 > 学院 > 开发设计 > 正文

OJB中的多条件查询

2019-11-18 15:31:18
字体:
来源:转载
供稿:网友
    项目还是一样,以一个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. }



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表