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

Sling Model Inject Annotation

  • February 10, 2026
  • 5 replies
  • 41 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.

Best answer by BrianKasingli

No.

@Inject in a Sling Model is read-only — it only pulls values from JCR into Java variables.

If you want to update JCR values, you must use:

ModifiableValueMap mvm = resource.adaptTo(ModifiableValueMap.class);
mvm.put("propertyName", "newValue");
resourceResolver.commit();


So:

@Inject → Read from JCR

ModifiableValueMap + commit() → Write to JCR

5 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
}
}
}
}

 

AmitVishwakarma
Community Advisor
Community Advisor
February 10, 2026

Hi ​@nsvsrk ,

No, @Inject in Sling Models is read‑only. It only helps populate your model fields from the current Resource / ValueMap. It does not write back to JCR.

To update JCR from Java, you must use the Sling Resource API (recommended) or JCR API directly.

 

How to “reverse” it correctly:

Use ResourceResolver -> Resource -> ModifiableValueMap:

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ModifiableValueMap;

@Model(adaptables = Resource.class)
public class MyModel {

@Inject
private Resource resource;

public void updateTitle(String newTitle) {
ResourceResolver resolver = resource.getResourceResolver();

ModifiableValueMap mvm = resource.adaptTo(ModifiableValueMap.class);
if (mvm != null) {
mvm.put("jcr:title", newTitle);
resolver.commit(); // persist changes
}
}
}

Key points:

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
February 12, 2026

No.

@Inject in a Sling Model is read-only — it only pulls values from JCR into Java variables.

If you want to update JCR values, you must use:

ModifiableValueMap mvm = resource.adaptTo(ModifiableValueMap.class);
mvm.put("propertyName", "newValue");
resourceResolver.commit();


So:

@Inject → Read from JCR

ModifiableValueMap + commit() → Write to JCR

kautuk_sahni
Community Manager
Community Manager
February 17, 2026

@nsvsrk Quick follow-up! Were you able to get this issue sorted out? If you did, please consider posting the solution you used so others can learn from it. And if any of the replies above were helpful—whether they fully solved your problem or just pointed you toward the right fix—marking one as accepted helps future community members find solutions more easily. Closing the loop here makes a real difference for everyone.

Kautuk Sahni