Java Code Examples for org.hibernate.query.NativeQuery#setMaxResults()
The following examples show how to use
org.hibernate.query.NativeQuery#setMaxResults() .
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: NativeQueryMethodInterceptorImpl.java From spring-native-query with MIT License | 4 votes |
private Object executeWithEntityManager(NativeQueryInfo info) { EntityManager entityManager = ApplicationContextProvider.getApplicationContext().getBean(EntityManager.class); Session session = entityManager.unwrap(Session.class); NativeQuery<?> query; if (info.isEntity()) { query = session.createNativeQuery(info.getSql(), info.getAliasToBean()); } else { query = session.createNativeQuery(info.getSql()); } addParameterJpa(query, info); if (info.hasPagination()) { query.setFirstResult(info.getFirstResult()); query.setMaxResults(info.getMaxResult()); } query.getQueryString(); if (!info.isJavaObject() && !info.isEntity()) { query.setResultTransformer(Transformers.aliasToBean(info.getAliasToBean())); } if (info.getReturnType().getSimpleName().equals(Void.TYPE.getName())) { query.executeUpdate(); return null; } if (info.returnTypeIsOptional()) { return getOptionalReturn(query::getSingleResult); } if (info.isSingleResult()) { return query.getSingleResult(); } List<?> resultList = query.list(); if (info.isPagination()) { return new PageImpl(resultList, info.getPageable(), getTotalRecords(info, session)); } return resultList; }
Example 2
Source File: GradebookDAO.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public List<User> getUsersByLesson(Long lessonId, int page, int size, String sortBy, String sortOrder, String searchString) { final String LOAD_LEARNERS_ORDERED_BY_NAME = "SELECT DISTINCT user.* " + " FROM lams_lesson lesson, lams_group g, lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " WHERE lesson.lesson_id = :lessonId " + " AND lesson.class_grouping_id=g.grouping_id " + " AND ug.group_id=g.group_id " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + "ORDER BY CONCAT(user.last_name, ' ', user.first_name) " + sortOrder; //when :sortBy='timeTaken' final String LOAD_LEARNERS_ORDERED_BY_TIME_TAKEN = "SELECT DISTINCT user.* " + " FROM lams_lesson lesson, lams_group g, lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " LEFT OUTER JOIN lams_learner_progress progress " + " ON progress.user_id=user.user_id AND progress.lesson_id=:lessonId " + " WHERE lesson.lesson_id = :lessonId " + " AND lesson.class_grouping_id=g.grouping_id " + " AND ug.group_id=g.group_id " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + "ORDER BY TIMEDIFF(progress.finish_date_time, progress.start_date_time) " + sortOrder; //when :sortBy='mark' final String LOAD_LEARNERS_ORDERED_BY_MARK = "SELECT DISTINCT user.* " + " FROM lams_lesson lesson, lams_group g, lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " LEFT OUTER JOIN lams_gradebook_user_lesson gradebookUserLesson " + " ON user.user_id=gradebookUserLesson.user_id AND gradebookUserLesson.lesson_id =:lessonId " + " WHERE lesson.lesson_id = :lessonId " + " AND lesson.class_grouping_id=g.grouping_id " + " AND ug.group_id=g.group_id " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + " ORDER BY gradebookUserLesson.mark " + sortOrder; //when :sortBy='feedback' final String LOAD_LEARNERS_ORDERED_BY_FEEDBACK = "SELECT DISTINCT user.* " + " FROM lams_lesson lesson, lams_group g, lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " LEFT OUTER JOIN lams_gradebook_user_lesson gradebookUserLesson " + " ON user.user_id=gradebookUserLesson.user_id AND gradebookUserLesson.lesson_id =:lessonId " + " WHERE lesson.lesson_id = :lessonId " + " AND lesson.class_grouping_id=g.grouping_id " + " AND ug.group_id=g.group_id " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + " ORDER BY gradebookUserLesson.feedback " + sortOrder; String queryString; if (sortBy.equals("timeTaken")) { queryString = LOAD_LEARNERS_ORDERED_BY_TIME_TAKEN; } else if (sortBy.equals("mark")) { queryString = LOAD_LEARNERS_ORDERED_BY_MARK; } else if (sortBy.equals("feedback")) { queryString = LOAD_LEARNERS_ORDERED_BY_FEEDBACK; } else { queryString = LOAD_LEARNERS_ORDERED_BY_NAME; } @SuppressWarnings("unchecked") NativeQuery<User> query = getSession().createSQLQuery(queryString); query.addEntity(User.class); query.setParameter("lessonId", lessonId); // support for custom search from a toolbar searchString = searchString == null ? "" : searchString; query.setParameter("searchString", searchString); query.setFirstResult(page * size); query.setMaxResults(size); return query.list(); }
Example 3
Source File: GradebookDAO.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public List<User> getUsersByActivity(Long lessonId, Long activityId, int page, int size, String sortBy, String sortOrder, String searchString) { final String LOAD_LEARNERS_ORDERED_BY_NAME = "SELECT DISTINCT user.* " + " FROM lams_lesson lesson, lams_group g, lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " WHERE lesson.lesson_id = :lessonId " + " AND lesson.class_grouping_id=g.grouping_id " + " AND ug.group_id=g.group_id " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + " ORDER BY CONCAT(user.last_name, ' ', user.first_name) " + sortOrder; //when :sortBy='timeTaken' final String LOAD_LEARNERS_ORDERED_BY_TIME_TAKEN_ACTIVITY = "SELECT DISTINCT user.* " + " FROM lams_lesson lesson, lams_group g, lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " LEFT OUTER JOIN lams_learner_progress progress " + " INNER JOIN lams_progress_completed completedActivityProgress " + " ON completedActivityProgress.learner_progress_id=progress.learner_progress_id " + " AND completedActivityProgress.activity_id=:activityId " + " ON progress.user_id=user.user_id " + " WHERE lesson.lesson_id = :lessonId " + " AND lesson.class_grouping_id=g.grouping_id " + " AND ug.group_id=g.group_id " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + "ORDER BY TIMEDIFF(completedActivityProgress.completed_date_time, completedActivityProgress.start_date_time) " + sortOrder; //when :sortBy='mark' final String LOAD_LEARNERS_ORDERED_BY_MARK_ACTIVITY = "SELECT DISTINCT user.* " + " FROM lams_lesson lesson, lams_group g, lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " LEFT OUTER JOIN lams_gradebook_user_activity gradebookUserActivity " + " ON user.user_id=gradebookUserActivity.user_id AND gradebookUserActivity.activity_id =:activityId " + " WHERE lesson.lesson_id = :lessonId " + " AND lesson.class_grouping_id=g.grouping_id " + " AND ug.group_id=g.group_id " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + "ORDER BY gradebookUserActivity.mark " + sortOrder; String queryString; if (sortBy.equals("timeTaken")) { queryString = LOAD_LEARNERS_ORDERED_BY_TIME_TAKEN_ACTIVITY; } else if (sortBy.equals("mark")) { queryString = LOAD_LEARNERS_ORDERED_BY_MARK_ACTIVITY; } else { queryString = LOAD_LEARNERS_ORDERED_BY_NAME; } @SuppressWarnings("unchecked") NativeQuery<User> query = getSession().createSQLQuery(queryString); query.addEntity(User.class); query.setParameter("lessonId", lessonId); if (sortBy.equals("timeTaken") || sortBy.equals("mark")) { query.setParameter("activityId", activityId); } // support for custom search from a toolbar searchString = searchString == null ? "" : searchString; query.setParameter("searchString", searchString); query.setFirstResult(page * size); query.setMaxResults(size); return query.list(); }
Example 4
Source File: GradebookDAO.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public List<User> getUsersByGroup(Long lessonId, Long activityId, Long groupId, int page, int size, String sortBy, String sortOrder, String searchString) { final String LOAD_LEARNERS_ORDERED_BY_NAME = "SELECT DISTINCT user.* " + " FROM lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " WHERE ug.group_id=:groupId " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + " ORDER BY CONCAT(user.last_name, ' ', user.first_name) " + sortOrder; //when :sortBy='timeTaken' final String LOAD_LEARNERS_ORDERED_BY_TIME_TAKEN_GROUP = "SELECT DISTINCT user.* " + " FROM lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " LEFT OUTER JOIN lams_learner_progress progress " + " INNER JOIN lams_progress_completed completedActivityProgress " + " ON completedActivityProgress.learner_progress_id=progress.learner_progress_id " + " AND completedActivityProgress.activity_id=:activityId " + " ON progress.user_id=user.user_id " + " WHERE ug.group_id=:groupId " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + " ORDER BY TIMEDIFF(completedActivityProgress.completed_date_time, completedActivityProgress.start_date_time) " + sortOrder; //when :sortBy='mark' final String LOAD_LEARNERS_ORDERED_BY_MARK_GROUP = "SELECT DISTINCT user.* " + " FROM lams_user_group ug " + " INNER JOIN lams_user user ON ug.user_id=user.user_id " + " LEFT OUTER JOIN lams_gradebook_user_activity gradebookUserActivity " + " ON user.user_id=gradebookUserActivity.user_id AND gradebookUserActivity.activity_id =:activityId " + " WHERE ug.group_id=:groupId " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + "ORDER BY gradebookUserActivity.mark " + sortOrder; String queryString; if (sortBy.equals("timeTaken")) { queryString = LOAD_LEARNERS_ORDERED_BY_TIME_TAKEN_GROUP; } else if (sortBy.equals("mark")) { queryString = LOAD_LEARNERS_ORDERED_BY_MARK_GROUP; } else { queryString = LOAD_LEARNERS_ORDERED_BY_NAME; } @SuppressWarnings("unchecked") NativeQuery<User> query = getSession().createSQLQuery(queryString); query.addEntity(User.class); if (sortBy.equals("timeTaken") || sortBy.equals("mark")) { query.setParameter("activityId", activityId); } query.setParameter("groupId", groupId); // support for custom search from a toolbar searchString = searchString == null ? "" : searchString; query.setParameter("searchString", searchString); query.setFirstResult(page * size); query.setMaxResults(size); return query.list(); }
Example 5
Source File: AssessmentUserDAOHibernate.java From lams with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("unchecked") @Override public List<AssessmentUserDTO> getPagedUsersBySession(Long sessionId, int page, int size, String sortBy, String sortOrder, String searchString, IUserManagementService userManagementService) { String[] portraitStrings = userManagementService.getPortraitSQL("user.user_id"); StringBuilder bldr = new StringBuilder(LOAD_USERS_ORDERED_BY_SESSION_SELECT) .append(portraitStrings[0]) .append(LOAD_USERS_ORDERED_BY_SESSION_FROM) .append(portraitStrings[1]) .append(LOAD_USERS_ORDERED_BY_SESSION_JOIN); if ( "total".equalsIgnoreCase(sortBy) ) bldr.append(LOAD_USERS_ORDERED_ORDER_BY_TOTAL); else bldr.append(LOAD_USERS_ORDERED_ORDER_BY_NAME); bldr.append(sortOrder); NativeQuery<Object[]> query = getSession().createNativeQuery(bldr.toString()); query.setParameter("sessionId", sessionId); // support for custom search from a toolbar searchString = searchString == null ? "" : searchString; query.setParameter("searchString", searchString); query.setFirstResult(page * size); query.setMaxResults(size); List<Object[]> list = query.list(); ArrayList<AssessmentUserDTO> userDtos = new ArrayList<AssessmentUserDTO>(); if (list != null && list.size() > 0) { for (Object[] element : list) { Long userId = ((Number) element[0]).longValue(); String firstName = (String) element[1]; String lastName = (String) element[2]; String login = (String) element[3]; float grade = element[4] == null ? 0 : ((Number) element[4]).floatValue(); Long portraitId = element[5] == null ? null : ((Number) element[5]).longValue(); AssessmentUserDTO userDto = new AssessmentUserDTO(); userDto.setUserId(userId); userDto.setFirstName(firstName); userDto.setLastName(lastName); userDto.setLogin(login); userDto.setGrade(grade); userDto.setPortraitId(portraitId); userDtos.add(userDto); } } return userDtos; }
Example 6
Source File: AssessmentUserDAOHibernate.java From lams with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("unchecked") @Override public List<AssessmentUserDTO> getPagedUsersBySessionAndQuestion(Long sessionId, Long questionUid, int page, int size, String sortBy, String sortOrder, String searchString, IUserManagementService userManagementService) { String[] portraitStrings = userManagementService.getPortraitSQL("user.user_id"); StringBuilder bldr = new StringBuilder(LOAD_USERS_ORDERED_BY_SESSION_QUESTION_SELECT) .append(portraitStrings[0]) .append(LOAD_USERS_ORDERED_BY_SESSION_QUESTION_FROM) .append(portraitStrings[1]) .append(LOAD_USERS_ORDERED_BY_SESSION_QUESTION_JOIN); if ( "grade".equalsIgnoreCase(sortBy) ) bldr.append(LOAD_USERS_ORDERED_ORDER_BY_RESULT); else bldr.append(LOAD_USERS_ORDERED_ORDER_BY_NAME); bldr.append(sortOrder); NativeQuery<Object[]> query = getSession().createNativeQuery(bldr.toString()); query.setParameter("sessionId", sessionId); query.setParameter("questionUid", questionUid); // support for custom search from a toolbar searchString = searchString == null ? "" : searchString; query.setParameter("searchString", searchString); query.setFirstResult(page * size); query.setMaxResults(size); List<Object[]> list = query.list(); ArrayList<AssessmentUserDTO> userDtos = new ArrayList<AssessmentUserDTO>(); if (list != null && list.size() > 0) { for (Object[] element : list) { Long questionResultUid = ((Number) element[0]).longValue(); String firstName = (String) element[1]; String lastName = (String) element[2]; String login = (String) element[3]; float grade = element[4] == null ? 0 : ((Number) element[4]).floatValue(); Long portraitId = element[5] == null ? null : ((Number) element[5]).longValue(); AssessmentUserDTO userDto = new AssessmentUserDTO(); userDto.setQuestionResultUid(questionResultUid); ; userDto.setFirstName(firstName); userDto.setLastName(lastName); userDto.setLogin(login); userDto.setGrade(grade); userDto.setPortraitId(portraitId); userDtos.add(userDto); } } return userDtos; }
Example 7
Source File: McUserDAO.java From lams with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("unchecked") @Override public List<McUserMarkDTO> getPagedUsersBySession(Long sessionId, int page, int size, String sortBy, String sortOrder, String searchString, IUserManagementService userManagementService) { String[] portraitStrings = userManagementService.getPortraitSQL("user.que_usr_id"); StringBuilder bldr = new StringBuilder(LOAD_USERS_SELECT) .append(portraitStrings[0]) .append(LOAD_USERS_FROM) .append(portraitStrings[1]) .append(LOAD_USERS_JOINWHERE) .append(sortOrder); NativeQuery<Object[]> query = getSession().createSQLQuery(bldr.toString()); query.setParameter("sessionId", sessionId); // support for custom search from a toolbar searchString = searchString == null ? "" : searchString; query.setParameter("searchString", searchString); query.setParameter("sortBy", sortBy); query.setFirstResult(page * size); query.setMaxResults(size); List<Object[]> list = query.list(); ArrayList<McUserMarkDTO> userDtos = new ArrayList<McUserMarkDTO>(); if (list != null && list.size() > 0) { for (Object[] element : list) { Long userUid = ((Number) element[0]).longValue(); Long userId = ((Number) element[1]).longValue(); String fullName = (String) element[2]; Integer totalMark = element[3] == null ? 0 : ((Number) element[3]).intValue(); Long portraitId = element[4] == null ? null : ((Number) element[4]).longValue(); McUserMarkDTO userDto = new McUserMarkDTO(); userDto.setQueUsrId(userUid.toString()); userDto.setUserId(userId.toString()); userDto.setFullName(fullName); userDto.setTotalMark(new Long(totalMark)); userDto.setPortraitId(portraitId==null ? null : portraitId.toString()); userDtos.add(userDto); } } return userDtos; }
Example 8
Source File: TaskListUserDAOHibernate.java From lams with GNU General Public License v2.0 | 4 votes |
@Override @SuppressWarnings({ "unchecked", "rawtypes", "deprecation" }) public Collection<TaskListUserDTO> getPagedUsersBySession(Long sessionId, int page, int size, String sortBy, String sortOrder, String searchString, IUserManagementService userManagementService) { String[] portraitStrings = userManagementService.getPortraitSQL("user.user_id"); StringBuilder bldr = new StringBuilder(LOAD_USERS_SELECT) .append(portraitStrings[0]) .append(LOAD_USERS_FROM) .append(portraitStrings[1]) .append(LOAD_USERS_JOINS) .append(sortOrder); NativeQuery query = getSession().createNativeQuery(bldr.toString()); query.setParameter("sessionId", sessionId); // support for custom search from a toolbar searchString = searchString == null ? "" : searchString; query.setParameter("searchString", searchString); query.setFirstResult(page * size); query.setMaxResults(size); List<Object[]> list = query.list(); //group by userId as long as it returns all completed visitLogs for each user HashMap<Long, TaskListUserDTO> userIdToUserDto = new LinkedHashMap<Long, TaskListUserDTO>(); if (list != null && list.size() > 0) { for (Object[] element : list) { Long userId = ((Number) element[0]).longValue(); String fullName = (String) element[1]; boolean isVerifiedByMonitor = element[2] == null ? false : (Boolean) element[2]; Long completedTaskUid = element[3] == null ? 0 : ((Number) element[3]).longValue(); Long portraitId = element[4] == null ? null : ((Number) element[4]).longValue(); TaskListUserDTO userDto = (userIdToUserDto.get(userId) == null) ? new TaskListUserDTO() : userIdToUserDto.get(userId); userDto.setUserId(userId); userDto.setFullName(fullName); userDto.setVerifiedByMonitor(isVerifiedByMonitor); userDto.getCompletedTaskUids().add(completedTaskUid); userDto.setPortraitId(portraitId); userIdToUserDto.put(userId, userDto); } } return userIdToUserDto.values(); }
Example 9
Source File: TaskListUserDAOHibernate.java From lams with GNU General Public License v2.0 | 4 votes |
@Override @SuppressWarnings({ "unchecked", "rawtypes", "deprecation" }) public Collection<TaskListUserDTO> getPagedUsersBySessionAndItem(Long sessionId, Long taskListItemUid, int page, int size, String sortBy, String sortOrder, String searchString) { String LOAD_USERS = "SELECT user.user_id, CONCAT(user.last_name, ' ', user.first_name), visitLog.complete, visitLog.access_date" + " FROM tl_latask10_user user" + " INNER JOIN tl_latask10_session session" + " ON user.session_uid=session.uid" + " LEFT OUTER JOIN tl_latask10_item_log visitLog " + " ON visitLog.user_uid = user.uid" + " AND visitLog.taskList_item_uid = :taskListItemUid" + " WHERE session.session_id = :sessionId " + " AND (CONCAT(user.last_name, ' ', user.first_name) LIKE CONCAT('%', :searchString, '%')) " + " ORDER BY " + " CASE " + " WHEN :sortBy='userName' THEN CONCAT(user.last_name, ' ', user.first_name) " + " WHEN :sortBy='completed' THEN visitLog.complete " + " WHEN :sortBy='accessDate' THEN visitLog.access_date " + " END " + sortOrder; NativeQuery query = getSession().createNativeQuery(LOAD_USERS); query.setParameter("sessionId", sessionId); query.setParameter("taskListItemUid", taskListItemUid); // support for custom search from a toolbar searchString = searchString == null ? "" : searchString; query.setParameter("searchString", searchString); query.setParameter("sortBy", sortBy); query.setFirstResult(page * size); query.setMaxResults(size); List<Object[]> list = query.list(); Collection<TaskListUserDTO> userDtos = new LinkedList<TaskListUserDTO>(); if (list != null && list.size() > 0) { for (Object[] element : list) { Long userId = ((Number) element[0]).longValue(); String fullName = (String) element[1]; boolean isCompleted = element[2] == null ? false : (Boolean) element[2]; Date accessDate = element[3] == null ? null : new Date(((Timestamp) element[3]).getTime()); TaskListUserDTO userDto = new TaskListUserDTO(); userDto.setUserId(userId); userDto.setFullName(fullName); userDto.setCompleted(isCompleted); userDto.setAccessDate(accessDate); ; userDtos.add(userDto); } } return userDtos; }