/**
  * Overrides the default behaviour and first checks if a PersistentCollection instance has been
  * initialised using the wasInitialised() method before cascading
  *
  * @param errors The Spring Errors instance
  * @param bean The BeanWrapper for the bean
  * @param persistentProperty The GrailsDomainClassProperty instance
  * @param propertyName The name of the property
  * @see org.hibernate.collection.PersistentCollection#wasInitialized()
  */
 protected void cascadeValidationToMany(
     Errors errors,
     BeanWrapper bean,
     GrailsDomainClassProperty persistentProperty,
     String propertyName) {
   Object collection = bean.getPropertyValue(propertyName);
   if (collection != null) {
     if (collection instanceof PersistentCollection) {
       PersistentCollection persistentCollection = (PersistentCollection) collection;
       if (persistentCollection.wasInitialized()) {
         super.cascadeValidationToMany(errors, bean, persistentProperty, propertyName);
       }
     } else {
       super.cascadeValidationToMany(errors, bean, persistentProperty, propertyName);
     }
   }
 }
 protected void cascadeValidationToOne(
     Errors errors,
     BeanWrapper bean,
     Object associatedObject,
     GrailsDomainClassProperty persistentProperty,
     String propertyName) {
   List validatedInstancesList = (List) validatedInstances.get();
   validatedInstancesList.add(associatedObject);
   super.cascadeValidationToOne(errors, bean, associatedObject, persistentProperty, propertyName);
 }
  @Override
  public void validate(Object obj, Errors errors, boolean cascade) {
    final Session session = sessionFactory.getCurrentSession();
    FlushMode previousMode = null;
    try {
      if (session != null) {
        previousMode = session.getFlushMode();
        session.setFlushMode(FlushMode.MANUAL);
      }

      super.validate(obj, errors, cascade);
    } finally {
      if (session != null && previousMode != null) {
        session.setFlushMode(previousMode);
      }
    }
  }