public Entity updateEntity(Entity entity) throws ApplicationException {
    if (entity == null) {
      return entity;
    }
    if (entity.getEntityVersionId() == null) {
      throw new ApplicationException(
          "An entity definition must first be created before it is updated.");
    }
    try {
      Date currentDate = new Date();
      entity.setDateChanged(currentDate);
      entity.setUserChangedBy(Context.getUserContext().getUser());
      for (EntityAttribute attrib : entity.getAttributes()) {
        if (attrib.getDateCreated() == null) {
          attrib.setDateCreated(currentDate);
          attrib.setUserCreatedBy(Context.getUserContext().getUser());
          // attrib.setEntity(entity);
        }
        attrib.setEntity(entity);
      }

      // Mark deletion attributes
      handleAttributeUpdates(entity);

      // remove validations for marked attribute
      for (EntityAttribute attrib : entity.getAttributes()) {
        if (attrib.getUserVoidedBy() != null) {

          // remove validations
          List<EntityAttributeValidation> existingValidations =
              loadEntityAttributeValidations(attrib);
          for (EntityAttributeValidation validation : existingValidations) {
            deleteEntityAttributeValidation(validation);
          }
        }
      }

      Entity updatedEntity = entityDefinitionDao.updateEntity(entity);
      updatedEntity = entityDefinitionDao.loadEntity(entity.getEntityVersionId());

      addToCache(updatedEntity);
      Context.notifyObserver(ObservationEventType.ENTITY_ATTRIBUTE_UPDATE_EVENT, entity);

      return updatedEntity;
    } catch (DaoException e) {
      throw new ApplicationException(e.getMessage());
    }
  }