Java Code Examples for org.hibernate.criterion.DetachedCriteria#addOrder()

The following examples show how to use org.hibernate.criterion.DetachedCriteria#addOrder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HomePageController.java    From TinyMooc with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@RequestMapping("goCourseHome.htm")
public ModelAndView goCourseHome(HttpServletRequest request) {
    String filterType = request.getParameter("filterType");
    DetachedCriteria dc = DetachedCriteria.forClass(UserCourse.class)
            .add(Restrictions.eq("userPosition", "创建者")).createCriteria("course")
            .add(Restrictions.eq("courseState", "批准"))
            .add(Restrictions.isNull("course"));
    if (filterType == null) {
        // 默认
        dc.addOrder(Order.desc("applyDate"));
    } else if (filterType.equals("grade"))
        dc.addOrder(Order.desc("totalMark"));
    else if (filterType.equals("time"))
        dc.addOrder(Order.desc("approveDate"));
    else if (filterType.equals("hot"))
        dc.addOrder(Order.desc("scanNum"));

    int pageSize = 12;
    int totalPage = userService.countTotalPage(dc, pageSize);
    PageHelper.forPage(totalPage, pageSize);
    List<UserCourse> list1 = (List<UserCourse>) userService.getByPage(dc, pageSize);
    request.setAttribute("list1", list1);
    return new ModelAndView("/homePage/allcourse");
}
 
Example 2
Source File: BaseDaoImpl.java    From csustRepo with MIT License 6 votes vote down vote up
/**
	 * 通过Criteria来查询
	 * @param criterion
	 * @param order
	 * @return 查询实体对象的结果列表
	 */
	@Transactional
	public List<T> cquery(Collection<Criterion> criterions,
			Collection<Order> orders) {
//		Session session=getSessionBeginTx();
//		
//		Criteria criteria = criteriaQuery(session,criterions, orders);
//		
//		List<T> list= criteria.list();
//		//提交事务
//		closeCommitTx(session);
		DetachedCriteria dc=DetachedCriteria.forClass(entityClass);
		if(criterions!=null){
			for (Criterion criterion : criterions) {
				dc.add(criterion);
			}
		}
		if(orders!=null){
			for (Order order : orders) {
				dc.addOrder(order);
			}
		}
		
		return getHibernateTemplate().findByCriteria(dc);
	}
 
Example 3
Source File: PrivateMailController.java    From TinyMooc with Apache License 2.0 5 votes vote down vote up
@RequestMapping("getDetail.htm")
public ModelAndView getDetail(HttpServletRequest request) {
    String senderId = request.getParameter("userId");
    User user = (User) request.getSession().getAttribute("user");
    User sender = privateMailService.findById(User.class, senderId);

    //打开会话的时候 ,将与这个sender相关信息全更成已读
    DetachedCriteria detachedCriteria1 = DetachedCriteria.forClass(Message.class);
    detachedCriteria1.add(Restrictions.eq("userBySenderId", sender));
    detachedCriteria1.add(Restrictions.eq("userByReceiverId", user));
    detachedCriteria1.add(Restrictions.eq("messageState", "未读"));

    List<Message> tempList = privateMailService.queryAllOfCondition(Message.class, detachedCriteria1);
    for (int i = 0; i < tempList.size(); i++) {
        Message message = tempList.get(i);
        message.setMessageState("已读");
        privateMailService.update(message);
    }
    int sum = privateMailService.sumMail(user.getUserId());
    HttpSession session = request.getSession();
    session.setAttribute("sumMail", sum);
    int credit = user.getCredit();
    Level level = userService.getUserLevel(credit);

    //查询回复列表
    DetachedCriteria detachedCriteria2 = DetachedCriteria.forClass(Message.class);
    detachedCriteria2.add(Restrictions.or(Restrictions.and(Restrictions.eq("userBySenderId", sender), Restrictions.eq("userByReceiverId", user)), Restrictions.and(Restrictions.eq("userByReceiverId", sender), Restrictions.eq("userBySenderId", user))));
    detachedCriteria2.addOrder(Order.desc("sendDate"));
    int pageSize = 8;
    int totalPage = privateMailService.countTotalPage(detachedCriteria2, pageSize);
    PageHelper.forPage(totalPage, pageSize);
    List<Message> list = (List<Message>) privateMailService.getByPage(detachedCriteria2, pageSize);
    request.setAttribute("sender", sender);
    request.setAttribute("level", level);
    request.setAttribute("num", list.size());
    request.setAttribute("list", list);

    return new ModelAndView("/privateMail/scanMail");
}
 
Example 4
Source File: HomePageController.java    From TinyMooc with Apache License 2.0 4 votes vote down vote up
/**
 * 我的萌课
 *
 * @param request
 * @return ModelAndView
 * @Date 2015年12月2日15:16:07
 */
