1 Reply Latest reply on Jul 15, 2015 5:19 AM by Horia Chiorean

    ConstraintViolationException when restoring a nt:file node

    Jochen Kraushaar Newbie

      Hi there,

       

      when I try to restore a removed nt:file node from version history, I am getting the error:

       

      javax.jcr.nodetype.ConstraintViolationException: The values for the '{http://www.jcp.org/jcr/1.0}content' property on node '/restored' no longer satisfy the type and/or constraints on the 'jcr:content' property definition on the 'nt:file' node type definition

       

      Example code:

       

      Node testFolder = root.addNode("testfolder", NodeType.NT_FOLDER);
      testFolder.addMixin(NodeType.MIX_VERSIONABLE);
      Node testFile = testFolder.addNode("testfile", NodeType.NT_FILE);
      testFile.addMixin(NodeType.MIX_VERSIONABLE);
      Node contentNode = testFile.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE);
      InputStream is = new ByteArrayInputStream(new byte[] { 84, 101, 115, 116 });
      Binary binary = valueFactory.createBinary(is);
      contentNode.setProperty(Property.JCR_DATA, binary);
      session.save();
      
      
      Version baseVersion = versionManager.getBaseVersion(testFile.getPath());
      
      
      versionManager.checkout(testFolder.getPath());
      testFile.remove();
      session.save();
      versionManager.checkin(testFolder.getPath());
      
      versionManager.restore("/restored", baseVersion, false);
      

       

      I am not sure what the problem is. I am using ModeShape 4.2.

       

      Thanks for your help

      Jochen

        • 1. Re: ConstraintViolationException when restoring a nt:file node
          Horia Chiorean Master

          I don't understand what you're trying to do: you're getting the base version of a file, then removing that file, creating a new version of the owning folder (checkout/checkin) and then attempting to restore the base version of the file, even though you haven't created a version of that file in the first place. This makes no sense to me.

           

          However, some things you should keep in mind:

          1. the base version of a new versionable node is initially the root version, *which is always an empty version* (as per the JCR spec 3.13.5.2). Never attempt to restore the root version of a node which has mandatory properties/child nodes (like your case)
          2. if you have a (parent -> child) relationship and both nodes have mix:versionable and OPV VERSION (which is the default) calling checkin(parent) will behave differently in terms of the child graph than when OPV is COPY.  You should look at JSR 283 #3.13.9 Versionable State

           

          You're getting this exception because you're attempting to restore an invalid version of a file which hasn't been properly versioned in the first place. Because of this the [jcr:content] mandatory child node of [nt:file] is not present anywhere in the versions graph. You'll find more information on how full versioning works in section 3.13 of the JCR 2.0 spec.