Search in sources :

Example 1 with FlushMode

use of org.hibernate.FlushMode in project hibernate-orm by hibernate.

the class QueryBinder method bindQuery.

public static void bindQuery(org.hibernate.annotations.NamedQuery queryAnn, MetadataBuildingContext context) {
    if (queryAnn == null) {
        return;
    }
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    FlushMode flushMode;
    flushMode = getFlushMode(queryAnn.flushMode());
    NamedQueryDefinition query = new NamedQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(flushMode).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).createNamedQueryDefinition();
    context.getMetadataCollector().addNamedQuery(query);
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named query: %s => %s", query.getName(), query.getQueryString());
    }
}
Also used : NamedQueryDefinition(org.hibernate.engine.spi.NamedQueryDefinition) AnnotationException(org.hibernate.AnnotationException) NamedQueryDefinitionBuilder(org.hibernate.engine.spi.NamedQueryDefinitionBuilder) FlushMode(org.hibernate.FlushMode)

Example 2 with FlushMode

use of org.hibernate.FlushMode in project openmrs-core by openmrs.

the class HibernateEncounterDAO method getSavedEncounterDatetime.

/**
 * @see org.openmrs.api.db.EncounterDAO#getSavedEncounterDatetime(org.openmrs.Encounter)
 */
@Override
public Date getSavedEncounterDatetime(Encounter encounter) {
    // Usages of this method currently are internal and don't require a flush
    // Otherwise we end up with premature flushes of Immutable types like Obs
    // that are associated to the encounter before we void and replace them
    Session session = sessionFactory.getCurrentSession();
    FlushMode flushMode = session.getFlushMode();
    session.setFlushMode(FlushMode.MANUAL);
    try {
        SQLQuery sql = session.createSQLQuery("select encounter_datetime from encounter where encounter_id = :encounterId");
        sql.setInteger("encounterId", encounter.getEncounterId());
        return (Date) sql.uniqueResult();
    } finally {
        session.setFlushMode(flushMode);
    }
}
Also used : SQLQuery(org.hibernate.SQLQuery) FlushMode(org.hibernate.FlushMode) Date(java.util.Date) Session(org.hibernate.Session)

Example 3 with FlushMode

use of org.hibernate.FlushMode in project openmrs-core by openmrs.

the class HibernateConceptDAO method getDefaultConceptMapType.

/**
 * @see org.openmrs.api.db.ConceptDAO#getDefaultConceptMapType()
 */
@Override
public ConceptMapType getDefaultConceptMapType() throws DAOException {
    FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode();
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
    try {
        // Defaults to same-as if the gp is not set.
        String defaultConceptMapType = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_DEFAULT_CONCEPT_MAP_TYPE);
        if (defaultConceptMapType == null) {
            throw new DAOException("The default concept map type is not set. You need to set the '" + OpenmrsConstants.GP_DEFAULT_CONCEPT_MAP_TYPE + "' global property.");
        }
        ConceptMapType conceptMapType = getConceptMapTypeByName(defaultConceptMapType);
        if (conceptMapType == null) {
            throw new DAOException("The default concept map type (name: " + defaultConceptMapType + ") does not exist! You need to set the '" + OpenmrsConstants.GP_DEFAULT_CONCEPT_MAP_TYPE + "' global property.");
        }
        return conceptMapType;
    } finally {
        sessionFactory.getCurrentSession().setFlushMode(previousFlushMode);
    }
}
Also used : DAOException(org.openmrs.api.db.DAOException) ConceptMapType(org.openmrs.ConceptMapType) FlushMode(org.hibernate.FlushMode)

Example 4 with FlushMode

use of org.hibernate.FlushMode in project openmrs-core by openmrs.

the class HibernateContextDAO method updateSearchIndexForType.

@Override
@Transactional
public void updateSearchIndexForType(Class<?> type) {
    // From http://docs.jboss.org/hibernate/search/3.3/reference/en-US/html/manual-index-changes.html#search-batchindex-flushtoindexes
    FullTextSession session = Search.getFullTextSession(sessionFactory.getCurrentSession());
    session.purgeAll(type);
    // Prepare session for batch work
    session.flush();
    session.clear();
    FlushMode flushMode = session.getFlushMode();
    CacheMode cacheMode = session.getCacheMode();
    try {
        session.setFlushMode(FlushMode.MANUAL);
        session.setCacheMode(CacheMode.IGNORE);
        // Scrollable results will avoid loading too many objects in memory
        ScrollableResults results = session.createCriteria(type).setFetchSize(1000).scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            index++;
            // index each element
            session.index(results.get(0));
            if (index % 1000 == 0) {
                // apply changes to indexes
                session.flushToIndexes();
                // free memory since the queue is processed
                session.clear();
            }
        }
        session.flushToIndexes();
        session.clear();
    } finally {
        session.setFlushMode(flushMode);
        session.setCacheMode(cacheMode);
    }
}
Also used : FullTextSession(org.hibernate.search.FullTextSession) CacheMode(org.hibernate.CacheMode) ScrollableResults(org.hibernate.ScrollableResults) FlushMode(org.hibernate.FlushMode) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with FlushMode

use of org.hibernate.FlushMode in project openmrs-core by openmrs.

the class HibernateObsDAO method getSavedStatus.

/**
 * @see org.openmrs.api.db.ObsDAO#getSavedStatus(org.openmrs.Obs)
 */
@Override
public Obs.Status getSavedStatus(Obs obs) {
    // avoid premature flushes when this internal method is called from inside a service method
    Session session = sessionFactory.getCurrentSession();
    FlushMode flushMode = session.getFlushMode();
    session.setFlushMode(FlushMode.MANUAL);
    try {
        SQLQuery sql = session.createSQLQuery("select status from obs where obs_id = :obsId");
        sql.setInteger("obsId", obs.getObsId());
        return Obs.Status.valueOf((String) sql.uniqueResult());
    } finally {
        session.setFlushMode(flushMode);
    }
}
Also used : SQLQuery(org.hibernate.SQLQuery) FlushMode(org.hibernate.FlushMode) Session(org.hibernate.Session)

Aggregations

FlushMode (org.hibernate.FlushMode)19 Session (org.hibernate.Session)13 CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)4 Transactional (org.springframework.transaction.annotation.Transactional)4 UpdateCartException (org.broadleafcommerce.core.order.service.exception.UpdateCartException)3 WorkflowException (org.broadleafcommerce.core.workflow.WorkflowException)3 SQLQuery (org.hibernate.SQLQuery)3 Connection (java.sql.Connection)2 ArrayList (java.util.ArrayList)2 Order (org.broadleafcommerce.core.order.domain.Order)2 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)2 AddToCartException (org.broadleafcommerce.core.order.service.exception.AddToCartException)2 RemoveFromCartException (org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1 Date (java.util.Date)1 SystemException (javax.transaction.SystemException)1 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)1 OfferAlreadyAddedException (org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException)1