问题
I copied a node in Jackrabbit using session.getWorkspace().copy(sourceNode.getPath(), destinationNode.getPath())
This operation changes are persisted instantly as far as I know. But when I tried to get the copied node for deleting it using session.getNodeByIdentifier("nodeId of copied node")
, it gives ItemNotFoundException. The reason for that error is that the copied node loses is mix:referenceable
property during copy which causes getNodeByIdentifier to fail.
The question is how do I set the mix:referenceable
property to copied node as I m not able to get the node from session after copy operation. Could someone help me out on this?
UPDATE:
CODE:
Node srcNode = session.getNodeByIdentifier("SOURCE_NODE_ID");
if(srcNode == null) {
System.out.println("File not found");
}
Node rootNode = session.getRootNode();
Node appNode = rootNode.getNode("JACKRABBIT");
Node destNode = appNode.addNode("Copy_Test_"+System.currentTimeMillis(),"nt:file");
session.getWorkspace().copy(srcNode.getPath(),destNode.getPath());
destNode.addMixin(MIX_VERSIONABLE);
destNode.addMixin(MIX_LOCKABLE);
destNode.addMixin(MIX_REFERENCEABLE);
destNode.addNode(DMSConstants.RESOURCE_NODE,"nt:unstructured");
session.refresh(true);
session.save();
EXCEPTION:
Exception in thread "main" javax.jcr.InvalidItemStateException: Unable to update a stale item: item.save() at org.apache.jackrabbit.rmi.server.ServerObject.getRepositoryException(ServerObject.java:111) at org.apache.jackrabbit.rmi.server.ServerSession.save(ServerSession.java:265) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346) at sun.rmi.transport.Transport$1.run(Transport.java:200) at sun.rmi.transport.Transport$1.run(Transport.java:197) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:196) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:748) at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276) at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:162) at org.apache.jackrabbit.rmi.server.ServerXASession_Stub.save(Unknown Source) at org.apache.jackrabbit.rmi.client.ClientSession.save(ClientSession.java:272)
Please note that I am using JCR 2.0 Also if I change the code to session.refresh(false)
, the code works fine but I m not able to find the node identifier from session for deleting the same which is my original issue.
回答1:
Why are you creating a node at the destination then copying to the same place? I believe the stale exception is because the call to copy
has updated the underlying node making your destNode
reference stale/out-of-date.
Simply remove the addNode
then do something like ...
String destPath = "Copy_Test_" + System.currentTimeMillis()";
session.getWorkspace().copy(srcNode.getPath(), destPath);
Node destNode = session.getPath(destPath);
回答2:
As @TedTrippin pointed out the issue was with creating a destination node before copy which was not required. As part of copy, the node is created. So my final working code is as follows:
Node srcNode = session.getNodeByIdentifier("SOURCE_NODE_ID");
if(srcNode == null) {
System.out.println("File not found");
}
Node rootNode = session.getRootNode();
Node appNode = rootNode.getNode("JACKRABBIT");
String destNodeName = "Copy_Test";
session.getWorkspace().copy(srcNode.getPath(),appNode.getPath() + "/" + destNodeName);
Node destNode = appNode.getNode(destNodeName);
destNode.addMixin(MIX_VERSIONABLE);
destNode.addMixin(MIX_LOCKABLE);
destNode.addMixin(MIX_REFERENCEABLE);
session.refresh(true);
session.save();
来源:https://stackoverflow.com/questions/46253975/node-identifier-of-copied-node-in-jackrabbit-not-found-while-deleting