Example usage for org.hibernate Query setFlushMode

List of usage examples for org.hibernate Query setFlushMode

Introduction

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

Prototype

@Override
    Query<R> setFlushMode(FlushModeType flushMode);

Source Link

Usage

From source file:org.unitedinternet.cosmo.dao.hibernate.ContentDaoImpl.java

License:Apache License

protected void checkForDuplicateICalUid(ICalendarItem item, CollectionItem parent) {

    // TODO: should icalUid be required?  Currrently its not and all
    // items created by the webui dont' have it.
    if (item.getIcalUid() == null) {
        return;//from  w ww. j a v  a  2  s.  co m
    }

    // ignore modifications
    if (item instanceof NoteItem && ((NoteItem) item).getModifies() != null) {
        return;
    }

    // Lookup item by parent/icaluid
    Query hibQuery = null;
    if (item instanceof NoteItem) {
        hibQuery = getSession().getNamedQuery("noteItemId.by.parent.icaluid")
                .setParameter("parentid", getBaseModelObject(parent).getId())
                .setParameter("icaluid", item.getIcalUid());
    } else {
        hibQuery = getSession().getNamedQuery("icalendarItem.by.parent.icaluid")
                .setParameter("parentid", getBaseModelObject(parent).getId())
                .setParameter("icaluid", item.getIcalUid());
    }
    hibQuery.setFlushMode(FlushMode.MANUAL);

    Long itemId = (Long) hibQuery.uniqueResult();

    // if icaluid is in use throw exception
    if (itemId != null) {
        // If the note is new, then its a duplicate icaluid
        if (getBaseModelObject(item).getId() == -1) {
            Item dup = (Item) getSession().load(HibItem.class, itemId);
            throw new IcalUidInUseException(
                    "iCal uid" + item.getIcalUid() + " already in use for collection " + parent.getUid(),
                    item.getUid(), dup.getUid());
        }
        // If the note exists and there is another note with the same
        // icaluid, then its a duplicate icaluid
        if (getBaseModelObject(item).getId().equals(itemId)) {
            Item dup = (Item) getSession().load(HibItem.class, itemId);
            throw new IcalUidInUseException(
                    "iCal uid" + item.getIcalUid() + " already in use for collection " + parent.getUid(),
                    item.getUid(), dup.getUid());
        }
    }
}

From source file:org.unitedinternet.cosmo.dao.hibernate.ItemDaoImpl.java

License:Apache License

