Saving Data in JCR Node, what am I doing wrong?
Asked Answered
H

1

1

I have created and deployed a bundle(Servlet) successfully that accepts username and password from user, now I want to save it in JCR Repository under /content/mydata/ I am getting Exception

java.lang.IllegalArgumentException: relPath is not a relative path: {}  {}oliver

Here is my code

  public class CustomerJCRAccessImp implements CustomerService {
        @Reference
        protected SlingRepository repository;

    protected static final Logger log = LoggerFactory.getLogger(CustomerJCRAccessImp.class);

    public void insertData(String username, String password) throws Exception {

        log.error("Username ::"+username+" Password ::"+password);
        log.error("XXX:: Inside the Service Method");
        Session session=    repository.loginAdministrative(null);
        Node node= session.getRootNode();
        Node contentNode = node.getNode("content");
        //node.i
        Node  myAppNode = contentNode.getNode("myApp");
        log.error("THE VALUE OF myApp NODE ::"+myAppNode);


        Node user = myAppNode.addNode("/"+username);
        user.setProperty("Roll No", "1");
        user.setProperty("Age", "10");
        user.setPrimaryType("nt:unstructured");

        session.save();
        session.logout();




    }
    protected void bindRepository(SlingRepository repository) {
        this.repository = repository; 
    }
}

I have done this by referring this link http://helpx.adobe.com/experience-manager/using/persisting-cq-data-java-content.html Thanks in Advance.

Heinrich answered 3/4, 2014 at 5:20 Comment(0)
K
5

The relative path parameter for the addNode() method shouldn't start with a "/". Try

Node user = videojetNode.addNode(username);

Though i agree that the term "relPath" in the docs is quite misleading, the relPath should either be the name of the node you would like to create under the current node or should start with the name of the child node and contains the relative path to the target node under which you want to create your node.

For example. In case the current node is content and you have the following tree

/
|_content
    |_x
       |_y

and if you wish to add a node called z as a child of y, then the relPath can be specified as

Node myNode = contentNode.addNode("x/y/z");

Note : A PathNotFoundException will be thrown in case any of the intermediary nodes are not available

Koval answered 3/4, 2014 at 6:35 Comment(6)
Well it doesn't work Rakhi what should I do ?? please help me :(Heinrich
You still get the same error? Or are you facing new issues? Could you provide those details?Koval
javax.jcr.nodetype.ConstraintViolationException: No child node definition for user found in node /content/videojet This is what I get. it comes on the line where I have done add node like this... Node user = videojetNode.addNode("user","nt:unstructured");Heinrich
What type of node is videojet? This exception is thrown when a particular node type is not allowed to be added under the given node or if you are trying to add a node as a child of a propertyKoval
Videojet is a regular nt:folder. Can I create nt:unstructured under this??Heinrich
In case your videojet is of type nt:folder then only nodes which extends the type nt:hierarchyNode such as nt:file, nt:folder etc can be added as its child. If thats the case, try changing the node type of your videojet to something else, that allows you to add nt:unstructured nodes beneath them.Koval

© 2022 - 2024 — McMap. All rights reserved.