1 Reply Latest reply on Jul 28, 2014 9:33 AM by rhauch

    ConstraintViolationException when creating a node with a protected property

    ma6rl

      I'm running into an issue when creating a node with a mixin that defines a protected property.

       

      The node type is defined as follows, the name should not change once it is set (the value is different to the node name):

       

      [asset:stock] >  mix:created, mix:lastModified mixin
        - asset:name (string) mandatory protected
      

       

      I create the node as follows:

       

       Node parentNode = repositorySession.getNode(parentPath);
       Node newNode = parentNode.addNode(nodeName);
       newNode.addMixin("asset:stock");
       newNode.setProperty("asset:name", "test node");
       repositorySession.save();
      

       

      This results in the following exception:

       

      javax.jcr.nodetype.ConstraintViolationException: Cannot find a definition for the property named 'asset:name' on the node at '/organizations/test/assets/test' with primary type 'nt:unstructured' and mixin types: [asset:stock]

        at org.modeshape.jcr.AbstractJcrNode.setProperty(AbstractJcrNode.java:1756) [modeshape-jcr-4.0.0.Alpha4.jar:4.0.0.Alpha4]

        at org.modeshape.jcr.AbstractJcrNode.setProperty(AbstractJcrNode.java:1423) [modeshape-jcr-4.0.0.Alpha4.jar:4.0.0.Alpha4]

        at org.modeshape.jcr.AbstractJcrNode.setProperty(AbstractJcrNode.java:98) [modeshape-jcr-4.0.0.Alpha4.jar:4.0.0.Alpha4]

       

      If I remove the protected constraint it works. Looking at the ModeShape documentation (Defining custom node types - ModeShape 4 - Project Documentation Editor) it appears that I should be able to set a protected property if it is absent on the node (which is the case when first creating the node)

      The property to which this definition applies is protected, meaning it can be read but not modified by client applications. When absent, the property can be set by client applications.

       

      Am I doing something wrong when creating the node?

        • 1. Re: ConstraintViolationException when creating a node with a protected property
          rhauch

          Protected properties are repository controlled so you really can't define custom node types with them. That's because the repository has to be able to know how to set them internally, and it won't know how to set the value of your custom protected property.

           

          Perhaps the documentation was not clear; it was trying to say that when the "protected" attribute is absent from the property definition, then clients can set the property.

           

          See the following section of the JCR 2.0 specification:

           

           

          3.7.2.2 Protected

          If an item I is declared protected it is repository-controlled.


          If I is a node then, through the core write methods of JCR (see §10.2 Core Write

          Methods),

          • I cannot be removed,
          • child nodes of I cannot be added, removed, or reordered,
          • properties of I cannot be added or removed,
          • the values of existing properties of I cannot be changed,
          • the primary node type of I cannot be changed and
          • mixin node types cannot be added to or removed from I.

          If I is a property then, through the core write methods of JCR (see §10.2 Core Write Methods),

          • I cannot be removed and
          • the value of I cannot be changed.

          Additionally, if I is a property, its being repository-controlled also implies that its value is under the control of the repository and can change at any time, before or after save. See §3.7.2.3.2 Auto-Created and Protected.