use of org.hibernate.FlushMode in project spring-framework by spring-projects.
the class HibernateJpaDialect method prepareTransaction.
@Override
public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name) throws PersistenceException {
Session session = getSession(entityManager);
FlushMode previousFlushMode = prepareFlushMode(session, readOnly);
return new SessionTransactionData(session, previousFlushMode, null, null);
}
use of org.hibernate.FlushMode in project spring-framework by spring-projects.
the class SpringSessionContext method currentSession.
/**
* Retrieve the Spring-managed Session for the current thread, if any.
*/
@Override
@SuppressWarnings("deprecation")
public Session currentSession() throws HibernateException {
Object value = TransactionSynchronizationManager.getResource(this.sessionFactory);
if (value instanceof Session) {
return (Session) value;
} else if (value instanceof SessionHolder) {
SessionHolder sessionHolder = (SessionHolder) value;
Session session = sessionHolder.getSession();
if (!sessionHolder.isSynchronizedWithTransaction() && TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new SpringSessionSynchronization(sessionHolder, this.sessionFactory, false));
sessionHolder.setSynchronizedWithTransaction(true);
// Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
// with FlushMode.MANUAL, which needs to allow flushing within the transaction.
FlushMode flushMode = SessionFactoryUtils.getFlushMode(session);
if (flushMode.equals(FlushMode.MANUAL) && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
session.setFlushMode(FlushMode.AUTO);
sessionHolder.setPreviousFlushMode(flushMode);
}
}
return session;
}
if (this.transactionManager != null) {
try {
if (this.transactionManager.getStatus() == Status.STATUS_ACTIVE) {
Session session = this.jtaSessionContext.currentSession();
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new SpringFlushSynchronization(session));
}
return session;
}
} catch (SystemException ex) {
throw new HibernateException("JTA TransactionManager found but status check failed", ex);
}
}
if (TransactionSynchronizationManager.isSynchronizationActive()) {
Session session = this.sessionFactory.openSession();
if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
session.setFlushMode(FlushMode.MANUAL);
}
SessionHolder sessionHolder = new SessionHolder(session);
TransactionSynchronizationManager.registerSynchronization(new SpringSessionSynchronization(sessionHolder, this.sessionFactory, true));
TransactionSynchronizationManager.bindResource(this.sessionFactory, sessionHolder);
sessionHolder.setSynchronizedWithTransaction(true);
return session;
} else {
throw new HibernateException("Could not obtain transaction-synchronized Session for current thread");
}
}
use of org.hibernate.FlushMode in project openmrs-core by openmrs.
the class HibernateAdministrationDAO method validate.
/**
* @see org.openmrs.api.db.AdministrationDAO#validate(java.lang.Object, Errors)
* @should Pass validation if field lengths are correct
* @should Fail validation if field lengths are not correct
* @should Fail validation for location class if field lengths are not correct
* @should Pass validation for location class if field lengths are correct
*/
// @SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
@Override
public void validate(Object object, Errors errors) throws DAOException {
Class entityClass = object.getClass();
ClassMetadata metadata = sessionFactory.getClassMetadata(entityClass);
if (metadata != null) {
String[] propNames = metadata.getPropertyNames();
Object identifierType = metadata.getIdentifierType();
String identifierName = metadata.getIdentifierPropertyName();
if (identifierType instanceof StringType || identifierType instanceof TextType) {
int maxLength = getMaximumPropertyLength(entityClass, identifierName);
String identifierValue = (String) metadata.getIdentifier(object, (SessionImplementor) sessionFactory.getCurrentSession());
if (identifierValue != null) {
int identifierLength = identifierValue.length();
if (identifierLength > maxLength) {
errors.rejectValue(identifierName, "error.exceededMaxLengthOfField", new Object[] { maxLength }, null);
}
}
}
for (String propName : propNames) {
Type propType = metadata.getPropertyType(propName);
if (propType instanceof StringType || propType instanceof TextType) {
String propertyValue = (String) metadata.getPropertyValue(object, propName);
if (propertyValue != null) {
int maxLength = getMaximumPropertyLength(entityClass, propName);
int propertyValueLength = propertyValue.length();
if (propertyValueLength > maxLength) {
errors.rejectValue(propName, "error.exceededMaxLengthOfField", new Object[] { maxLength }, null);
}
}
}
}
}
FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode();
sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
try {
for (Validator validator : getValidators(object)) {
validator.validate(object, errors);
}
} finally {
sessionFactory.getCurrentSession().setFlushMode(previousFlushMode);
}
}
use of org.hibernate.FlushMode in project openmrs-core by openmrs.
the class HibernateContextDAO method getUserByUuid.
/**
* @see org.openmrs.api.db.ContextDAO#getUserByUuid(java.lang.String)
*/
@Override
@Transactional(readOnly = true)
public User getUserByUuid(String uuid) {
// don't flush here in case we're in the AuditableInterceptor. Will cause a StackOverflowEx otherwise
FlushMode flushMode = sessionFactory.getCurrentSession().getFlushMode();
sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
User u = (User) sessionFactory.getCurrentSession().createQuery("from User u where u.uuid = :uuid").setString("uuid", uuid).uniqueResult();
// reset the flush mode to whatever it was before
sessionFactory.getCurrentSession().setFlushMode(flushMode);
return u;
}
use of org.hibernate.FlushMode in project openmrs-core by openmrs.
the class HibernateProgramWorkflowDAO method getPatientProgramByAttributeNameAndValue.
@Override
public List<PatientProgram> getPatientProgramByAttributeNameAndValue(String attributeName, String attributeValue) {
FlushMode flushMode = sessionFactory.getCurrentSession().getFlushMode();
sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
Query query;
try {
query = sessionFactory.getCurrentSession().createQuery("SELECT pp FROM patient_program pp " + "INNER JOIN pp.attributes attr " + "INNER JOIN attr.attributeType attr_type " + "WHERE attr.valueReference = :attributeValue " + "AND attr_type.name = :attributeName " + "AND pp.voided = 0").setParameter("attributeName", attributeName).setParameter("attributeValue", attributeValue);
return query.list();
} finally {
sessionFactory.getCurrentSession().setFlushMode(flushMode);
}
}
Aggregations