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.trailmagic.image.hibernate.HibernateImageGroupRepository.java

License:Open Source License

public List<User> getOwnersByType(Type groupType) {
    try {//  w w w. j a  v  a  2 s. c  o m
        Session session = SessionFactoryUtils.getSession(sessionFactory, false);
        Query qry = session.getNamedQuery(GROUP_OWNERS_QRY);
        qry.setString("groupType", groupType.toString());
        qry.setCacheable(true);
        return qry.list();
    } catch (HibernateException e) {
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}

From source file:com.trailmagic.image.hibernate.HibernateImageGroupRepository.java

License:Open Source License

public List<ImageGroup> getByOwnerScreenNameAndType(String screenName, Type groupType) {
    try {// w  w w .  jav a2 s.  c o m
        Session session = SessionFactoryUtils.getSession(sessionFactory, false);
        Query qry = session.getNamedQuery(GROUPS_BY_OWNER_NAME_QRY);
        qry.setString("screenName", screenName);
        qry.setString("groupType", groupType.toString());
        qry.setCacheable(true);
        return qry.list();
    } catch (HibernateException e) {
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}

From source file:com.trailmagic.image.hibernate.HibernateImageGroupRepository.java

License:Open Source License

public ImageGroup getByOwnerNameAndTypeWithFrames(User owner, String groupName, Type groupType) {
    try {/*from  ww w. j  ava2s.  c o  m*/
        Session session = SessionFactoryUtils.getSession(sessionFactory, false);
        Query qry = session.getNamedQuery(GROUP_BY_OWNER_NAME_TYPE_QRY);
        qry.setEntity("owner", owner);
        qry.setString("groupName", groupName);
        qry.setString("groupType", groupType.toString());
        qry.setCacheable(true);
        final ImageGroup group = (ImageGroup) qry.uniqueResult();
        // join fetch stopped working after enabling caching?
        if (group != null && group.getFrames().size() > 0) {
            group.getFrames().first();
        }
        return group;
    } catch (HibernateException e) {
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}

From source file:com.trailmagic.image.hibernate.HibernateImageGroupRepository.java

License:Open Source License

public List<ImageGroup> getByImage(Image image) {
    try {/* w  ww  . j  a v  a2s  .  c  o m*/
        Session session = SessionFactoryUtils.getSession(sessionFactory, false);
        Query qry = session.getNamedQuery(GROUPS_BY_IMAGE_QRY);
        qry.setEntity("image", image);
        qry.setCacheable(true);
        return qry.list();
    } catch (HibernateException e) {
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}

From source file:com.trailmagic.image.hibernate.HibernateImageGroupRepository.java

License:Open Source License

public ImageGroup getRollForImage(Image image) {
    // images should always only be in one roll
    try {//  ww w.  j a  va 2 s.  c om
        Session session = SessionFactoryUtils.getSession(sessionFactory, false);
        Query qry = session.getNamedQuery(ROLL_FOR_IMAGE_QRY);
        qry.setEntity("image", image);
        qry.setCacheable(true);
        List results = qry.list();
        if (results.size() < 1) {
            return null;
        }

        return (ImageGroup) results.get(0);
    } catch (HibernateException e) {
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateEntityDao.java

License:Apache License

@SuppressWarnings({ "unchecked" })
public <U> List<U> findByNamedQuery(final String queryName, final String propertyName,
        final Object propertyValue, final int maxResults, final boolean cachable) {
    Session session = getSessionFactory().getCurrentSession();
    Query queryObject = session.getNamedQuery(queryName);
    if (propertyName != null) {
        queryObject.setParameter(propertyName, propertyValue);
    }//w  ww.j a  v  a2  s. c o  m
    if (maxResults > 0) {
        queryObject.setMaxResults(maxResults);
    }
    queryObject.setCacheable(cachable);
    return queryObject.list();
}

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateEntityDao.java

License:Apache License

@SuppressWarnings({ "unchecked" })
public final <U> U findUniqueByNamedQuery(final String queryName, final String propertyName,
        final Object propertyValue, final String secondPropertyName, final Object secondPropertyValue,
        final boolean cachable) {
    Session session = getSessionFactory().getCurrentSession();
    Query queryObject = session.getNamedQuery(queryName);
    if (propertyName != null) {
        queryObject.setParameter(propertyName, propertyValue);
    }//  ww w  .  j a  v  a  2  s. c  om
    if (secondPropertyName != null) {
        queryObject.setParameter(secondPropertyName, secondPropertyValue);
    }
    queryObject.setCacheable(cachable);
    return (U) queryObject.uniqueResult();
}

From source file:de.fhdo.gui.admin.modules.terminology.Associations.java

License:Apache License

private void initList() {
    logger.debug("initList()");
    // Header/* w w  w.j  a v a 2s.  c  o m*/
    List<GenericListHeaderType> header = new LinkedList<GenericListHeaderType>();
    header.add(new GenericListHeaderType("ID", 90, "", true, "String", true, true, false, false));
    header.add(new GenericListHeaderType("Name vorwrts", 250, "", true, "String", true, true, false, false));
    header.add(
            new GenericListHeaderType("Name rckwrts", 250, "", true, "String", true, true, false, false));
    header.add(new GenericListHeaderType("Codesystem", 350, "", true, "String", true, true, false, false));

    //CacheManager.getInstance().clearAll();
    //clearHibernateCache();
    // Daten laden
    //SessionFactory sf = HibernateUtil.getSessionFactory();
    SessionFactory sf = HibernateUtil.getNewSessionFactory();

    //sf.evictQueries();
    //sf.getCurrentSession().clear();
    /*Cache cache = sf.getCache();
            
    cache.evictEntityRegions();
    cache.evictCollectionRegions();
    cache.evictDefaultQueryRegion();
    cache.evictQueryRegions();
    //cache.evictNaturalIdRegions();
    //cache.evictAllRegions();
            
    cache.evictAllRegions(); // Evict data from all query regions.*/

    Session hb_session = sf.openSession();

    //hb_session.getTransaction().begin();

    List<GenericListRowType> dataList = new LinkedList<GenericListRowType>();
    try {
        String hql = "select distinct at from AssociationType at"
                + " join fetch at.codeSystemEntityVersion csev" + " join csev.codeSystemEntity cse"
                + " left join cse.codeSystemVersionEntityMemberships csvem";
        //+ " left join fetch csvem.codeSystemVersion csv"
        //+ " left join fetch csv.codeSystem";

        Query q = hb_session.createQuery(hql);
        q.setCacheable(false);
        q.setCacheMode(CacheMode.IGNORE);
        //q.setHint("toplink.refresh", "true");

        hb_session.setCacheMode(CacheMode.IGNORE);
        hb_session.clear();
        hb_session.flush();

        List<de.fhdo.terminologie.db.hibernate.AssociationType> list = q.list();

        logger.debug("Anzahl: " + list.size());

        for (int i = 0; i < list.size(); ++i) {
            de.fhdo.terminologie.db.hibernate.AssociationType association = list.get(i);
            GenericListRowType row = createRowFromAssociation(association);

            dataList.add(row);
        }
    } catch (Exception e) {
        LoggingOutput.outputException(e, this);
    } finally {
        hb_session.close();
    }

    // Liste initialisieren
    Include inc = (Include) getFellow("incList");
    Window winGenericList = (Window) inc.getFellow("winGenericList");
    genericList = (GenericList) winGenericList;

    genericList.setListActions(this);
    genericList.setButton_new(true);
    genericList.setButton_edit(true);
    genericList.setButton_delete(true);
    genericList.setListHeader(header);
    genericList.setDataList(dataList);
}

From source file:de.fhdo.gui.admin.modules.terminology.Codesystems.java

License:Apache License

private void initList() {
    logger.debug("initList()");

    try {//w ww  . ja v a  2 s.c o  m
        West title = (West) getFellow("titleItem");
        int selectedIndex = -1;

        // header information for list view
        List<GenericListHeaderType> header = new LinkedList<GenericListHeaderType>();
        header.add(new GenericListHeaderType("ID", 60, "", true, "String", true, true, false, false));
        header.add(new GenericListHeaderType(Labels.getLabel("name"), 0, "", true, "String", true, true, false,
                false));

        // load data from db
        SessionFactory sf = HibernateUtil.getNewSessionFactory();
        Session hb_session = sf.openSession();
        //Session hb_session = HibernateUtil.getSessionFactory().openSession();

        List<GenericListRowType> dataList = new LinkedList<GenericListRowType>();
        try {
            if (mode == Mode.VALUESET) {
                ValueSet selectedVS = null;

                if (selectedItem instanceof ValueSet)
                    selectedVS = (ValueSet) selectedItem;

                String hql = "from ValueSet order by name";

                Query q = hb_session.createQuery(hql);
                q.setCacheable(false);
                q.setCacheMode(CacheMode.IGNORE);

                hb_session.setCacheMode(CacheMode.IGNORE);
                hb_session.clear();
                hb_session.flush();

                logger.debug("hql: " + hql);

                List<ValueSet> vsList = q.list();

                for (int i = 0; i < vsList.size(); ++i) {
                    ValueSet vs = vsList.get(i);
                    GenericListRowType row = createRowFromValueSet(vs);

                    dataList.add(row);

                    if (selectedVS != null) {
                        if (vs.getId().longValue() == selectedVS.getId())
                            selectedIndex = i;
                    }
                }

                // set title
                title.setTitle(Labels.getLabel("valuesets"));
            } else {
                CodeSystem selectedCS = null;

                if (selectedItem instanceof CodeSystem)
                    selectedCS = (CodeSystem) selectedItem;

                String hql = "from CodeSystem order by name";
                Query q = hb_session.createQuery(hql);
                q.setCacheable(false);
                q.setCacheMode(CacheMode.IGNORE);

                hb_session.setCacheMode(CacheMode.IGNORE);
                hb_session.clear();
                hb_session.flush();

                logger.debug("hql: " + hql);
                List<CodeSystem> csList = q.list();

                for (int i = 0; i < csList.size(); ++i) {
                    CodeSystem cs = csList.get(i);
                    GenericListRowType row = createRowFromCodesystem(cs);

                    dataList.add(row);

                    if (selectedCS != null) {
                        if (cs.getId().longValue() == selectedCS.getId())
                            selectedIndex = i;
                    }
                }

                // set title
                title.setTitle(Labels.getLabel("codesystems"));
            }
        } catch (Exception e) {
            LoggingOutput.outputException(e, this);
        } finally {
            hb_session.close();
        }

        // initialize list
        Include inc = (Include) getFellow("incList");
        Window winGenericList = (Window) inc.getFellow("winGenericList");
        genericList = (GenericList) winGenericList;
        genericList.setListId("list");

        genericList.setListActions(this);
        genericList.setButton_new(true);
        genericList.setButton_edit(false);
        genericList.setButton_delete(true);
        genericList.setListHeader(header);
        genericList.setDataList(dataList);

        if (selectedIndex >= 0)
            genericList.setSelectedIndex(selectedIndex);
    } catch (Exception ex) {
        LoggingOutput.outputException(ex, this);
    }

    initListVersion();
}

From source file:de.fhdo.gui.admin.modules.terminology.Codesystems.java

License:Apache License

private void initListVersion() {
    logger.debug("initListVersion()");

    logger.debug("selectedItem: " + selectedItem);

    Include incVersions = (Include) getFellow("incListVersions");
    West title = (West) getFellow("titleVersion");

    if (selectedItem == null) {
        logger.debug("show empty message");
        if (mode == Mode.VALUESET)
            title.setTitle(Labels.getLabel("valuesetVersion"));
        else/*from w  w  w  . ja  va 2 s.  c  om*/
            title.setTitle(Labels.getLabel("codesystemVersion"));

        incVersions.setSrc(null);
        incVersions.setSrc("/gui/templates/MessageInclude.zul?msg=" + Labels.getLabel("noSelection"));
    } else {
        logger.debug("show version list");

        int selectedIndex = -1;

        incVersions.setSrc(null);
        incVersions.setSrc("/gui/templates/GenericList.zul");

        try {
            // header information for list view
            List<GenericListHeaderType> header = new LinkedList<GenericListHeaderType>();
            header.add(new GenericListHeaderType("ID", 60, "", true, "String", true, true, false, false));
            header.add(new GenericListHeaderType(Labels.getLabel("name"), 0, "", true, "String", true, true,
                    false, false));
            header.add(new GenericListHeaderType(Labels.getLabel("status"), 80, "", true, "String", true, true,
                    false, false));

            // load data from db
            Session hb_session = HibernateUtil.getSessionFactory().openSession();
            hb_session.setCacheMode(org.hibernate.CacheMode.IGNORE);
            hb_session.clear();

            List<GenericListRowType> dataList = new LinkedList<GenericListRowType>();
            try {
                if (mode == Mode.VALUESET) {
                    // fill version list with value set versions
                    ValueSet selectedVS = (ValueSet) selectedItem;
                    ValueSetVersion selectedVSV = (ValueSetVersion) selectedItemVersion;

                    String hql = "from ValueSetVersion where valueSetId=:vs_id order by name";
                    Query q = hb_session.createQuery(hql);
                    q.setParameter("vs_id", selectedVS.getId());

                    q.setCacheable(false);
                    q.setCacheMode(CacheMode.IGNORE);

                    hb_session.setCacheMode(CacheMode.IGNORE);
                    hb_session.clear();
                    hb_session.flush();

                    List<ValueSetVersion> vsList = q.list();

                    for (int i = 0; i < vsList.size(); ++i) {
                        ValueSetVersion vsv = vsList.get(i);
                        GenericListRowType row = createRowFromValueSetVersion(vsv);

                        dataList.add(row);

                        if (selectedVSV != null) {
                            if (vsv.getVersionId().longValue() == selectedVSV.getVersionId())
                                selectedIndex = i;
                        }
                    }

                    // set title
                    title.setTitle(Labels.getLabel("valuesetVersion") + " - " + selectedVS.getName());
                } else {
                    // fill version list with code system versions
                    CodeSystem selectedCS = (CodeSystem) selectedItem;
                    CodeSystemVersion selectedCSV = (CodeSystemVersion) selectedItemVersion;

                    String hql = "from CodeSystemVersion where codeSystemId=:cs_id order by name";
                    Query q = hb_session.createQuery(hql);
                    q.setParameter("cs_id", selectedCS.getId());

                    q.setCacheable(false);
                    q.setCacheMode(CacheMode.IGNORE);

                    hb_session.setCacheMode(CacheMode.IGNORE);
                    hb_session.clear();
                    hb_session.flush();

                    List<CodeSystemVersion> csList = q.list();

                    for (int i = 0; i < csList.size(); ++i) {
                        CodeSystemVersion csv = csList.get(i);
                        GenericListRowType row = createRowFromCodesystemVersion(csv);

                        dataList.add(row);

                        if (selectedCSV != null) {
                            if (csv.getVersionId().longValue() == selectedCSV.getVersionId())
                                selectedIndex = i;
                        }
                    }

                    // set title
                    title.setTitle(Labels.getLabel("codesystemVersion") + " - " + selectedCS.getName());
                }
            } catch (Exception e) {
                LoggingOutput.outputException(e, this);
            } finally {
                hb_session.close();
            }

            // initialize list
            Window winGenericList = (Window) incVersions.getFellow("winGenericList");
            genericListVersion = (GenericList) winGenericList;
            genericListVersion.setListId("listVersion");

            genericListVersion.setListActions(this);
            genericListVersion.setButton_new(true);
            genericListVersion.setButton_edit(false);
            genericListVersion.setButton_delete(true);
            genericListVersion.setListHeader(header);
            genericListVersion.setDataList(dataList);

            if (selectedIndex >= 0)
                genericListVersion.setSelectedIndex(selectedIndex);

            Button button = new Button(Labels.getLabel("changeStatus") + "...");
            button.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
                public void onEvent(Event t) throws Exception {
                    changeStatus();
                }
            });

            genericListVersion.removeCustomButtons();
            genericListVersion.addCustomButton(button);
        } catch (Exception ex) {
            LoggingOutput.outputException(ex, this);
        }
    }

    initDetails();
}