@RequestMapping("myTinyMooc.htm")
public ModelAndView myTinyMooc(HttpServletRequest request) {
    User user = (User) request.getSession().getAttribute("user");

    String message = "";
    if (user == null) {
        message = "请先登录啊( ̄▽ ̄)";
        return new ModelAndView("/login/login", "message", message);
    } else {
        int credit = user.getCredit();
        Level level = userService.getUserLevel(credit);
        String sessionId = (String) request.getSession().getAttribute("userId");
        //热门标签
        DetachedCriteria dCriteriaLabel = DetachedCriteria.forClass(Label.class);
        dCriteriaLabel.addOrder(Order.desc("frequency"));
        List<Label> labelList = userService.queryMaxNumOfCondition(Label.class, dCriteriaLabel, 8);
        //课程推荐
        DetachedCriteria dCriteriaCourse = DetachedCriteria.forClass(Course.class);
        dCriteriaCourse.addOrder(Order.desc("scanNum"));
        dCriteriaCourse.add(Restrictions.eq("courseState", "批准"));
        // TODO 是否取消课程的自关联
        dCriteriaCourse.add(Restrictions.isNull("course"));
        List<Course> courseList1 = userService.queryMaxNumOfCondition(Course.class, dCriteriaCourse, 6);

        List<UserCourse> hotCourseList = new ArrayList<>();
        for (int i = 0; i < courseList1.size(); i++) {
            Course course = courseList1.get(i);
            DetachedCriteria dCriteriaUserCourse = DetachedCriteria.forClass(UserCourse.class);
            dCriteriaUserCourse.add(Restrictions.eq("course", course));
            List<UserCourse> userCourseList = userService.queryAllOfCondition(UserCourse.class, dCriteriaUserCourse);
            // UserCourse中以创建者作为标识
            for (int j = 0; j < userCourseList.size(); j++) {
                if (userCourseList.get(j).getUserPosition().equals("创建者")) {
                    hotCourseList.add(userCourseList.get(j));
                    break;
                }
            }
        }
        //达人推荐
        DetachedCriteria dCriteria2 = DetachedCriteria.forClass(User.class)
                .addOrder(Order.desc("credit"))
                .add(Restrictions.ne("userId", user.getUserId()));
        List<User> expertList = userService.queryMaxNumOfCondition(User.class, dCriteria2, 4);
        // 封装数据
        request.setAttribute("hotCourseList", hotCourseList);
        request.setAttribute("labelList", labelList);
        request.setAttribute("expertList", expertList);
        return new ModelAndView("/homePage/myTinyMooc", "level", level);
    }
}
 
Example 5
Source File: HomePageController.java    From TinyMooc with Apache License 2.0 4 votes vote down vote up
@RequestMapping("recommendTeam.htm")
public ModelAndView recommendTeam(HttpServletRequest request) {
    User user = (User) request.getSession().getAttribute("user");
    int credit = user.getCredit();
    Level level = userService.getUserLevel(credit);
    //热门标签
    DetachedCriteria detachedCriteria1 = DetachedCriteria.forClass(Label.class);
    detachedCriteria1.addOrder(Order.desc("frequency"));
    List<Label> labelList = userService.queryMaxNumOfCondition(Label.class, detachedCriteria1, 9);
    //达人推荐
    DetachedCriteria detachedCriteria2 = DetachedCriteria.forClass(User.class)
            .addOrder(Order.desc("credit"))
            .add(Restrictions.ne("userId", user.getUserId()));
    List<User> expertList = userService.queryMaxNumOfCondition(User.class, detachedCriteria2, 4);
    //小组推荐
    DetachedCriteria detachedCriteria3 = DetachedCriteria.forClass(LabelObject.class)
            .add(Restrictions.eq("objectId", user.getUserId()));
    List<LabelObject> labelObjectList = userService.queryAllOfCondition(LabelObject.class, detachedCriteria3);
    List<UserTeam> teamList = new ArrayList<UserTeam>();

    for (int i = 0; i < labelObjectList.size(); i++) {
        Label label = labelObjectList.get(i).getLabel();
        DetachedCriteria detachedCriteria4 = DetachedCriteria.forClass(LabelObject.class)
                .add(Restrictions.eq("label", label))
                .add(Restrictions.eq("objectType", "team"));
        List<LabelObject> labelObjectTempList = userService.queryAllOfCondition(LabelObject.class, detachedCriteria4);
        for (int j = 0; j < labelObjectTempList.size(); j++) {
            String objectId = labelObjectTempList.get(j).getObjectId();
            User userTest = userService.findById(User.class, objectId);
            if (userTest == null) {
                break;
            }
            DetachedCriteria dCriteriaTest = DetachedCriteria.forClass(UserTeam.class)
                    .add(Restrictions.eq("user", userTest))
                    .add(Restrictions.eq("userPosition", "组长"))
                    .add(Restrictions.eq("userState", "批准"));
            List<UserTeam> list = userService.queryAllOfCondition(UserTeam.class, dCriteriaTest);
            UserTeam userTeamTemp = null;
            if (list.size() == 0) {
                continue;
            } else {
                userTeamTemp = list.get(0);
            }
            teamList.add(userTeamTemp);
        }
    }
    request.getSession().setAttribute("labelList", labelList);
    request.getSession().setAttribute("expertList", expertList);
    request.getSession().setAttribute("teamList", teamList);
    request.getSession().setAttribute("level", level);
    return new ModelAndView("/homePage/recommendTeam");
}