Java Code Examples for org.hibernate.Query.setDate()

The following are Jave code examples for showing how to use setDate() of the org.hibernate.Query class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: SchTtableServer   File: CourseDaoImpl.java   Source Code and License Vote up 6 votes
@Override
public List<CourseBean> getList(String userKey, Date dateStart, int days) {
    if (userKey == null || dateStart == null || days <= 0) {
        return null;
    }
    
    String hql = "FROM CourseBean WHERE userKey = :userKey"
            + " AND date >= :dateStart AND date < :dateEnd"
            + " ORDER BY date, courseOrder";
    
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(hql);
    query.setString("userKey", userKey);
    query.setDate("dateStart", dateStart);
    query.setDate("dateEnd", DateUtil.addDate(dateStart, days));
    
    return (List<CourseBean>) query.list();
}
 
Example 2
Project: SchTtableServer   File: CourseDaoImpl.java   Source Code and License Vote up 6 votes
@Override
public CourseBean get(String userKey, Date date, int courseOrder) {
    if (userKey == null || date == null || courseOrder <= 0) {
        return null;
    }
    
    String hql = "FROM CourseBean WHERE userKey = :userKey"
            + " AND date = :date AND courseOrder = :courseOrder";
    
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(hql);
    query.setString("userKey", userKey);
    query.setDate("date", date);
    query.setInteger("courseOrder", courseOrder);
    List list = query.list();
    if (list != null && !list.isEmpty()) {
        return (CourseBean) list.get(0);
    }
    return null;
}
 
Example 3
Project: SchTtableServer   File: CourseDaoImpl.java   Source Code and License Vote up 6 votes
@Override
public int getCount(String userKey, Date dateStart, int days) {
    if (userKey == null || dateStart == null || days <= 0) {
        return -1;
    }
    
    String hql = "SELECT SUM(courseNum) FROM CourseBean WHERE userKey = :userKey"
            + " AND date >= :dateStart AND date < :dateEnd";
    
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(hql);
    query.setString("userKey", userKey);
    query.setDate("dateStart", dateStart);
    query.setDate("dateEnd", DateUtil.addDate(dateStart, days));
    
    Long sum = (Long) query.uniqueResult();
    return sum != null ? sum.intValue() : 0;
}
 
Example 4
Project: unitimes   File: Location.java   Source Code and License Vote up 6 votes
public Set<Long> findClassLocationTable(int startSlot, int length, Vector<Date> dates) {
	String datesStr = "";
	for (int i=0; i<dates.size(); i++) {
		if (i>0) datesStr += ", ";
		datesStr += ":date"+i;
	}
	Query q = LocationDAO.getInstance().getSession()
	    .createQuery("select distinct e.clazz.uniqueId from " +
	    		"ClassEvent e inner join e.meetings m where " +
        		"m.locationPermanentId=:permanentId and " +
        		"m.stopPeriod>:startSlot and :endSlot>m.startPeriod and " + // meeting time within given time period
        		"m.meetingDate in ("+datesStr+")") // and date
        .setLong("permanentId",getPermanentId())
        .setInteger("startSlot", startSlot)
        .setInteger("endSlot", startSlot + length);
	for (int i=0; i<dates.size(); i++) {
		q.setDate("date"+i, dates.elementAt(i));
	}
	return new HashSet<Long>(q.setCacheable(true).list());
}
 
Example 5
Project: sdudoc   File: LogDaoImpl.java   Source Code and License Vote up 5 votes
@Override
public void deleteLogDaysAgo(int days) {
	String sql = "delete from SysLog where time<:date";
	Session session = sessionFactory.getCurrentSession();
	Query query = session.createQuery(sql);
	Date date = new Date();// 取时间
	Calendar calendar = new GregorianCalendar();
	calendar.setTime(date);
	calendar.add(Calendar.DATE, -days);
	date = calendar.getTime(); 
	System.out.println(date);
	query.setDate("date", date);
	query.executeUpdate();
}
 
