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());
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations