项目还是一样,以一个reservation为例,一个reservation就是一个课程在实际中的体现,它有开始和终止时间还有状态等信息。timeslot就是定义了他的时间,里面有三个字段(timeslotID,start,end,reservationID)。
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);
- }
- }
2. 如果我要创建一个reservation,要判断的是当前这个创建的reservation是否与其它的reservation有交叉点的时间,那查询条件就这样修改
- broker = PersistenceBrokerFactory.defaultPersistenceBroker();
- broker.clearCache();
- //general criteria, with all the query condition
- Criteria crit = new Criteria();
- Criteria crit1 = null;
- List criteriaList = new Vector();
- if (this.selectType == Constants.TOUCHING_RESERVATIONS) {
- crit1 = new Criteria();
- /**
- * <p>the start time is in between <code>this.startTimeUTC</code> and <code>this.endTimeUTC</code>
- */
- crit1.addGreaterOrEqualThan("start", startTimeUTC);
- crit1.addLessOrEqualThan("start", endTimeUTC);
- /**
- * <p>the end time is in between <code>this.startTimeUTC</code> and <code>this.endTimeUTC</code>
- */
- Criteria crit2 = new Criteria();
- crit2.addGreaterOrEqualThan("end", startTimeUTC);
- crit2.addLessOrEqualThan("end", endTimeUTC);
-
- /**
- * <p>the start time is in less than <code>this.startTimeUTC</code> and
- * the end time is greater than <code>this.endTimeUTC</code>
- */
- Criteria crit3 = new Criteria();
- crit3.addLessOrEqualThan("start", startTimeUTC);
- crit3.addGreaterOrEqualThan("end", endTimeUTC);
- crit1.addOrCriteria(crit2);
- crit1.addOrCriteria(crit3);
- }
- 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);
- }
- QueryByCriteria tquery = QueryFactory.newQuery(Timeslot.class, crit);
- this.timeslots = broker.getCollectionByQuery(tquery);
- broker.clearCache();
- broker.close();