Example 6
Project: unitimes   File: Student.java   Source Code and License Vote up 5 votes
public static Hashtable<Long,Set<Long>> findConflictingStudents(Long classId, int startSlot, int length, List<Date> dates) {
	Hashtable<Long,Set<Long>> table = new Hashtable();
	if (dates.isEmpty()) return table;
	String datesStr = "";
	for (int i=0; i<dates.size(); i++) {
		if (i>0) datesStr += ", ";
		datesStr += ":date"+i;
	}
	Query q = LocationDAO.getInstance().getSession()
	    .createQuery("select distinct e.clazz.uniqueId, e.student.uniqueId "+
	        	"from StudentClassEnrollment e, ClassEvent c inner join c.meetings m, StudentClassEnrollment x "+
	        	"where x.clazz.uniqueId=:classId and x.student=e.student and " + // only look among students of the given class 
	        	"e.clazz=c.clazz and " + // link ClassEvent c with StudentClassEnrollment e
        		"m.stopPeriod>:startSlot and :endSlot>m.startPeriod and " + // meeting time within given time period
        		"m.meetingDate in ("+datesStr+") and m.approvalStatus = 1")
        .setLong("classId",classId)
        .setInteger("startSlot", startSlot)
        .setInteger("endSlot", startSlot + length);
	for (int i=0; i<dates.size(); i++) {
		q.setDate("date"+i, dates.get(i));
	}
    for (Iterator i = q.setCacheable(true).list().iterator();i.hasNext();) {
        Object[] o = (Object[])i.next();
        Set<Long> set = table.get((Long)o[0]);
        if (set==null) {
        	set = new HashSet<Long>();
        	table.put((Long)o[0], set);
        }
        set.add((Long)o[1]);
    }
    return table;
}
 
Example 7
Project: unitimes   File: Location.java   Source Code and License Vote up 5 votes
public static Hashtable<Long,Set<Long>> findClassLocationTable(Long sessionId, int startSlot, int length, Vector<Date> dates) {
	Hashtable<Long,Set<Long>> table = new Hashtable();
	String datesStr = "";
	for (int i=0; i<dates.size(); i++) {
		if (i>0) datesStr += ", ";
		datesStr += ":date"+i;
	}
	Query q = LocationDAO.getInstance().getSession()
	    .createQuery("select distinct r.uniqueId, e.clazz.uniqueId from " +
	    		"ClassEvent e inner join e.meetings m, Location r where " +
        		"r.session.uniqueId=:sessionId and r.permanentId=m.locationPermanentId and " + // link Location r with Meeting m
        		"m.stopPeriod>:startSlot and :endSlot>m.startPeriod and " + // meeting time within given time period
        		"m.meetingDate in ("+datesStr+")")
        .setLong("sessionId",sessionId)
        .setInteger("startSlot", startSlot)
        .setInteger("endSlot", startSlot + length);
	for (int i=0; i<dates.size(); i++) {
		q.setDate("date"+i, dates.elementAt(i));
	}
    for (Iterator i = q.setCacheable(true).list().iterator();i.hasNext();) {
        Object[] o = (Object[])i.next();
        Set<Long> ids = table.get((Long)o[0]);
        if (ids==null) {
        	ids = new HashSet<Long>();
        	table.put((Long)o[0], ids);
        }
        ids.add((Long)o[1]);
    }
    return table;
}
 
Example 8
Project: unitimes   File: Location.java   Source Code and License Vote up 4 votes
public static Hashtable<Long,Set<Long>> findClassLocationTable(Long sessionId, Set<Long> permanentIds, int startSlot, int length, List<Date> dates) {
	if (permanentIds.isEmpty() || dates.isEmpty()) return new Hashtable<Long,Set<Long>>();
	EventDateMapping.Class2EventDateMap class2eventMap = EventDateMapping.getMapping(sessionId);
	String datesStr = "";
	for (int i=0; i<dates.size(); i++) {
		if (i>0) datesStr += ", ";
		datesStr += ":date"+i;
	}
	Hashtable<Long,Set<Long>> table = new Hashtable<Long,Set<Long>>();
	Iterator<Long> permanentIdIterator = permanentIds.iterator();
	while (permanentIdIterator.hasNext()){
 	String permIds = "";
 	Long permanentId;
 	int cntPermIds = 0;
 	while(permanentIdIterator.hasNext() && cntPermIds < 1000){
 		permanentId = permanentIdIterator.next();
 		if (permIds.length()>0) permIds += ",";
 		permIds += permanentId;
 		cntPermIds++;
 	}
 	Query q = LocationDAO.getInstance().getSession()
 	    .createQuery("select distinct m.locationPermanentId, e.clazz.uniqueId from " +
 	    		"ClassEvent e inner join e.meetings m where " +
         		"m.locationPermanentId in ("+permIds+") and " +
         		"m.stopPeriod>:startSlot and :endSlot>m.startPeriod and " + // meeting time within given time period
         		"m.meetingDate in ("+datesStr+") and m.approvalStatus = 1") // and date
         .setInteger("startSlot", startSlot)
         .setInteger("endSlot", startSlot + length);
 	for (int i=0; i<dates.size(); i++) {
 		q.setDate("date"+i, class2eventMap.getEventDate(dates.get(i)));
 	}
     for (Iterator i = q.setCacheable(true).list().iterator();i.hasNext();) {
         Object[] o = (Object[])i.next();
         Set<Long> ids = table.get((Long)o[0]);
         if (ids==null) {
         	ids = new HashSet<Long>();
         	table.put((Long)o[0], ids);
         }
         ids.add((Long)o[1]);
     }
	}
    return table;
}
 
