Example usage for org.hibernate Query setCacheable

List of usage examples for org.hibernate Query setCacheable

Introduction

In this page you can find the example usage for org.hibernate Query setCacheable.

Prototype

Query<R> setCacheable(boolean cacheable);

Source Link

Document

Enable/disable second level query (result) caching for this query.

Usage

From source file:com.gisgraphy.domain.repository.AdmDao.java

License:Open Source License

public Adm getAdm3(final String countryCode, final String adm1Code, final String adm2Code,
        final String adm3Code) {
    Assert.notNull(countryCode);//from ww  w. ja  va  2  s  .  c o  m
    Assert.notNull(adm1Code);
    Assert.notNull(adm2Code);
    Assert.notNull(adm3Code);
    return (Adm) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            String queryString = "from " + Adm.class.getSimpleName()
                    + " as a where a.countryCode=? and a.adm2Code=? and a.adm3Code=?";
            if (!"00".equals(adm1Code)) {
                queryString += "and a.adm1Code= ?";
            }
            queryString += " and a.level=3";

            Query qry = session.createQuery(queryString);
            qry.setCacheable(true);

            qry.setParameter(0, countryCode.toUpperCase());
            qry.setParameter(1, adm2Code);
            qry.setParameter(2, adm3Code);

            if (!"00".equals(adm1Code)) {
                qry.setParameter(3, adm1Code);
            }

            Adm result;
            try {
                result = (Adm) qry.uniqueResult();
            } catch (HibernateException e) {
                if (!"00".equals(adm1Code)) {
                    throw e;
                } else {
                    logger.error("Can not retrieve Adm for countrycode=" + countryCode + " and adm2code="
                            + adm2Code + " and adm3code=" + adm3Code + " in flex mode : result is ambiguous");
                    return null;
                }
            }

            return result;
        }
    });
}

From source file:com.gisgraphy.domain.repository.AdmDao.java

License:Open Source License

public Adm getAdm4(final String countryCode, final String adm1Code, final String adm2Code,
        final String adm3Code, final String adm4Code) {
    Assert.notNull(countryCode);/*from   www .  j a va 2 s  .  com*/
    Assert.notNull(adm1Code);
    Assert.notNull(adm2Code);
    Assert.notNull(adm3Code);
    Assert.notNull(adm4Code);

    return (Adm) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            String queryString = "from " + Adm.class.getSimpleName()
                    + " as a where a.countryCode=? and a.adm2Code=? and a.adm3Code=? and a.adm4Code=?";
            if (!"00".equals(adm1Code)) {
                queryString += "and a.adm1Code= ?";
            }
            queryString += " and a.level=4";

            Query qry = session.createQuery(queryString);
            qry.setCacheable(true);

            qry.setParameter(0, countryCode.toUpperCase());
            qry.setParameter(1, adm2Code);
            qry.setParameter(2, adm3Code);
            qry.setParameter(3, adm4Code);

            if (!"00".equals(adm1Code)) {
                qry.setParameter(4, adm1Code);
            }

            Adm result;
            try {
                result = (Adm) qry.uniqueResult();
            } catch (HibernateException e) {
                if (!"00".equals(adm1Code)) {
                    throw e;
                } else {
                    logger.error("Can not retrieve Adm for countrycode=" + countryCode + " and adm2code="
                            + adm2Code + " and adm3code=" + adm3Code + " and adm4code=" + adm4Code
                            + " in flex mode : result is ambiguous");
                    return null;
                }
            }

            return result;
        }
    });
}

From source file:com.gisgraphy.domain.repository.AdmDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Adm> getUnused() {
    return (List<Adm>) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(final Session session) throws PersistenceException {
            final String queryString = "from " + Adm.class.getSimpleName()
                    + " as a where a not in (select distinct(g.adm) from GisFeature g)";

            final Query qry = session.createQuery(queryString);
            qry.setCacheable(true);

            List<Adm> result = qry.list();
            if (result == null) {
                result = new ArrayList<Adm>();
            }/*from www  . j  ava  2  s.  c o  m*/

            return result;
        }
    });
}

