我同意,你不应该手动冲洗。我的 webapp 开发者经验说 FlushMode 应该设置为“AUTO”。但有时我需要禁用脏检查(用于验证数据、验证数据库中的数据以及表单或服务返回的数据)。为此,我通过更改 FLUSHMODE 创建了一个特殊类并禁用了脏检查:
@Component
public class ValidateRefPaysService implements IValidateRefPaysService {
...
@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;
@Override
@Transactional(readOnly=true)
public void validate(RefPays refPays) throws BusinessException {
try {
sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
if ( refPays.getId() != null ) {
RefPays refPaysBase = refPaysDAO.getById(refPays.getId());
if ( refPaysBase != null ) {
throw new BusinessException("id already exists in database.", "RefPays.savePays.id.alreadyexist", "refPays.savePays.id.alreadyexist");
}
}
} finally {
sessionFactory.getCurrentSession().setFlushMode(FlushMode.AUTO);
}
}
}