Example 9
Project: unitimes   File: Location.java   Source Code and License Vote up 4 votes
public static Hashtable<Long,Set<Event>> findEventTable(Long sessionId, Set<Long> permanentIds, int startSlot, int length, List<Date> dates) {
	if (permanentIds.isEmpty() || dates.isEmpty()) return new Hashtable<Long,Set<Event>>();
	EventDateMapping.Class2EventDateMap class2eventMap = EventDateMapping.getMapping(sessionId);
	String datesStr = "";
	for (int i=0; i<dates.size(); i++) {
		if (i>0) datesStr += ", ";
		datesStr += ":date"+i;
	}

	Hashtable<Long,Set<Event>> table = new Hashtable<Long,Set<Event>>();
	Iterator<Long> permanentIdIterator = permanentIds.iterator();
	while (permanentIdIterator.hasNext()){
 	String permIds = "";
 	Long permanentId;
 	int cntPermIds = 0;
 	while(permanentIdIterator.hasNext() && cntPermIds < 1000){
 		permanentId = permanentIdIterator.next();
 		if (permIds.length()>0) permIds += ",";
 		permIds += permanentId;
 		cntPermIds++;
 	}
	
 	Query q = LocationDAO.getInstance().getSession()
 	    .createQuery("select distinct m.locationPermanentId, e from " +
 	    		"Event e inner join e.meetings m where " +
 	    		"e.class!=ClassEvent and "+
         		"m.locationPermanentId in ("+permIds+") and " +
         		"m.stopPeriod>:startSlot and :endSlot>m.startPeriod and " + // meeting time within given time period
         		"m.meetingDate in ("+datesStr+") and m.approvalStatus = 1") // and date
         .setInteger("startSlot", startSlot)
         .setInteger("endSlot", startSlot + length);
 	for (int i=0; i<dates.size(); i++) {
 		q.setDate("date"+i, class2eventMap.getEventDate(dates.get(i)));
 	}
     for (Iterator i = q.setCacheable(true).list().iterator();i.hasNext();) {
         Object[] o = (Object[])i.next();
         Set<Event> events = table.get((Long)o[0]);
         if (events==null) {
         	events = new HashSet<Event>();
         	table.put((Long)o[0], events);
         }
         events.add((Event)o[1]);
     }
	}
    return table;
}
 