From source file:com.gisgraphy.domain.repository.AdmDao.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public List<Adm> getDirties() {
    return (List<Adm>) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(final Session session) throws PersistenceException {
            final String queryString = "from " + Adm.class.getSimpleName() + " as a where a.featureId < 0";

            final Query qry = session.createQuery(queryString);
            qry.setCacheable(true);

            List<Adm> result = qry.list();
            if (result == null) {
                result = new ArrayList<Adm>();
            }/*w  w  w . j  av a  2s .c o  m*/

            return result;
        }
    });
}

From source file:com.gisgraphy.domain.repository.AdmDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Adm> getAllbyLevel(final int level) {
    return (List<Adm>) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(final Session session) throws PersistenceException {
            final String queryString = "from " + Adm.class.getSimpleName() + " as a where a.level=?";

            final Query qry = session.createQuery(queryString);
            qry.setCacheable(true);

            qry.setParameter(0, level);/*from  w w w. ja v  a  2  s .c om*/

            List<Adm> result = qry.list();
            if (result == null) {
                result = new ArrayList<Adm>();
            }

            return result;
        }
    });
}

From source file:com.gisgraphy.domain.repository.AdmDao.java

License:Open Source License

public long countByLevel(final int level) {
    // level is of type int =>could not be null
    return ((Long) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            String queryString = "select count(*) from " + Adm.class.getSimpleName() + " a where a.level=? ";

            Query qry = session.createQuery(queryString);
            qry.setCacheable(true);
            qry.setParameter(0, level);/*from  w w  w .jav  a2 s.c  om*/

            Long result = (Long) qry.uniqueResult();
            return result;
        }
    })).longValue();
}

From source file:com.gisgraphy.domain.repository.AdmDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Adm> getAdmByCountryAndCodeAndLevel(final String countryCode, final String admCode,
        final int level) {
    Assert.notNull(countryCode);/*from  w w  w  . j a v  a  2s  .com*/
    Assert.notNull(admCode);
    // level is a int =>can not be null
    return (List<Adm>) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            String queryString = "from " + Adm.class.getSimpleName() + " as a where  a.countryCode= ? and a.adm"
                    + level + "Code = ?  and a.level=?";

            Query qry = session.createQuery(queryString);
            qry.setCacheable(true);

            qry.setParameter(0, countryCode.toUpperCase());
            qry.setParameter(1, admCode);
            qry.setParameter(2, level);

            List<Adm> result = (List<Adm>) qry.list();
            if (result == null) {
                return new ArrayList<Adm>();
            }
            return result;
        }
    });
}

From source file:com.gisgraphy.domain.repository.AdmDao.java

License:Open Source License

public int deleteAllByLevel(final int level) {
    return ((Integer) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            String queryString = "delete from " + persistentClass.getSimpleName() + " as a where a.level=?";

            Query qry = session.createQuery(queryString);
            qry.setParameter(0, level);/*  w w  w . j  a  va 2s  . com*/
            qry.setCacheable(false);

            return Integer.valueOf(qry.executeUpdate());

        }
    })).intValue();
}

From source file:com.gisgraphy.domain.repository.AdmDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Long> listFeatureIdByLevel(final int level) {
    return ((List<Long>) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(final Session session) throws PersistenceException {
            final String queryString = "select featureId from " + persistentClass.getSimpleName()
                    + " as a where a.level=?";

            final Query qry = session.createQuery(queryString);
            qry.setParameter(0, level);//from w w w  . j  av  a 2 s .  com
            qry.setCacheable(false);
            return qry.list();

        }
    }));
}

From source file:com.gisgraphy.domain.repository.AlternateNameDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<String> getUsedLanguagesCodes() {
    return (List<String>) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(final Session session) throws PersistenceException {
            final String queryString = "select distinct(a.language) from AlternateName a where a.language != null";

            final Query qry = session.createQuery(queryString);
            qry.setCacheable(true);

            List<String> result = (List<String>) qry.list();
            if (result == null) {
                result = new ArrayList<String>();
            }/*from   w  w  w  .  j a v  a 2 s.  c  om*/

            return result;
        }
    });

}