public void persist()

in SlingModelPersist/src/main/java/org/apache/sling/models/persistor/impl/ModelPersistorImpl.java [96:142]


    public void persist(final @NotNull String nodePath, final @NotNull Object instance,
            @NotNull ResourceResolver resourceResolver, boolean deepPersist)
            throws RepositoryException, PersistenceException, IllegalArgumentException, IllegalAccessException {
        if (StringUtils.isBlank(nodePath)) {
            throw new IllegalArgumentException("Node path cannot be null/empty");
        }

        if (instance == null) {
            throw new IllegalArgumentException("Object to save cannot be null");
        }

        if (resourceResolver == null) {
            throw new IllegalArgumentException("ResourceResolver cannot be null");
        }

        Resource resource;
        final ResourceTypeKey resourceType = ResourceTypeKey.fromObject(instance);

        // let's create the resource first
        LOGGER.debug("Creating node at: {} of type: {}", nodePath, resourceType.primaryType);
        boolean isUpdate = resourceResolver.getResource(nodePath) != null;
        resource = ResourceUtil.getOrCreateResource(resourceResolver, nodePath, resourceType.primaryType, NT_UNSTRUCTURED, true);
        if (StringUtils.isNotEmpty(resourceType.childType)) {
            LOGGER.debug("Needs a child node, creating node at: {} of type: {}", nodePath, resourceType.childType);
            resource = ResourceUtil.getOrCreateResource(resourceResolver, nodePath + "/" + JCR_CONTENT, resourceType.childType, NT_UNSTRUCTURED, true);
        }

        if (ReflectionUtils.isArrayOrCollection(instance)) {
            persistComplexValue(instance, true, "dunno", resource);
        } else {
            // find properties to be saved
            List<Field> fields = ReflectionUtils.getAllFields(instance.getClass());
            if (fields == null || fields.isEmpty()) {
                // TODO: remove all properties
            } else {
                Resource r = resource;
                fields.stream()
                        .filter(field -> ReflectionUtils.isNotTransient(field, isUpdate))
                        .filter(field -> ReflectionUtils.isSupportedType(field) || ReflectionUtils.isCollectionOfPrimitiveType(field))
                        .filter(f -> ReflectionUtils.hasNoTransientGetter(f.getName(), instance.getClass()))
                        .forEach(field -> persistField(r, instance, field, deepPersist));
            }
        }

        // save back
        resourceResolver.commit();
    }