Example 10
Project: ctsms   File: QueryParameterValue.java   Source Code and License Vote up 4 votes
public void set(Query query, int pos, HashMap<NamedParameterValues, Object> namedParameterValuesCache) throws Exception {
	switch (type) {
		case CRITERION_VALUE:
			QueryUtil.setQueryParameterFromCriterion(propertyName, query, valueType, pos, criterion);
			break;
		case FILTER_VALUE:
			QueryUtil.setQueryParameterFromString(query, propertyClass, pos, value);
			break;
		case STATIC_TERM_VALUE:
			QueryUtil.setQueryParameterFromCriterion(null, query, valueType, pos, criterion);
			break;
		case TODAY:
			query.setDate(pos, getCachedTime(namedParameterValuesCache));
			break;
		case NOW:
			query.setTimestamp(pos, CommonUtil.dateToTimestamp(getCachedTime(namedParameterValuesCache)));
			break;
		case TODAY_PLUS_VARIABLE_PERIOD:
			if (VariablePeriod.EXPLICIT.equals(period)) {
				throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.NAMED_PARAMETER_EXPLICIT_VARIABLE_PERIOD,
						DefaultMessages.NAMED_PARAMETER_EXPLICIT_VARIABLE_PERIOD));
			}
			query.setDate(pos, DateCalc.addInterval(getCachedTime(namedParameterValuesCache), period, null));
			break;
		case TODAY_MINUS_VARIABLE_PERIOD:
			if (VariablePeriod.EXPLICIT.equals(period)) {
				throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.NAMED_PARAMETER_EXPLICIT_VARIABLE_PERIOD,
						DefaultMessages.NAMED_PARAMETER_EXPLICIT_VARIABLE_PERIOD));
			}
			query.setDate(pos, DateCalc.subInterval(getCachedTime(namedParameterValuesCache), period, null));
			break;
		case NOW_PLUS_VARIABLE_PERIOD:
			if (VariablePeriod.EXPLICIT.equals(period)) {
				throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.NAMED_PARAMETER_EXPLICIT_VARIABLE_PERIOD,
						DefaultMessages.NAMED_PARAMETER_EXPLICIT_VARIABLE_PERIOD));
			}
			query.setTimestamp(pos, CommonUtil.dateToTimestamp(DateCalc.addInterval(getCachedTime(namedParameterValuesCache), period, null)));
			break;
		case NOW_MINUS_VARIABLE_PERIOD:
			if (VariablePeriod.EXPLICIT.equals(period)) {
				throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.NAMED_PARAMETER_EXPLICIT_VARIABLE_PERIOD,
						DefaultMessages.NAMED_PARAMETER_EXPLICIT_VARIABLE_PERIOD));
			}
			query.setTimestamp(pos, CommonUtil.dateToTimestamp(DateCalc.subInterval(getCachedTime(namedParameterValuesCache), period, null)));
			break;
		case CONTEXT_USER_ID:
			query.setBigInteger(pos, new BigInteger(getCachedUser(namedParameterValuesCache).getId().toString()));
			break;
		case CONTEXT_USER_DEPARTMENT_ID:
			query.setBigInteger(pos, new BigInteger(getCachedUserDepartment(namedParameterValuesCache).getId().toString()));
			break;
		case CONTEXT_IDENTITY_ID:
			query.setBigInteger(pos, new BigInteger(getCachedIdentity(namedParameterValuesCache).getId().toString()));
			break;
		case CONTEXT_IDENTITY_DEPARTMENT_ID:
			query.setBigInteger(pos, new BigInteger(getCachedIdentityDepartment(namedParameterValuesCache).getId().toString()));
			break;
		default:
	}
}
 
Example 11
Project: ctsms   File: QueryUtil.java   Source Code and License Vote up 4 votes
public static void setQueryParameterFromCriterion(String propertyName, Query query, CriterionValueType valueType, int pos, CriterionInstantVO value) throws Exception {
	switch (valueType) {
		case BOOLEAN:
			query.setBoolean(pos, value.getBooleanValue());
			break;
		case BOOLEAN_HASH:
			query.setBinary(pos, CryptoUtil.hashForSearch(value.getBooleanValue()));
			break;
		case DATE:
			query.setDate(pos, value.getDateValue());
			break;
		case DATE_HASH:
			query.setBinary(pos, CryptoUtil.hashForSearch(value.getDateValue()));
			break;
		case TIME:
			query.setTime(pos, value.getTimeValue());
			break;
		case TIME_HASH:
			query.setBinary(pos, CryptoUtil.hashForSearch(value.getTimeValue()));
			break;
		case FLOAT:
			query.setFloat(pos, value.getFloatValue().floatValue());
			break;
		case FLOAT_HASH:
			query.setBinary(pos, CryptoUtil.hashForSearch(value.getFloatValue()));
			break;
		case LONG:
			query.setBigInteger(pos, new BigInteger(value.getLongValue().toString()));
			break;
		case LONG_HASH:
			query.setBinary(pos, CryptoUtil.hashForSearch(value.getLongValue()));
			break;
		case STRING:
			query.setString(pos, value.getStringValue());
			break;
		case STRING_HASH:
			query.setBinary(pos, CryptoUtil.hashForSearch(value.getStringValue()));
			break;
		case TIMESTAMP:
			query.setTimestamp(pos, value.getTimestampValue());
			break;
		case TIMESTAMP_HASH:
			query.setBinary(pos, CryptoUtil.hashForSearch(value.getTimestampValue()));
			break;
		case NONE:
			break;
		default:
			// datatype unimplemented
			throw new IllegalArgumentException(MessageFormat.format(CommonUtil.UNSUPPORTED_CRITERION_VALUE_TYPE, valueType.toString()));
	}
}
 
