Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

javax.jcr.nodetype.ConstraintViolationException: no matching property definition found for {}id

Avatar

Former Community Member

Hi,

I have been trying to  add some node and write some properties through java code.

I am getting the following error “javax.jcr.nodetype.ConstraintViolationException: no matching property definition found for {}id”.

Could somebody tell me why it happens and how to solve error?

Here is my sample code :

Session session = this.repository.loginAdministrative(null);

Node root = session.getRootNode();

Node content = root.getNode("content");

Node customerRoot = null;

customerRoot = content.getNode("customer");

Node custNode = customerRoot.addNode("customer"+firstName+lastName+custId);

custNode.setProperty("id", custId);

custNode.setProperty("firstName", firstName);

custNode.setProperty("lastName", lastName);

custNode.setProperty("phone", phone); 

custNode.setProperty("desc", desc);

session.save();

session.logout();

Thanks,

Anderson

1 Accepted Solution

Avatar

Correct answer by
Level 10

Your issue is most likely is around the customer node. If it does not exist - an exception is thrown. This code tests to see if the content/customer node exists. If it does not -- the code creates it.

This Java code adds a new customer under the content/customer node.

/* * This Java Quick Start uses the jackrabbit-standalone-2.4.0.jar * file. See the previous section for the location of this JAR file */ import java.util.Iterator; import javax.jcr.Repository; import javax.jcr.Session; import javax.jcr.SimpleCredentials; import javax.jcr.Node; import org.apache.jackrabbit.commons.JcrUtils; import org.apache.jackrabbit.core.TransientRepository; public class TestCustomerCode { public static void main(String[] args) throws Exception { try { //Create a connection to the CQ repository running on local host Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server"); //Create a Session javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray())); String firstName ="Tom"; String lastName = "Blue"; String phone ="555-555-5555"; String desc = "active customer"; //Create a node that represents the root node Node root = session.getRootNode(); //Get the content node in the JCR Node content = root.getNode("content"); //Determine if the content/customer node exists Node customerRoot = null; int custRec = doesCustExist(content); //-1 means that content/customer does not exist if (custRec == -1) //content/customer does not exist -- create it customerRoot = content.addNode("customer","sling:OrderedFolder"); else //content/customer does exist -- retrieve it customerRoot = content.getNode("customer"); int custId = custRec+1; //assign a new id to the customer node //Store content from the client JSP in the JCR Node custNode = customerRoot.addNode("customer"+firstName+lastName+custId,"nt:unstructured"); //make sure name of node is unique custNode.setProperty("id", custId); custNode.setProperty("firstName", firstName); custNode.setProperty("lastName", lastName); custNode.setProperty("phone", phone); custNode.setProperty("desc", desc); // Save the session changes and log out session.save(); session.logout(); } catch(Exception e){ e.printStackTrace(); } } /* * Determines if the content/customer node exists * This method returns these values: * -1 - if customer does not exist * 0 - if content/customer node exists; however, contains no children * number - the number of children that the content/customer node contains */ private static int doesCustExist(Node content) { try { int index = 0 ; int childRecs = 0 ; java.lang.Iterable<Node> custNode = JcrUtils.getChildNodes(content, "customer"); Iterator it = custNode.iterator(); //only going to be 1 content/customer node if it exists if (it.hasNext()) { //Count the number of child nodes to customer Node customerRoot = content.getNode("customer"); Iterable itCust = JcrUtils.getChildNodes(customerRoot); Iterator childNodeIt = itCust.iterator(); //Count the number of customer child nodes while (childNodeIt.hasNext()) { childRecs++; childNodeIt.next(); } return childRecs; } else return -1; //content/customer does not exist } catch(Exception e) { e.printStackTrace(); } return 0; } }

You can also create an OSGi bundle that uses JCR API code like this from within a service. For details, see http://scottsdigitalcommunity.blogspot.ca/2013/02/querying-adobe-experience-manager-data.html.

 

Hope this helps!

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Your issue is most likely is around the customer node. If it does not exist - an exception is thrown. This code tests to see if the content/customer node exists. If it does not -- the code creates it.

This Java code adds a new customer under the content/customer node.

/* * This Java Quick Start uses the jackrabbit-standalone-2.4.0.jar * file. See the previous section for the location of this JAR file */ import java.util.Iterator; import javax.jcr.Repository; import javax.jcr.Session; import javax.jcr.SimpleCredentials; import javax.jcr.Node; import org.apache.jackrabbit.commons.JcrUtils; import org.apache.jackrabbit.core.TransientRepository; public class TestCustomerCode { public static void main(String[] args) throws Exception { try { //Create a connection to the CQ repository running on local host Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server"); //Create a Session javax.jcr.Session session = repository.login( new SimpleCredentials("admin", "admin".toCharArray())); String firstName ="Tom"; String lastName = "Blue"; String phone ="555-555-5555"; String desc = "active customer"; //Create a node that represents the root node Node root = session.getRootNode(); //Get the content node in the JCR Node content = root.getNode("content"); //Determine if the content/customer node exists Node customerRoot = null; int custRec = doesCustExist(content); //-1 means that content/customer does not exist if (custRec == -1) //content/customer does not exist -- create it customerRoot = content.addNode("customer","sling:OrderedFolder"); else //content/customer does exist -- retrieve it customerRoot = content.getNode("customer"); int custId = custRec+1; //assign a new id to the customer node //Store content from the client JSP in the JCR Node custNode = customerRoot.addNode("customer"+firstName+lastName+custId,"nt:unstructured"); //make sure name of node is unique custNode.setProperty("id", custId); custNode.setProperty("firstName", firstName); custNode.setProperty("lastName", lastName); custNode.setProperty("phone", phone); custNode.setProperty("desc", desc); // Save the session changes and log out session.save(); session.logout(); } catch(Exception e){ e.printStackTrace(); } } /* * Determines if the content/customer node exists * This method returns these values: * -1 - if customer does not exist * 0 - if content/customer node exists; however, contains no children * number - the number of children that the content/customer node contains */ private static int doesCustExist(Node content) { try { int index = 0 ; int childRecs = 0 ; java.lang.Iterable<Node> custNode = JcrUtils.getChildNodes(content, "customer"); Iterator it = custNode.iterator(); //only going to be 1 content/customer node if it exists if (it.hasNext()) { //Count the number of child nodes to customer Node customerRoot = content.getNode("customer"); Iterable itCust = JcrUtils.getChildNodes(customerRoot); Iterator childNodeIt = itCust.iterator(); //Count the number of customer child nodes while (childNodeIt.hasNext()) { childRecs++; childNodeIt.next(); } return childRecs; } else return -1; //content/customer does not exist } catch(Exception e) { e.printStackTrace(); } return 0; } }

You can also create an OSGi bundle that uses JCR API code like this from within a service. For details, see http://scottsdigitalcommunity.blogspot.ca/2013/02/querying-adobe-experience-manager-data.html.

 

Hope this helps!

Avatar

Level 3

Hi,

I tried the mothod. Worked great. Thanks. But when when i implement the session.logout() method, my current session is logged out and other functions do not work the way they should. I have implemented this function, but without the logout method. Can I know is it fine to do that or should i implement the logout method. Also if implemented, how do i prevent my other components from breakin.

Thanks