List of usage examples for org.hibernate Query setCacheable
Query<R> setCacheable(boolean cacheable);
From source file:org.cast.isi.service.ISIResponseService.java
License:Open Source License
@SuppressWarnings("unchecked") public List<String> getPagesWithNotes(User student) { if (student.getRole().equals(Role.GUEST)) return Collections.EMPTY_LIST; Query q = Databinder.getHibernateSession().getNamedQuery("FeedbackMessage.getPagesWithNotesByStudent"); q.setParameter("student", student); q.setCacheable(true); return q.list(); }
From source file:org.cast.isi.service.ISIResponseService.java
License:Open Source License
@SuppressWarnings("unchecked") public List<FeedbackMessage> getNotesForPage(User student, String loc) { if (student.getRole().equals(Role.GUEST)) return Collections.EMPTY_LIST; Query q = Databinder.getHibernateSession().getNamedQuery("FeedbackMessage.getNotesForPage"); q.setParameter("student", student); q.setParameter("loc", loc); q.setCacheable(true); return q.list(); }
From source file:org.cast.isi.service.ISIResponseService.java
License:Open Source License
public String locationOfFirstUnreadMessage(User student, ISIXmlSection sec) { List<String> locations = new ArrayList<String>(); if (sec.hasChildren()) { for (XmlSection sec3 : sec.getChildren()) { locations.add((new ContentLoc(sec3)).getLocation()); }//w w w. j a v a 2 s .c o m } else { locations.add(new ContentLoc(sec).getLocation()); } Query q = Databinder.getHibernateSession() .getNamedQuery("FeedbackMessage.getFirstUnreadMessageByStudentAndSection"); q.setParameter("student", student); q.setParameterList("locationList", locations); q.setMaxResults(1); q.setCacheable(true); if (q.list().isEmpty()) return null; FeedbackMessage m = (FeedbackMessage) q.list().get(0); return m.getLocation(); }
From source file:org.cast.isi.service.ISIResponseService.java
License:Open Source License
@SuppressWarnings("unchecked") public List<ISIResponse> getAllWhiteboardResponses() { Query q = Databinder.getHibernateSession().getNamedQuery("ISIResponse.getAllWhiteboardResponses"); q.setCacheable(true); return q.list(); }
From source file:org.cast.isi.service.ISIResponseService.java
License:Open Source License
@SuppressWarnings("unchecked") public List<ISIResponse> getWhiteboardResponsesByPeriod(Period p) { Query q = Databinder.getHibernateSession().getNamedQuery("ISIResponse.getPeriodWhiteboardResponses"); q.setParameter("period", p); q.setCacheable(true); return q.list(); }
From source file:org.cast.isi.service.ISIResponseService.java
License:Open Source License
@SuppressWarnings("unchecked") public List<String> getResponseCollectionNames(IModel<User> mUser) { //Query q = Databinder.getHibernateSession().createQuery("select distinct(p.collectionName) " + // "from ISIPrompt p join p.responses r where p.collectionName is not null and r.valid='true' and r.user.id=:userId"); // TODO heikki verify query is OK Query q = Databinder.getHibernateSession().createQuery( "select distinct(r.prompt.collectionName) from Response r where r.prompt.collectionName is not null and r.valid='true' and r.user.id=:userId"); q.setLong("userId", mUser.getObject().getId()); q.setCacheable(true); return q.list(); }
From source file:org.cast.isi.service.ISIResponseService.java
License:Open Source License
/** * Generate the prompts for this user's responseCollections * @param mUser// www . j av a2 s.c om * @return */ @SuppressWarnings("unchecked") public List<ISIPrompt> getResponseCollectionNamePrompts(IModel<User> mUser) { Query q = Databinder.getHibernateSession().createQuery( "select distinct(r.prompt) from Response r where r.prompt.collectionName is not null and r.prompt.collectionName !='' and r.valid='true' and r.user.id=:userId"); q.setLong("userId", mUser.getObject().getId()); q.setCacheable(true); return q.list(); }
From source file:org.codehaus.grepo.query.hibernate.generator.QueryGeneratorBase.java
License:Apache License
protected void applyCachingSetting(HibernateQueryOptions queryOptions, HibernateQueryExecutionContext context, Query query) { if (HibernateGeneratorUtils.isCachingEnabled(queryOptions, context)) { query.setCacheable(true); String cacheRegion = HibernateGeneratorUtils.getCacheRegion(queryOptions, context); if (cacheRegion != null) { query.setCacheRegion(cacheRegion); }/*from w w w .j a va 2 s.c o m*/ } }
From source file:org.codehaus.groovy.grails.orm.hibernate.metaclass.ExecuteQueryPersistentMethod.java
License:Apache License
@SuppressWarnings("rawtypes") @Override//from ww w . j av a2 s. com protected Object doInvokeInternal(Class clazz, String methodName, Closure additionalCriteria, Object[] arguments) { checkMethodSignature(clazz, arguments); final String query = arguments[0].toString(); final Map queryMetaParams = extractQueryMetaParams(arguments); final List positionalParams = extractPositionalParams(arguments); final Map namedParams = extractNamedParams(arguments); return getHibernateTemplate().executeFind(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(query); SimpleTypeConverter converter = new SimpleTypeConverter(); // process paginate params if (queryMetaParams.containsKey(GrailsHibernateUtil.ARGUMENT_MAX)) { Integer maxParam = converter.convertIfNecessary( queryMetaParams.get(GrailsHibernateUtil.ARGUMENT_MAX), Integer.class); q.setMaxResults(maxParam.intValue()); } if (queryMetaParams.containsKey(GrailsHibernateUtil.ARGUMENT_OFFSET)) { Integer offsetParam = converter.convertIfNecessary( queryMetaParams.get(GrailsHibernateUtil.ARGUMENT_OFFSET), Integer.class); q.setFirstResult(offsetParam.intValue()); } if (queryMetaParams.containsKey(GrailsHibernateUtil.ARGUMENT_CACHE)) { q.setCacheable( ((Boolean) queryMetaParams.get(GrailsHibernateUtil.ARGUMENT_CACHE)).booleanValue()); } if (queryMetaParams.containsKey(GrailsHibernateUtil.ARGUMENT_FETCH_SIZE)) { Integer fetchSizeParam = converter.convertIfNecessary( queryMetaParams.get(GrailsHibernateUtil.ARGUMENT_FETCH_SIZE), Integer.class); q.setFetchSize(fetchSizeParam.intValue()); } if (queryMetaParams.containsKey(GrailsHibernateUtil.ARGUMENT_TIMEOUT)) { Integer timeoutParam = converter.convertIfNecessary( queryMetaParams.get(GrailsHibernateUtil.ARGUMENT_TIMEOUT), Integer.class); q.setFetchSize(timeoutParam.intValue()); } if (queryMetaParams.containsKey(GrailsHibernateUtil.ARGUMENT_READ_ONLY)) { q.setReadOnly( ((Boolean) queryMetaParams.get(GrailsHibernateUtil.ARGUMENT_READ_ONLY)).booleanValue()); } if (queryMetaParams.containsKey(GrailsHibernateUtil.ARGUMENT_FLUSH_MODE)) { q.setFlushMode((FlushMode) queryMetaParams.get(GrailsHibernateUtil.ARGUMENT_FLUSH_MODE)); } // process positional HQL params int index = 0; for (Object parameter : positionalParams) { q.setParameter(index++, parameter instanceof CharSequence ? parameter.toString() : parameter); } // process named HQL params for (Object o : namedParams.entrySet()) { Map.Entry entry = (Map.Entry) o; if (!(entry.getKey() instanceof String)) { throw new GrailsQueryException("Named parameter's name must be of type String"); } String parameterName = (String) entry.getKey(); if (!QUERY_META_PARAMS.contains(parameterName)) { Object parameterValue = entry.getValue(); if (parameterValue == null) { throw new IllegalArgumentException( "Named parameter [" + entry.getKey() + "] value may not be null"); } if (Collection.class.isAssignableFrom(parameterValue.getClass())) { q.setParameterList(parameterName, (Collection) parameterValue); } else if (parameterValue.getClass().isArray()) { q.setParameterList(parameterName, (Object[]) parameterValue); } else if (parameterValue instanceof CharSequence) { q.setParameter(parameterName, parameterValue.toString()); } else { q.setParameter(parameterName, parameterValue); } } } return q.list(); } }); }
From source file:org.codehaus.groovy.grails.orm.hibernate.metaclass.FindAllPersistentMethod.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override//from w ww . java 2s.co m protected Object doInvokeInternal(final Class clazz, String methodName, Closure additionalCriteria, final Object[] arguments) { if (arguments.length == 0) { return getHibernateTemplate().loadAll(clazz); } final Object arg = arguments[0] instanceof CharSequence ? arguments[0].toString() : arguments[0]; // if the arg is an instance of the class find by example if (arg instanceof String) { final String query = ((String) arg).trim(); final String shortName = GrailsNameUtils.getShortName(clazz); if (!query.matches("(?i)from(?-i)\\s+[" + clazz.getName() + "|" + shortName + "].*")) { throw new GrailsQueryException("Invalid query [" + query + "] for domain class [" + clazz + "]"); } return getHibernateTemplate().executeFind(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(query); Object[] queryArgs = null; Map queryNamedArgs = null; int max = retrieveMaxValue(); int offset = retrieveOffsetValue(); boolean useCache = useCache(); if (arguments.length > 1) { if (arguments[1] instanceof Collection) { queryArgs = GrailsClassUtils.collectionToObjectArray((Collection) arguments[1]); } else if (arguments[1].getClass().isArray()) { queryArgs = (Object[]) arguments[1]; } else if (arguments[1] instanceof Map) { queryNamedArgs = (Map) arguments[1]; } } if (queryArgs != null) { for (int i = 0; i < queryArgs.length; i++) { if (queryArgs[i] instanceof CharSequence) { q.setParameter(i, queryArgs[i].toString()); } else { q.setParameter(i, queryArgs[i]); } } } if (queryNamedArgs != null) { for (Object o : queryNamedArgs.entrySet()) { Map.Entry entry = (Map.Entry) o; if (!(entry.getKey() instanceof String)) { throw new GrailsQueryException( "Named parameter's name must be String: " + queryNamedArgs); } String stringKey = (String) entry.getKey(); // Won't try to bind these parameters since they are processed separately if (GrailsHibernateUtil.ARGUMENT_MAX.equals(stringKey) || GrailsHibernateUtil.ARGUMENT_OFFSET.equals(stringKey) || GrailsHibernateUtil.ARGUMENT_CACHE.equals(stringKey)) continue; Object value = entry.getValue(); if (value == null) { q.setParameter(stringKey, null); } else if (value instanceof CharSequence) { q.setParameter(stringKey, value.toString()); } else if (List.class.isAssignableFrom(value.getClass())) { q.setParameterList(stringKey, (List) value); } else if (value.getClass().isArray()) { q.setParameterList(stringKey, (Object[]) value); } else { q.setParameter(stringKey, value); } } } if (max > 0) { q.setMaxResults(max); } if (offset > 0) { q.setFirstResult(offset); } q.setCacheable(useCache); return q.list(); } private boolean useCache() { boolean useCache = false; if (arguments.length > 1 && arguments[arguments.length - 1] instanceof Map) { useCache = retrieveBoolean(arguments[arguments.length - 1], GrailsHibernateUtil.ARGUMENT_CACHE); } return useCache; } private int retrieveMaxValue() { int result = -1; if (arguments.length > 1) { result = retrieveInt(arguments[1], GrailsHibernateUtil.ARGUMENT_MAX); if (arguments.length > 2 && result == -1) { result = retrieveInt(arguments[2], GrailsHibernateUtil.ARGUMENT_MAX); } } return result; } private int retrieveOffsetValue() { int result = -1; if (arguments.length > 1) { if (isMapWithValue(arguments[1], GrailsHibernateUtil.ARGUMENT_OFFSET)) { result = ((Number) ((Map) arguments[1]).get(GrailsHibernateUtil.ARGUMENT_OFFSET)) .intValue(); } if (arguments.length > 2 && result == -1) { if (isMapWithValue(arguments[2], GrailsHibernateUtil.ARGUMENT_OFFSET)) { result = retrieveInt(arguments[2], GrailsHibernateUtil.ARGUMENT_OFFSET); } else if (isIntegerOrLong(arguments[1]) && isIntegerOrLong(arguments[2])) { result = ((Number) arguments[2]).intValue(); } } if (arguments.length > 3 && result == -1) { if (isIntegerOrLong(arguments[3])) { result = ((Number) arguments[3]).intValue(); } } } return result; } private boolean retrieveBoolean(Object param, String key) { boolean value = false; if (isMapWithValue(param, key)) { value = new SimpleTypeConverter().convertIfNecessary(((Map) param).get(key), Boolean.class); } return value; } private int retrieveInt(Object param, String key) { if (isMapWithValue(param, key)) { return new SimpleTypeConverter().convertIfNecessary(((Map) param).get(key), Integer.class); } if (isIntegerOrLong(param)) { return ((Number) param).intValue(); } return -1; } private boolean isIntegerOrLong(Object param) { return (param instanceof Integer) || (param instanceof Long); } private boolean isMapWithValue(Object param, String key) { return (param instanceof Map) && ((Map) param).containsKey(key); } }); } if (clazz.isAssignableFrom(arg.getClass())) { return getHibernateTemplate().executeFind(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Example example = Example.create(arg).ignoreCase(); Criteria crit = session.createCriteria(clazz); crit.add(example); Map argsMap = (arguments.length > 1 && (arguments[1] instanceof Map)) ? (Map) arguments[1] : Collections.EMPTY_MAP; GrailsHibernateUtil.populateArgumentsForCriteria(grailsApplication, clazz, crit, argsMap); return crit.list(); } }); } if (arguments[0] instanceof Map) { return getHibernateTemplate().executeFind(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Criteria crit = session.createCriteria(clazz); GrailsHibernateUtil.populateArgumentsForCriteria(grailsApplication, clazz, crit, (Map) arguments[0]); return crit.list(); } }); } throw new MissingMethodException(methodName, clazz, arguments); }