public Ticket findTicket(String key) {
    if (key == null) {
        throw new IllegalArgumentException("key cannot be null");
    }//from  w  ww .j  a va2  s.  com

    try {
        // prevent auto flushing when looking up ticket
        getSession().setFlushMode(FlushMode.MANUAL);
        Query hibQuery = getSession().getNamedQuery("ticket.by.key").setParameter("key", key);
        hibQuery.setCacheable(true);
        hibQuery.setFlushMode(FlushMode.MANUAL);
        return (Ticket) hibQuery.uniqueResult();
    } catch (HibernateException e) {
        getSession().clear();
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}

From source file:org.unitedinternet.cosmo.dao.hibernate.UserDaoImpl.java

License:Apache License

private User findUserByUsernameIgnoreCase(String username) {
    Session session = getSession();/*from  w  ww . jav a  2s .c om*/
    Query hibQuery = session.getNamedQuery("user.byUsername.ignorecase").setParameter("username", username);
    hibQuery.setCacheable(true);
    hibQuery.setFlushMode(FlushMode.MANUAL);
    List users = hibQuery.list();
    if (users.size() > 0) {
        return (User) users.get(0);
    } else {
        return null;
    }
}

From source file:org.unitedinternet.cosmo.dao.hibernate.UserDaoImpl.java

License:Apache License

private User findUserByUsernameOrEmailIgnoreCaseAndId(Long userId, String username, String email) {
    Session session = getSession();//from  w w  w  .j ava 2 s.  c o  m
    Query hibQuery = session.getNamedQuery("user.byUsernameOrEmail.ignorecase.ingoreId")
            .setParameter("username", username).setParameter("email", email).setParameter("userid", userId);
    hibQuery.setCacheable(true);
    hibQuery.setFlushMode(FlushMode.MANUAL);
    List users = hibQuery.list();
    if (users.size() > 0) {
        return (User) users.get(0);
    } else {
        return null;
    }
}

From source file:org.unitedinternet.cosmo.dao.hibernate.UserDaoImpl.java

License:Apache License

private User findUserByEmail(String email) {
    Session session = getSession();//from ww  w . ja va 2 s .co m
    Query hibQuery = session.getNamedQuery("user.byEmail").setParameter("email", email);
    hibQuery.setCacheable(true);
    hibQuery.setFlushMode(FlushMode.MANUAL);
    List users = hibQuery.list();
    if (users.size() > 0) {
        return (User) users.get(0);
    } else {
        return null;
    }
}

From source file:org.unitedinternet.cosmo.dao.hibernate.UserDaoImpl.java

License:Apache License

private User findUserByEmailIgnoreCase(String email) {
    Session session = getSession();/*from  ww  w .  ja  va 2 s .c om*/
    Query hibQuery = session.getNamedQuery("user.byEmail.ignorecase").setParameter("email", email);
    hibQuery.setCacheable(true);
    hibQuery.setFlushMode(FlushMode.MANUAL);
    List users = hibQuery.list();
    if (users.size() > 0) {
        return (User) users.get(0);
    } else {
        return null;
    }
}

From source file:org.unitime.timetable.action.ajax.CourseNumSuggestAction.java

License:Open Source License

/**
 * Retrieve Suggestion List of Course Numbers for a given Subject Area
 *///w ww . j  a  va  2s  .  c om
public Collection getMultipleSuggestionList(HttpServletRequest request, Map map) {

    List result = null;

    // Read form variables -- Classes Schedule Screen
    if (map.get("session") != null && map.get("session") instanceof String && map.get("subjectArea") != null
            && map.get("subjectArea") instanceof String && map.get("courseNumber") != null) {

        StringBuffer query = new StringBuffer();
        query.append("select distinct co.courseNbr ");
        query.append("  from CourseOffering co ");
        query.append(" where co.subjectArea.session.uniqueId = :acadSessionId ");
        query.append("       and co.subjectArea.subjectAreaAbbreviation = :subjectArea");
        query.append("       and co.courseNbr like :courseNbr ");
        query.append(" order by co.courseNbr ");

        CourseOfferingDAO cdao = new CourseOfferingDAO();
        Session hibSession = cdao.getSession();

        Query q = hibSession.createQuery(query.toString());
        q.setFetchSize(5000);
        q.setCacheable(true);
        q.setFlushMode(FlushMode.MANUAL);
        q.setInteger("acadSessionId", Integer.parseInt(map.get("session").toString()));
        q.setString("subjectArea", map.get("subjectArea").toString());
        q.setString("courseNbr", map.get("courseNumber").toString() + "%");

        result = q.list();

        if (result == null)
            result = new ArrayList();

        return result;
    }

    User user = Web.getUser(request.getSession());

    // Security Checks
    if (!Web.isLoggedIn(request.getSession()) || user == null
            || user.getAttribute(Constants.SESSION_ID_ATTR_NAME) == null)
        return new ArrayList();

    // Get Academic Session
    String acadSessionId = user.getAttribute(Constants.SESSION_ID_ATTR_NAME).toString();

    // Read form variables -- Instructional Offerings Screen, Reservations Screen
    if (map.get("subjectAreaId") != null && map.get("courseNbr") != null
            && map.get("subjectAreaId").toString().length() > 0
            && !Constants.ALL_OPTION_VALUE.equals(map.get("subjectAreaId"))) {

        StringBuffer query = new StringBuffer();
        query.append("select distinct co.courseNbr ");
        query.append("  from CourseOffering co ");
        query.append(" where co.subjectArea.session.uniqueId = :acadSessionId ");
        query.append("        and co.subjectArea.uniqueId = :subjectAreaId ");
        query.append("        and co.courseNbr like :courseNbr ");
        //query.append("        and co.isControl = true ");
        query.append(" order by co.courseNbr ");

        CourseOfferingDAO cdao = new CourseOfferingDAO();
        Session hibSession = cdao.getSession();

        Query q = hibSession.createQuery(query.toString());
        q.setFetchSize(5000);
        q.setCacheable(true);
        q.setFlushMode(FlushMode.MANUAL);
        q.setInteger("acadSessionId", Integer.parseInt(acadSessionId));
        q.setInteger("subjectAreaId", Integer.parseInt(map.get("subjectAreaId").toString()));
        q.setString("courseNbr", map.get("courseNbr").toString() + "%");

        result = q.list();
    }

    // Read form variables -- Distribution Preferences Screen
    if (map.get("filterSubjectAreaId") != null
            && !Constants.BLANK_OPTION_VALUE.equals(map.get("filterSubjectAreaId"))
            && !Constants.ALL_OPTION_VALUE.equals(map.get("filterSubjectAreaId"))
            && map.get("filterCourseNbr") != null) {

        StringBuffer query = new StringBuffer();
        query.append("select distinct co.courseNbr ");
        query.append("  from CourseOffering co ");
        query.append(" where co.subjectArea.session.uniqueId = :acadSessionId ");
        query.append("        and co.subjectArea.uniqueId = :subjectAreaId ");
        query.append("        and co.courseNbr like :courseNbr ");
        query.append("        and co.isControl = true ");
        query.append("        and co.instructionalOffering.notOffered = false ");
        query.append(" order by co.courseNbr ");

        CourseOfferingDAO cdao = new CourseOfferingDAO();
        Session hibSession = cdao.getSession();

        Query q = hibSession.createQuery(query.toString());
        q.setFetchSize(5000);
        q.setCacheable(true);
        q.setFlushMode(FlushMode.MANUAL);
        q.setInteger("acadSessionId", Integer.parseInt(acadSessionId));
        q.setInteger("subjectAreaId", Integer.parseInt(map.get("filterSubjectAreaId").toString()));
        q.setString("courseNbr", map.get("filterCourseNbr").toString() + "%");

        result = q.list();
    }

    // Read form variables -- Classes Screen
    if (map.get("subjectAreaIds") != null && map.get("subjectAreaIds") instanceof String
            && map.get("courseNbr") != null) {

        StringBuffer query = new StringBuffer();
        query.append("select distinct co.courseNbr ");
        query.append("  from CourseOffering co ");
        query.append(" where co.subjectArea.session.uniqueId = :acadSessionId ");
        query.append("        and co.subjectArea.uniqueId = :subjectAreaId");
        query.append("        and co.courseNbr like :courseNbr ");
        query.append("        and co.instructionalOffering.notOffered = false ");
        //query.append("        and co.isControl = true ");
        query.append(" order by co.courseNbr ");

        CourseOfferingDAO cdao = new CourseOfferingDAO();
        Session hibSession = cdao.getSession();

        Query q = hibSession.createQuery(query.toString());
        q.setFetchSize(5000);
        q.setCacheable(true);
        q.setFlushMode(FlushMode.MANUAL);
        q.setInteger("acadSessionId", Integer.parseInt(acadSessionId));
        q.setInteger("subjectAreaId", Integer.parseInt(map.get("subjectAreaIds").toString()));
        q.setString("courseNbr", map.get("courseNbr").toString() + "%");

        result = q.list();
    }

    if (result == null)
        result = new ArrayList();

    return result;
}

From source file:ubic.gemma.persistence.service.expression.bioAssayData.DesignElementDataVectorDaoImpl.java

License:Apache License

/**
 * @param  ee      ee/*w  ww  . j a  va2 s  . com*/
 * @param  cs2gene Map of probes to genes.
 * @return         map of vectors to gene ids.
 */
Map<T, Collection<Long>> getVectorsForProbesInExperiments(Long ee, Map<Long, Collection<Long>> cs2gene) {

    // Do not do in clause for experiments, as it can't use the indices
    //language=HQL
    String queryString = "select dedv, dedv.designElement.id from ProcessedExpressionDataVector dedv fetch all properties"
            + " where dedv.designElement.id in ( :cs ) and dedv.expressionExperiment.id = :eeId ";

    Session session = this.getSessionFactory().getCurrentSession();
    org.hibernate.Query queryObject = session.createQuery(queryString);
    queryObject.setReadOnly(true);
    queryObject.setFlushMode(FlushMode.MANUAL);

    Map<T, Collection<Long>> dedv2genes = new HashMap<>();
    StopWatch timer = new StopWatch();
    timer.start();

    queryObject.setLong("eeId", ee);

    int batchSize = 100;
    for (Collection<Long> batch : new BatchIterator<>(cs2gene.keySet(), batchSize)) {
        this.getVectorsBatch(cs2gene, queryObject, dedv2genes, batch);
    }

    if (timer.getTime() > Math.max(200, 20 * dedv2genes.size())) {
        AbstractDao.log.info("Fetched " + dedv2genes.size() + " vectors for " + cs2gene.size() + " probes in "
                + timer.getTime() + "ms\n" + "Vector query was: " + queryString);

    }
    return dedv2genes;
}

From source file:ubic.gemma.persistence.service.expression.bioAssayData.DesignElementDataVectorDaoImpl.java

License:Apache License

Map<T, Collection<Long>> getVectorsForProbesInExperiments(Map<Long, Collection<Long>> cs2gene) {

    //language=HQL
    String queryString = "select dedv, dedv.designElement.id from ProcessedExpressionDataVector dedv fetch all properties"
            + " where dedv.designElement.id in ( :cs ) ";

    Session session = this.getSessionFactory().getCurrentSession();
    org.hibernate.Query queryObject = session.createQuery(queryString);
    queryObject.setReadOnly(true);//ww w.  jav a  2 s  .  c  o m
    queryObject.setFlushMode(FlushMode.MANUAL);

    Map<T, Collection<Long>> dedv2genes = new HashMap<>();
    StopWatch timer = new StopWatch();
    timer.start();

    int batchSize = 100;
    for (Collection<Long> batch : new BatchIterator<>(cs2gene.keySet(), batchSize)) {
        this.getVectorsBatch(cs2gene, queryObject, dedv2genes, batch);
    }

    if (timer.getTime() > Math.max(200, 20 * dedv2genes.size())) {
        AbstractDao.log.info("Fetched " + dedv2genes.size() + " vectors for " + cs2gene.size() + " probes in "
                + timer.getTime() + "ms\n" + "Vector query was: " + queryString);

    }
    return dedv2genes;
}

From source file:ubic.gemma.persistence.service.expression.bioAssayData.DesignElementDataVectorDaoImpl.java

License:Apache License

private void getVectorsBatch(Map<Long, Collection<Long>> cs2gene, org.hibernate.Query queryObject,
        Map<T, Collection<Long>> dedv2genes, Collection<Long> batch) {
    queryObject.setParameterList("cs", batch);
    queryObject.setFlushMode(FlushMode.MANUAL);
    queryObject.setReadOnly(true);//from ww w .  j a  va2 s  . c  o m
    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);

    while (results.next()) {
        @SuppressWarnings("unchecked")
        T dedv = (T) results.get(0);
        Long cs = (Long) results.get(1);
        Collection<Long> associatedGenes = cs2gene.get(cs);
        if (!dedv2genes.containsKey(dedv)) {
            dedv2genes.put(dedv, associatedGenes);
        } else {
            Collection<Long> mappedGenes = dedv2genes.get(dedv);
            mappedGenes.addAll(associatedGenes);
        }
    }

    results.close();
}