1

I'm trying to add a reference but when I call node.getReferences() I can't see it.

I've tried creating a simple example -> create 2 nodes under root and reference one from the other. That works fine.

In my working code it doesn't. I'm guessing it's got something to do with versioning but I can' find any doc's explaining what's going on. Let me explain the structure

Root
  |__project node
       |
       |__ node 1
       |
       |__ node 2

All nodes have mix:versionable and mix:referenceble.

Bit of code...

         node1.checkout();
         node2.checkout();
         node2.setProperty("ref to node1", node1);
         session.save();

         if (!node1.getReferences().hasNext())
             System.out.println("No references");

I've tried removing the checkout's and the save but all to no avail.

Any comments or recommended reading appreciated.

Ted.

TedTrippin
  • 3,525
  • 5
  • 28
  • 46

1 Answers1

3

The code you listed should work as you expect. Are you using some remoting layer (RMI, WebDAV, etc.) that might have a bug in the way references are handled?

You can try for example the following code with a local Jackrabbit instance:

Node root = session.getRootNode().addNode("test");
Node node1 = root.addNode("node1");
node1.addMixin("mix:referenceable");
Node node2 = root.addNode("node2");
node2.setProperty("reference", node1);
session.save();

System.out.println("References to " + node1.getPath() + ":");
for (Property reference : JcrUtils.getReferences(node1)) {
    System.out.println("- " + reference.getPath());
}

It prints out the following:

References to /test/node1:
- /test/node2/reference
Jukka Zitting
  • 1,092
  • 6
  • 13
  • I was in fact testing with a local Jackrabbit instance. I'm wondering if I forgot to add the "mix:referenceable" (being a JCR newbe!) !!. I've since given up on references and used a separate node with properties which has ended up being a more suitable solution in the end. – TedTrippin Oct 12 '11 at 12:05