Example 12
Project: ctsms   File: QueryUtil.java   Source Code and License Vote up 4 votes
public static void setQueryParameterFromString(Query query, Class propertyClass, int pos, String value) throws Exception {
	if (propertyClass.equals(String.class)) {
		query.setString(pos, value);
	} else if (propertyClass.equals(Long.class)) {
		query.setBigInteger(pos, new BigInteger(value));
	} else if (propertyClass.equals(java.lang.Long.TYPE)) {
		query.setBigInteger(pos, new BigInteger(value));
	} else if (propertyClass.equals(Integer.class)) {
		query.setInteger(pos, new Integer(value));
	} else if (propertyClass.equals(java.lang.Integer.TYPE)) {
		query.setInteger(pos, new Integer(value));
	} else if (propertyClass.equals(Boolean.class)) {
		query.setBoolean(pos, new Boolean(value));
	} else if (propertyClass.equals(java.lang.Boolean.TYPE)) {
		query.setBoolean(pos, new Boolean(value));
	} else if (propertyClass.equals(Float.class)) {
		query.setFloat(pos, new Float(value));
	} else if (propertyClass.equals(java.lang.Float.TYPE)) {
		query.setFloat(pos, new Float(value));
	} else if (propertyClass.equals(Double.class)) {
		query.setDouble(pos, new Double(value));
	} else if (propertyClass.equals(java.lang.Double.TYPE)) {
		query.setDouble(pos, new Double(value));
	} else if (propertyClass.equals(Date.class)) {
		query.setDate(pos, CommonUtil.parseDate(value, CommonUtil.INPUT_DATE_PATTERN));
	} else if (propertyClass.equals(Timestamp.class)) {
		query.setTimestamp(pos, CommonUtil.dateToTimestamp(CommonUtil.parseDate(value, CommonUtil.INPUT_DATETIME_PATTERN)));
	} else if (propertyClass.equals(VariablePeriod.class)) {
		query.setString(pos, VariablePeriod.fromString(value).name());
	} else if (propertyClass.equals(AuthenticationType.class)) {
		query.setString(pos, AuthenticationType.fromString(value).name());
	} else if (propertyClass.equals(Sex.class)) {
		query.setString(pos, Sex.fromString(value).name());
	} else if (propertyClass.equals(DBModule.class)) {
		query.setString(pos, DBModule.fromString(value).name());
	} else if (propertyClass.equals(HyperlinkModule.class)) {
		query.setString(pos, HyperlinkModule.fromString(value).name());
	} else if (propertyClass.equals(JournalModule.class)) {
		query.setString(pos, JournalModule.fromString(value).name());
	} else if (propertyClass.equals(FileModule.class)) {
		query.setString(pos, FileModule.fromString(value).name());
	} else if (propertyClass.equals(Color.class)) {
		query.setString(pos, Color.fromString(value).name());
	} else if (propertyClass.equals(InputFieldType.class)) {
		query.setString(pos, InputFieldType.fromString(value).name());
	} else if (propertyClass.equals(EventImportance.class)) {
		query.setString(pos, EventImportance.fromString(value).name());
	} else if (propertyClass.equals(ExportStatus.class)) {
		query.setString(pos, ExportStatus.fromString(value).name());
	} else if (propertyClass.isArray() && propertyClass.getComponentType().equals(java.lang.Byte.TYPE)) { // only string hashes supported, no boolean, float, etc...
		query.setBinary(pos, CryptoUtil.hashForSearch(value));
	} else {
		// illegal type...
		throw new IllegalArgumentException(MessageFormat.format(CommonUtil.INPUT_TYPE_NOT_SUPPORTED, propertyClass.toString()));
	}
}