Sling Model Inject Annotation | Community
Skip to main content
Level 8
February 10, 2026
Question

Sling Model Inject Annotation

  • February 10, 2026
  • 2 replies
  • 9 views

Hi all,

 

Using Sling Model @Inject Annotation, we could copy JCR node values in to Java variables.

Is the opposite also possible, in terms of updating JCR node values from Java variables?

If so is there an example?

 

Appreciate all your replies.

 

Thanks,

RK.

2 replies

arunpatidar
Community Advisor
Community Advisor
February 10, 2026

Hi ​@nsvsrk 
Not its not true. Using Sling Model @Inject Annotation, does not copy the JCR node but try to find a resource with that type and adapt it.
You can also try to adapt a resource and update it. but I would recommend to use Sling API instead of JCR Node API to update resource/node.
https://www.javadoc.io/doc/org.apache.sling/org.apache.sling.api/2.16.0/org/apache/sling/api/resource/ModifiableValueMap.html

Arun Patidar
chaudharynick
Level 4
February 10, 2026

Hi ​@nsvsrk 

It is not possible. The @Inject annotation is for adapting the object to the closest possible type by the Sling Models Injector.

 

To "reverse" the process, you need to obtain a ModifiableValueMap from the resource. Here is the standard pattern:

public void updateNodeProperty(ResourceResolver resolver, String path, String newValue) {

Resource resource = resolver.getResource(path);

if (resource != null) {

ModifiableValueMap map = resource.adaptTo(ModifiableValueMap.class);

if (map != null) {
map.put("myProperty", newValue);
try {
resolver.commit();
} catch (PersistenceException e) {
// Handle error
}
}
}
}