List of usage examples for org.hibernate Query setCacheable
Query<R> setCacheable(boolean cacheable);
From source file:com.gisgraphy.domain.repository.CityDao.java
License:Open Source License
@SuppressWarnings("unchecked") public List<City> listByZipCode(final String zipcode, final String countrycode) { return (List<City>) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { String queryString = "from " + City.class.getSimpleName() + " as c left outer join c.zipCodes z where z.code = ?"; if (countrycode != null) { queryString = queryString + " and c.countryCode=?"; }//from ww w.ja va 2 s . com Query qry = session.createQuery(queryString); qry.setCacheable(true); qry.setParameter(0, zipcode); if (countrycode != null) { qry.setParameter(1, countrycode.toUpperCase()); } List<City> results = (List<City>) qry.list(); if (results == null) { results = new ArrayList<City>(); } return results; } }); }
From source file:com.gisgraphy.domain.repository.CountryDao.java
License:Open Source License
public Country getByIso3166Alpha2Code(final String iso3166Alpha2Code) { if (iso3166Alpha2Code == null) { return null; }// w ww . j a va2s . com if (iso3166Alpha2Code.length() != 2) { logger.info("can not retrieve country with iso639Alpha2LanguageCode=" + iso3166Alpha2Code + " : iso639Alpha2LanguageCode must have a length of 2 "); return null; } return (Country) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { String queryString = "from " + Country.class.getSimpleName() + " as c where c.iso3166Alpha2Code= ?"; Query qry = session.createQuery(queryString); qry.setCacheable(true); qry.setParameter(0, iso3166Alpha2Code.toUpperCase()); Country result = (Country) qry.uniqueResult(); return result; } }); }
From source file:com.gisgraphy.domain.repository.CountryDao.java
License:Open Source License
public Country getByIso3166Alpha3Code(final String iso3166Alpha3Code) { if (iso3166Alpha3Code == null) { return null; }//from www .j a va 2 s. co m if (iso3166Alpha3Code.length() != 3) { logger.info("can not retrieve country with iso3166Alpha3Code=" + iso3166Alpha3Code + " : iso3166Alpha3Code must have a length of 3 "); return null; } return (Country) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { String queryString = "from " + Country.class.getSimpleName() + " as c where c.iso3166Alpha3Code= ?"; Query qry = session.createQuery(queryString); qry.setCacheable(true); qry.setParameter(0, iso3166Alpha3Code.toUpperCase()); Country result = (Country) qry.uniqueResult(); return result; } }); }
From source file:com.gisgraphy.domain.repository.CountryDao.java
License:Open Source License
public Country getByName(final String name) { if (name == null) { return null; }//from ww w . ja va 2s . c o m return (Country) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { String queryString = "from " + Country.class.getSimpleName() + " as c where c.name= ?"; Query qry = session.createQuery(queryString); qry.setCacheable(true); qry.setParameter(0, name); Country result = (Country) qry.uniqueResult(); return result; } }); }
From source file:com.gisgraphy.domain.repository.CountryDao.java
License:Open Source License
@SuppressWarnings("unchecked") public List<Country> getAllSortedByName() { return (List<Country>) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { String queryString = "from " + persistentClass.getSimpleName() + " order by name"; Query qry = session.createQuery(queryString); qry.setCacheable(true); List<Country> results = (List<Country>) qry.list(); if (results == null) { results = new ArrayList<Country>(); }/*from w w w . j a v a 2s. c om*/ return results; } }); }
From source file:com.gisgraphy.domain.repository.CountryDao.java
License:Open Source License
@SuppressWarnings("unchecked") public List<Long> listFeatureIds() { return ((List<Long>) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(final Session session) throws PersistenceException { final String queryString = "select featureId from " + persistentClass.getSimpleName(); final Query qry = session.createQuery(queryString); qry.setCacheable(false); return qry.list(); }//from w w w . j a v a2 s.c o m })); }
From source file:com.gisgraphy.domain.repository.GenericDao.java
License:Open Source License
@SuppressWarnings("unchecked") public List<T> getAll() { List<T> returnValue = new ArrayList<T>(); try {/*from ww w. j a v a2 s . c om*/ return (List<T>) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { String queryString = "from " + persistentClass.getSimpleName(); Query qry = session.createQuery(queryString); qry.setCacheable(true); List<T> results = (List<T>) qry.list(); if (results == null) { results = new ArrayList<T>(); } return results; } }); } catch (DataAccessResourceFailureException e) { log.info("could not retrieve all object of type " + persistentClass.getName(), e); } catch (ObjectNotFoundException e) { log.info("could not retrieve object of type " + persistentClass.getName() + " with id ", e); } catch (HibernateException e) { log.info("could not retrieve all object of type " + persistentClass.getName(), e); } catch (IllegalStateException e) { log.info("could not retrieve all object of type " + persistentClass.getName(), e); } return returnValue; }
From source file:com.gisgraphy.domain.repository.GenericDao.java
License:Open Source License
public long count() { return ((Long) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { String queryString = "select count(*) from " + persistentClass.getSimpleName(); Query qry = session.createQuery(queryString); qry.setCacheable(true); Long result = (Long) qry.uniqueResult(); return result; }//from w w w .ja v a 2 s. c o m })).longValue(); }
From source file:com.gisgraphy.domain.repository.GenericDao.java
License:Open Source License
@SuppressWarnings("unchecked") public List<T> getAllPaginate(final int from, final int maxResults) { return (List<T>) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { String queryString = "from " + persistentClass.getSimpleName() + " order by id"; //we order by id because we want to keep the same order query after query Query qry = session.createQuery(queryString); qry.setCacheable(true); if (maxResults > 0) { qry.setMaxResults(maxResults); }// ww w. j a va 2 s . co m if (from >= 1) { qry.setFirstResult(from - 1); } List<T> results = (List<T>) qry.list(); if (results == null) { results = new ArrayList<T>(); } return results; } }); }
From source file:com.gisgraphy.domain.repository.GenericGisDao.java
License:Open Source License
/** * base method for all findNearest* /* w w w .j a va 2 s. c om*/ * * @param point * The point from which we want to find GIS Object * @param pointId * the id of the point that we don't want to be include, it * is used to not include the gisFeature from which we want * to find the nearest * @param distance * distance The radius in meters * @param firstResult * the firstResult index (for pagination), numbered from 1, * if < 1 : it will not be taken into account * @param maxResults * The Maximum number of results to retrieve (for * pagination), if <= 0 : it will not be taken into acount * @param requiredClass * the class of the object to be retireved * @param isMunicipality whether we should filter on city that are flag as 'municipality'. act as a filter, if false it doesn't filters( false doesn't mean that we return non municipality) * @return A List of GisFeatureDistance with the nearest elements or an * emptylist (never return null), ordered by distance.<u>note</u> * the specified gisFeature will not be included into results * @see GisFeatureDistance * @return a list of gisFeature (never return null but an empty list) */ @SuppressWarnings("unchecked") protected List<GisFeatureDistance> getNearestAndDistanceFrom(final Point point, final Long pointId, final double distance, final int firstResult, final int maxResults, final boolean includeDistanceField, final Class<? extends GisFeature> requiredClass, final boolean isMunicipality) { Assert.notNull(point); return (List<GisFeatureDistance>) this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws PersistenceException { Criteria criteria = session.createCriteria(requiredClass); if (maxResults > 0) { criteria = criteria.setMaxResults(maxResults); } if (firstResult >= 1) { criteria = criteria.setFirstResult(firstResult - 1); } criteria = criteria.add(new DistanceRestriction(point, distance)); List<String> fieldList = IntrospectionHelper.getFieldsAsList(requiredClass); ProjectionList projections = ProjectionBean.fieldList(fieldList, true); if (includeDistanceField) { projections.add(SpatialProjection.distance_sphere(point, GisFeature.LOCATION_COLUMN_NAME) .as("distance")); } criteria.setProjection(projections); if (pointId != 0) { // remove The From Point criteria = criteria.add(Restrictions.not(Restrictions.idEq(pointId))); } if (includeDistanceField) { criteria.addOrder(new ProjectionOrder("distance")); } if (isMunicipality && (requiredClass == City.class || requiredClass == GisFeature.class)) { criteria.add(Restrictions.eq(City.MUNICIPALITY_FIELD_NAME, isMunicipality)); } criteria.setCacheable(true); List<Object[]> queryResults = criteria.list(); String[] aliasList; if (includeDistanceField) { aliasList = (String[]) ArrayUtils.add(IntrospectionHelper.getFieldsAsArray(requiredClass), "distance"); } else { aliasList = IntrospectionHelper.getFieldsAsArray(requiredClass); } int idPropertyIndexInAliasList = 0; for (int i = 0; i < aliasList.length; i++) { if (aliasList[i] == "id") { idPropertyIndexInAliasList = i; break; } } boolean hasZipCodesProperty = ZipCodesAware.class.isAssignableFrom(requiredClass); Map<Long, Set<String>> idToZipCodesMap = null; if (hasZipCodesProperty && queryResults.size() > 0) { List<Long> ids = new ArrayList<Long>(); for (Object[] tuple : queryResults) { ids.add((Long) tuple[idPropertyIndexInAliasList]); } String zipCodeQuery = "SELECT code as code,gisfeature as id FROM " + ZipCode.class.getSimpleName().toLowerCase() + " zip where zip.gisfeature in (:ids)"; Query qry = session.createSQLQuery(zipCodeQuery).addScalar("code", Hibernate.STRING) .addScalar("id", Hibernate.LONG); qry.setCacheable(true); qry.setParameterList("ids", ids); List<Object[]> zipCodes = (List<Object[]>) qry.list(); if (zipCodes.size() > 0) { idToZipCodesMap = new HashMap<Long, Set<String>>(); for (Object[] zipCode : zipCodes) { Long idFromZipcode = (Long) zipCode[1]; Set<String> zipCodesFromMap = idToZipCodesMap.get(idFromZipcode); if (zipCodesFromMap == null) { Set<String> zipCodesToAdd = new HashSet<String>(); idToZipCodesMap.put(idFromZipcode, zipCodesToAdd); zipCodesFromMap = zipCodesToAdd; } zipCodesFromMap.add((String) zipCode[0]); } } } List<GisFeatureDistance> results = ResultTransformerUtil.transformToGisFeatureDistance(aliasList, queryResults, idToZipCodesMap, requiredClass); return results; } }); }