删除时找不到 Jackrabbit 中复制节点的节点标识符

Node Identifier of copied node in Jackrabbit not found while deleting

我使用 session.getWorkspace().copy(sourceNode.getPath(), destinationNode.getPath())

在 Jackrabbit 中复制了一个节点

据我所知,此操作更改会立即保留。但是当我尝试使用 session.getNodeByIdentifier("nodeId of copied node") 获取复制的节点以将其删除时,它给出了 ItemNotFoundException。该错误的原因是复制的节点在复制过程中丢失 mix:referenceable 属性 导致 getNodeByIdentifier 失败。

问题是如何将 mix:referenceable 属性 设置为复制的节点,因为复制操作后我无法从会话中获取节点。有人可以帮我解决这个问题吗?

更新:

代码:

    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 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.run(Transport.java:200) at sun.rmi.transport.Transport.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[=16=](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)

请注意,我使用的是 JCR 2.0 另外,如果我将代码更改为 session.refresh(false),代码工作正常,但我无法从会话中找到节点标识符以删除我原来的节点标识符问题。

为什么要在目的地创建一个节点然后复制到同一个地方?我相信陈旧的异常是因为对 copy 的调用更新了基础节点,使您的 destNode 引用 stale/out-of-date.

只需删除 addNode 然后执行类似...

String destPath = "Copy_Test_" + System.currentTimeMillis()";
session.getWorkspace().copy(srcNode.getPath(), destPath);
Node destNode = session.getPath(destPath);

正如@TedTrippin 所指出的,问题在于在复制之前创建一个不需要的目标节点。作为复制的一部分,节点被创建。所以我最终的工作代码如下:

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();